This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from TeenBiscuits/t1-añadir-ejemplo-final
Añadido el último ejemplo del Tema 1
- Loading branch information
Showing
14 changed files
with
342 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
cmake_minimum_required(VERSION 3.27) | ||
project(TAD_Rational) | ||
|
||
set(CMAKE_C_STANDARD 99) | ||
|
||
add_executable(Rational_struct main.c | ||
rational_struct.c | ||
rational_struct.h) | ||
add_executable(Rational_pointer main.c | ||
rational_pointer.c | ||
rational_pointer.h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include <stdio.h> | ||
|
||
//#include "rational_struct.h" | ||
//#include "rational_pointer.h" | ||
|
||
int main() { | ||
Rational r1, r2, r3, r4, s; | ||
|
||
r1 = createRational(2, 3); | ||
r2 = createRational(5, 7); | ||
|
||
r3 = createRational(7, 3); | ||
r4 = createRational(5, 4); | ||
|
||
s = sum(r1, r2); | ||
printf("The sum is %d/ %d\n", numerator(s), denominator(s)); | ||
s = sum(r3, r4); | ||
printf("The sum is %d/ %d\n", numerator(s), denominator(s)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include <stdlib.h> | ||
#include "rational_pointer.h" | ||
|
||
// Operación para crear un racional | ||
Rational createRational(int n, int d) { | ||
Rational temp; | ||
|
||
temp = malloc(sizeof(*temp)); | ||
temp->num = n; | ||
temp->den = d; | ||
return temp; | ||
} | ||
|
||
// Operación que retorna el numerador de un racional | ||
int numerator(Rational r) { | ||
return r->num; | ||
} | ||
|
||
// Operación que retorna el denominador de un racional | ||
int denominator(Rational r) { | ||
return r->den; | ||
} | ||
|
||
// Operación que retorna la suma de dos racionales | ||
Rational sum(Rational r1, Rational r2) { | ||
Rational s; | ||
|
||
s = malloc(sizeof(*s)); | ||
s->num = r1->num * r2->den + r2->num * r1->den; | ||
s->den = r1->den * r2->den; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#ifndef TAD_Rational_RATIONAL_POINTER_H | ||
#define TAD_Rational_RATIONAL_POINTER_H | ||
|
||
typedef struct Data *Rational; | ||
struct Data { | ||
int num; | ||
int den; | ||
}; | ||
|
||
Rational createRational(int n, int d); | ||
|
||
int numerator(Rational r); | ||
|
||
int denominator(Rational r); | ||
|
||
Rational sum(Rational r1, Rational r2); | ||
|
||
#endif // TAD_Rational_RATIONAL_POINTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include "rational_struct.h" | ||
|
||
// Operación para crear un racional | ||
Rational createRational(int n, int d) { | ||
Rational temp; | ||
temp.num = n; | ||
temp.den = d; | ||
return temp; | ||
} | ||
|
||
// Operación que retorna el numerador de un racional | ||
int numerator(Rational r) { | ||
return r.num; | ||
} | ||
|
||
// Operación que retorna el denominador de un racional | ||
int denominator(Rational r) { | ||
return r.den; | ||
} | ||
|
||
// Operación que retorna la suma de dos racionales | ||
Rational sum(Rational r1, Rational r2) { | ||
Rational s; | ||
|
||
s.num = r1.num * r2.den + r2.num * r1.den; | ||
s.den = r1.den * r2.den; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#ifndef TAD_Rational_RATIONAL_STRUCT_H | ||
#define TAD_Rational_RATIONAL_STRUCT_H | ||
|
||
typedef struct Data Rational; | ||
struct Data { | ||
int num; | ||
int den; | ||
}; | ||
|
||
Rational createRational(int n, int d); | ||
|
||
int numerator(Rational r); | ||
|
||
int denominator(Rational r); | ||
|
||
Rational sum(Rational r1, Rational r2); | ||
|
||
#endif // TAD_Rational_RATIONAL_STRUCT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
cmake_minimum_required(VERSION 3.27) | ||
project(TAD_Rational) | ||
|
||
set(CMAKE_C_STANDARD 99) | ||
|
||
add_executable(Rational_struct main.c | ||
rational_struct.c | ||
rational_struct.h) | ||
add_executable(Rational_pointer main.c | ||
rational_pointer.c | ||
rational_pointer.h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include <stdio.h> | ||
|
||
//#include "rational_struct.h" | ||
//#include "rational_pointer.h" | ||
|
||
int main() { | ||
Rational r1, r2, r3, r4, s; | ||
|
||
r1 = createRational(2, 3); | ||
r2 = createRational(5, 7); | ||
|
||
r3 = createRational(7, 3); | ||
r4 = createRational(5, 4); | ||
|
||
s = sum(r1, r2); | ||
printf("The sum is %d/ %d\n", numerator(s), denominator(s)); | ||
s = sum(r3, r4); | ||
printf("The sum is %d/ %d\n", numerator(s), denominator(s)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include <stdlib.h> | ||
#include "rational_pointer.h" | ||
|
||
// Operación para crear un racional | ||
Rational createRational(int n, int d) { | ||
Rational temp; | ||
|
||
temp = malloc(sizeof(*temp)); | ||
temp->num = n; | ||
temp->den = d; | ||
return temp; | ||
} | ||
|
||
// Operación que retorna el numerador de un racional | ||
int numerator(Rational r) { | ||
return r->num; | ||
} | ||
|
||
// Operación que retorna el denominador de un racional | ||
int denominator(Rational r) { | ||
return r->den; | ||
} | ||
|
||
// Operación que retorna la suma de dos racionales | ||
Rational sum(Rational r1, Rational r2) { | ||
Rational s; | ||
|
||
s = malloc(sizeof(*s)); | ||
s->num = r1->num * r2->den + r2->num * r1->den; | ||
s->den = r1->den * r2->den; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#ifndef TAD_Rational_RATIONAL_POINTER_H | ||
#define TAD_Rational_RATIONAL_POINTER_H | ||
|
||
typedef struct Data *Rational; | ||
struct Data { | ||
int num; | ||
int den; | ||
}; | ||
|
||
Rational createRational(int n, int d); | ||
|
||
int numerator(Rational r); | ||
|
||
int denominator(Rational r); | ||
|
||
Rational sum(Rational r1, Rational r2); | ||
|
||
#endif // TAD_Rational_RATIONAL_POINTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#include "rational_struct.h" | ||
|
||
// Operación para crear un racional | ||
Rational createRational(int n, int d) { | ||
Rational temp; | ||
temp.num = n; | ||
temp.den = d; | ||
return temp; | ||
} | ||
|
||
// Operación que retorna el numerador de un racional | ||
int numerator(Rational r) { | ||
return r.num; | ||
} | ||
|
||
// Operación que retorna el denominador de un racional | ||
int denominator(Rational r) { | ||
return r.den; | ||
} | ||
|
||
// Operación que retorna la suma de dos racionales | ||
Rational sum(Rational r1, Rational r2) { | ||
Rational s; | ||
|
||
s.num = r1.num * r2.den + r2.num * r1.den; | ||
s.den = r1.den * r2.den; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: Facultade de Informática de A Coruña | ||
|
||
#ifndef TAD_Rational_RATIONAL_STRUCT_H | ||
#define TAD_Rational_RATIONAL_STRUCT_H | ||
|
||
typedef struct Data Rational; | ||
struct Data { | ||
int num; | ||
int den; | ||
}; | ||
|
||
Rational createRational(int n, int d); | ||
|
||
int numerator(Rational r); | ||
|
||
int denominator(Rational r); | ||
|
||
Rational sum(Rational r1, Rational r2); | ||
|
||
#endif // TAD_Rational_RATIONAL_STRUCT_H |