From fba9c34c547c99d56b4bf2d83caf5c68a638107f Mon Sep 17 00:00:00 2001 From: Mitsuo Tokumori Date: Fri, 20 Oct 2023 07:20:44 -0500 Subject: Add p1 and p2 solution for 2023-2 E1 --- 2023-2/E1/mitsuo/p2/fun.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 2023-2/E1/mitsuo/p2/fun.cpp (limited to '2023-2/E1/mitsuo/p2/fun.cpp') diff --git a/2023-2/E1/mitsuo/p2/fun.cpp b/2023-2/E1/mitsuo/p2/fun.cpp new file mode 100644 index 0000000..9e61144 --- /dev/null +++ b/2023-2/E1/mitsuo/p2/fun.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + +#include +#include +#include "fun.hpp" + +using namespace std; + +enum Reg {COD, NOM, PRECIO, STOCK}; + +void cargarProductos(void *&prod, const char *nombArch){ + ifstream in(nombArch); + if(!in) { + cerr << "Error: no se pudo abrir archivo\n"; + exit(1); + } + void **productos=nullptr; + char *cod; + int nd=0, cap=0; + while(1){ + cod = leeCad(in,','); + if(in.eof()) break; + if(nd==cap) aumentaEspacios(productos,nd,cap); + guardaProd(in, productos[nd-1], cod); + nd++; + } + prod = productos; + qsort(prod, nd-1, sizeof(void *), cmpProducto); +} + +void aumentaEspacios(void**&productos, int &nd, int &cap){ + void **aux; + cap += INCREMENTO; + if(productos==nullptr){ + productos = new void * [cap]{}; + nd=1; + } + else{ + aux = new void * [cap]{}; + for(int i=0;i>precio; + arch.get(); + arch>>stock; + arch>>ws; + reg = new void * [4] {}; + reg[COD] = cod; + reg[NOM] = nomb; + reg[PRECIO] = new double (precio); + reg[STOCK] = new int (stock); + return reg; +} + +int cmpProducto(const void *a, const void *b){ + void **ai = (void**)a, **bi = (void**)b; + void **duplaA = (void**)(*ai), **duplaB = (void**)(*bi); + void **regA = (void**)(duplaA[0]), **regB = (void**)(duplaB[0]); + char *codA = (char *)(regA[0]), *codB = (char*)(regB[0]); + return strcmp(codA, codB); +} + +void pruebaDeCargaDeProductos(void *prod){ + void **productos = (void**)prod; + ofstream out("PruebaProductos.txt"); + if(!out){ + cout<<"error al abrir archivo PruebaProductos.txt"<