diff options
Diffstat (limited to '2022-2/L6')
| -rw-r--r-- | 2022-2/L6/mitsuo/BibliotecaPilaGenerica.cpp | 99 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/BibliotecaPilaGenerica.h | 14 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/Makefile | 16 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/Medicinas.csv | 25 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/PilaConEnteros.h | 0 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/PilaConRegistros.h | 0 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/fun.cpp | 69 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/fun.h | 11 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/main.cpp | 27 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/numeros.txt | 3 | ||||
| -rw-r--r-- | 2022-2/L6/mitsuo/repnumeros.txt | 0 |
11 files changed, 264 insertions, 0 deletions
diff --git a/2022-2/L6/mitsuo/BibliotecaPilaGenerica.cpp b/2022-2/L6/mitsuo/BibliotecaPilaGenerica.cpp new file mode 100644 index 0000000..01a7dc3 --- /dev/null +++ b/2022-2/L6/mitsuo/BibliotecaPilaGenerica.cpp @@ -0,0 +1,99 @@ +#include <iostream> +#include <fstream> +#include "BibliotecaPilaGenerica.h" + +using namespace std; + +/* NEXT: (void *), DATA: (int *) */ +enum nodeIndices { NEXT, DATA }; + +void cargapila(void *&pila, void *arreglo) +{ + int i; + int **numbers = (int **) arreglo; + pila = nullptr; + for (i = 0; numbers[i]; i++) { + push(pila, numbers[i]); + } +} + +void push(void *&pila, void *data) +{ + void **node = new void * [2]; + node[NEXT] = pila; + node[DATA] = data; + pila = node; +} + +void *pop(void *&pila) +{ + void **node; + if (pila == nullptr) { + return nullptr; + } else { + node = (void **) pila; + pila = node[NEXT]; + return node[DATA]; + } +} + +int pilavacia(void *pila) +{ + return pila == nullptr; +} + +void imprimepila( + void *pila, + void (*imprimenumero)(ostream &out, void *), + const char *filename +) { + ofstream out(filename); + if (!out) { + cerr << "Error: no se pudo abrir archivo\n"; + } + void **next; + void **node; + int data; + + next = (void **) pila; + while (next) { + node = (void **) next; + imprimenumero(out, node[DATA]); + + next = (void **) node[NEXT]; + } +} + +/* hanoi */ +void muevepila(void *pilasrc, void *&piladest) +{ + void *pilaaux = nullptr; + + /* count elements */ + int i = 0; + void **next = (void **) pilasrc; + while (next) { + next = (void **) *next; + i++; + } + // cout << "n = " << i << '\n'; + hanoi(i, pilasrc, nullptr, piladest); +} + +static void hanoi(int n, void *pila1, void *pila2, void *pila3) +{ + /* FIXME: I don't know where it's the bug. Here or in pop(). Also the stack + * representation different from what is asked in the problem statement */ + void *data; + if (n == 0) { + return; + } else if (n == 1) { + data = pop(pila1); + push(pila3, data); + } else { + hanoi(n - 1, pila1, pila3, pila2); + data = pop(pila1); + push(pila3, data); + hanoi(n - 1, pila2, pila1, pila3); + } +} diff --git a/2022-2/L6/mitsuo/BibliotecaPilaGenerica.h b/2022-2/L6/mitsuo/BibliotecaPilaGenerica.h new file mode 100644 index 0000000..4fed61f --- /dev/null +++ b/2022-2/L6/mitsuo/BibliotecaPilaGenerica.h @@ -0,0 +1,14 @@ +#include <iostream> + +void cargapila(void *&pila, void *arreglo); +void push(void *&pila, void *data); +void *pop(void *&pila); +int pilavacia(void *pila); +void imprimepila( + void *pila, + void (*imprimenumero)(std::ostream &out, void *), + const char *filename +); +void muevepila(void *pilasrc, void *&piladest); + +static void hanoi(int n, void *pila1, void *pila2, void *pila3); diff --git a/2022-2/L6/mitsuo/Makefile b/2022-2/L6/mitsuo/Makefile new file mode 100644 index 0000000..f8bcb92 --- /dev/null +++ b/2022-2/L6/mitsuo/Makefile @@ -0,0 +1,16 @@ +# Just a simple C++ makefile +# +# Use with `$ make` +# Run build binary with `$ ./a.out` + + +# No file association to the "clean" target. Also no dependency. +# See more: https://stackoverflow.com/a/2145605/7498073 +.PHONY: all +all: + clang++ -g *.cpp && ./a.out + + +.PHONY: clean +clean: + rm *.o a.out diff --git a/2022-2/L6/mitsuo/Medicinas.csv b/2022-2/L6/mitsuo/Medicinas.csv new file mode 100644 index 0000000..b53f86c --- /dev/null +++ b/2022-2/L6/mitsuo/Medicinas.csv @@ -0,0 +1,25 @@ +60509,AMPICILINA 125MG SUSP 90ML,58.65
+73972,VITAMINA E 400 MG C90 CAPS,54.4
+94429,TIAMAZOL 5MG 20T,52.7
+43709,CEFUROXIMA 250MG SUSP,78.2
+82469,GUAYACOL AMPOLLETA 1ML,60.35
+93578,SUCRALFATO 40 TAB/1G,55.25
+26484,DILTIAZEM 30 TAB/30 MG,29.75
+95196,DIGOXINA 0.250 MG C/24,62.05
+65667,SUBSALICILATO/BISMUTO SUSP 120ML,61.2
+94415,PRAVASTATINA 10 MG C/15,68.85
+11370,KETOROLACO 4T 30MG SUBLINGUAL,2.55
+49644,HIDROCORTISONA CRA 30GR,73.95
+76238,BEZOCAINA 10GR GEL,56.95
+79189,CLORFENAMINA 4MG T20,73.1
+81333,CARNITINA C/30,40.8
+33311,ALBENDAZOL 200MG C/6 TAB,20.4
+62302,BEZAFIBRATO 200MG C/30TAB,41.65
+72495,ACIDO GLUTAMICO 600 MG C/100,43.35
+16813,VANCOMICINA 500MG SOL INY,5.95
+36711,TIBOLONA 2.5MG 30T,84.15
+55021,AMOXICILINA 500MG/5ML SUSP,82.45
+34080,ELECTROLIT FRESA 500ML,60.35
+36622,TEOFILINA 20 TAB/100 MG,57.8
+72969,ISOSORBIDE 10MG T/20,58.65
+40997,ELECTROLIT PED FRESA 625ML,41.65
diff --git a/2022-2/L6/mitsuo/PilaConEnteros.h b/2022-2/L6/mitsuo/PilaConEnteros.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/2022-2/L6/mitsuo/PilaConEnteros.h diff --git a/2022-2/L6/mitsuo/PilaConRegistros.h b/2022-2/L6/mitsuo/PilaConRegistros.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/2022-2/L6/mitsuo/PilaConRegistros.h diff --git a/2022-2/L6/mitsuo/fun.cpp b/2022-2/L6/mitsuo/fun.cpp new file mode 100644 index 0000000..569350c --- /dev/null +++ b/2022-2/L6/mitsuo/fun.cpp @@ -0,0 +1,69 @@ +#include <cstdlib> +#include <iostream> +#include <fstream> +#include <cstdlib> +#include "fun.h" + +using namespace std; + +void cargaarreglo(void *&arreglo, + int (*cmpnumero)(const void *, const void *), + int *leenumero(istream &in), + const char *filename) +{ + ifstream in(filename); + + if (!in) { + cerr << "Error, no se pudo abrir archivo\n"; + return; + } + + int **numbers; + int *data; + int *buffer[MAXLEN]; + int i; + /* load buffer */ + for (i = 0; (data = leenumero(in)); i++) { + buffer[i] = data; + } + buffer[i] = nullptr; + + /* create "exact" array of pointers to int */ + numbers = new int * [i + 1]; + for (i = 0; buffer[i]; i++) { + numbers[i] = buffer[i]; + } + numbers[i] = nullptr; + + /* sort array */ + + qsort(numbers, i, sizeof(void *), cmpnumero); + + // for (i = 0; numbers[i]; i++) { + // cout << *numbers[i] << '\n'; + // } + + /* modify parameter reference */ + arreglo = numbers; +} + +/* qsort comparisong function. Descending order */ +int cmpnumero(const void *p1, const void *p2) +{ + return - (** (int **) p1 - ** (int **) p2); +} + +/* allocates memory */ +int *leenumero(istream &in) +{ + int n; + if (in >> n) + return new int{n}; + else + return nullptr; +} + +void imprimenumero(ostream &out, void *data) +{ + cout << * (int *) data << '\n'; +} diff --git a/2022-2/L6/mitsuo/fun.h b/2022-2/L6/mitsuo/fun.h new file mode 100644 index 0000000..aaa34c9 --- /dev/null +++ b/2022-2/L6/mitsuo/fun.h @@ -0,0 +1,11 @@ +#include <iostream> + +#define MAXLEN 100 + +int cmpnumero(const void *p1, const void *p2); +int *leenumero(std::istream &in); +void cargaarreglo(void *&arreglo, + int (*cmpnumero)(const void *, const void *), + int *leenumero(std::istream &in), + const char *filename); +void imprimenumero(std::ostream &out, void *data);
\ No newline at end of file diff --git a/2022-2/L6/mitsuo/main.cpp b/2022-2/L6/mitsuo/main.cpp new file mode 100644 index 0000000..919fe1c --- /dev/null +++ b/2022-2/L6/mitsuo/main.cpp @@ -0,0 +1,27 @@ +/* Mitsuo Tokumori 20170895 */ + +#include <iostream> + +#include "BibliotecaPilaGenerica.h" +#include "PilaConEnteros.h" +#include "PilaConRegistros.h" +#include "fun.h" + +using namespace std; + +int main(int argc, char **argv) +{ + void *arreglo, *pilaini, *pilafin; + + cargaarreglo(arreglo, cmpnumero, leenumero, "numeros.txt"); + cargapila(pilaini, arreglo); + muevepila(pilaini, pilafin); + imprimepila(pilafin, imprimenumero, "repnumeros.txt"); +// imprimepila(pilafin, imprimenumero, "repnumeros.txt"); + +// cargaarreglo(arreglo, cmpregistro, leeregistro, "medicinas.csv"); +// cargapila(pilaini, arreglo); +// muevepila(pilaini, pilafin); +// imprimepila(pilafin, imprimeregistro, "repmedicinas.txt"); + return 0; +}
\ No newline at end of file diff --git a/2022-2/L6/mitsuo/numeros.txt b/2022-2/L6/mitsuo/numeros.txt new file mode 100644 index 0000000..9cae0c5 --- /dev/null +++ b/2022-2/L6/mitsuo/numeros.txt @@ -0,0 +1,3 @@ +13
+4
+1
diff --git a/2022-2/L6/mitsuo/repnumeros.txt b/2022-2/L6/mitsuo/repnumeros.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/2022-2/L6/mitsuo/repnumeros.txt |
