Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #98 from TeenBiscuits/t1-añadir-ejemplo-final
Browse files Browse the repository at this point in the history
Añadido el último ejemplo del Tema 1
  • Loading branch information
TeenBiscuits authored Mar 3, 2024
2 parents d545f1c + 418e9d3 commit e363049
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/CMakeLists.txt
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)
21 changes: 21 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/main.c
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));
}
34 changes: 34 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/rational_pointer.c
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;
}
20 changes: 20 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/rational_pointer.h
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
30 changes: 30 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/rational_struct.c
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;
}
20 changes: 20 additions & 0 deletions docs/code/Ejemplos/TAD_Rational/rational_struct.h
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
67 changes: 64 additions & 3 deletions docs/topics/Tema-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,72 @@ Podemos clasificarlas en:
- Observadoras / Acceso: No devuelve un TAD
- Destructoras: Devuelven el TAD

## Ejemplo de especificación de un TAD
## Ejemplo de especificación de un TAD: Rational

<!-- TODO ## Ejemplo de especificación de un TAD: Rational -->
Vamos a crear un TAD para los números racionales, es decir, un par de números enteros: Numerador y denominador

<warning><b>¡En construcción! <a href="https://github.com/TeenBiscuits/Pro2324">Colabora</a></b></warning>
```tex
\frac{Numerador}{Denominador}
```

**Constructoras Generadoras**

- ```tex
CreateRational (Entero_{1}, Entero_{2}) \rightarrow Rational
```

Objetivo: Crea un número racional.

Entrada: Numerador del nuevo racional y denominador del nuevo racional.

Precondición: Denominador distinto de 0.

Salida: El número racional creado.

**Constructoras Modificadoras**

- ```tex
Sum (Rational_{1}, Rational_{2}) \rightarrow Rational_{3}
```

Objetivo: Calcula la suma de dos números racionales.

Entrada: Números racionales a sumar.

Salida: Un nuevo racional suma de los números a la entrada.

**Observadoras**

- ```tex
Numerator (Rational) \rightarrow Entero
```

Objetivo: Obtiene el numerador de un número racional.

Entrada: Número racional del que obtener el numerador.

Salida: Numerador del número a la entrada.

- ```tex
Denominator (Rational) \rightarrow Entero
```

Objetivo: Obtiene el denominador de un número racional.

Entrada: Número racional del que obtener el denominador.

Salida: Denominador del número a la entrada.

### Rational TAD: El código

<code-block lang="C" src="./Ejemplos/TAD_Rational/main.c" collapsible="true" collapsed-title="main.c"/>

<code-block lang="C" src="./Ejemplos/TAD_Rational/rational_pointer.c" collapsible="true" collapsed-title="rational_pointer.c"/>

<code-block lang="C" src="./Ejemplos/TAD_Rational/rational_pointer.h" collapsible="true" collapsed-title="rational_pointer.h"/>

<code-block lang="C" src="./Ejemplos/TAD_Rational/rational_struct.c" collapsible="true" collapsed-title="rational_struct.c"/>

<code-block lang="C" src="./Ejemplos/TAD_Rational/rational_struct.h" collapsible="true" collapsed-title="rational_struct.h"/>

<code-block lang="CMAKE" src="./Ejemplos/TAD_Rational/CMakeLists.txt" collapsible="true" collapsed-title="CMakeLists.txt"/>
3 changes: 2 additions & 1 deletion src/Ejemplos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ project(Ejemplos C)

set(CMAKE_C_STANDARD 99)

#add_executable(Ejemplo-01 Ejemplo-01.c)
#add_executable(Ejemplo-01 Ejemplo-01.c)
add_subdirectory(./TAD_Rational)
13 changes: 13 additions & 0 deletions src/Ejemplos/TAD_Rational/CMakeLists.txt
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)
21 changes: 21 additions & 0 deletions src/Ejemplos/TAD_Rational/main.c
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));
}
34 changes: 34 additions & 0 deletions src/Ejemplos/TAD_Rational/rational_pointer.c
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;
}
20 changes: 20 additions & 0 deletions src/Ejemplos/TAD_Rational/rational_pointer.h
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
30 changes: 30 additions & 0 deletions src/Ejemplos/TAD_Rational/rational_struct.c
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;
}
20 changes: 20 additions & 0 deletions src/Ejemplos/TAD_Rational/rational_struct.h
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

0 comments on commit e363049

Please sign in to comment.