Advertisement
pmanriquez93

LP OAC

Oct 13th, 2014
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.24 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: alulab14
  4.  *
  5.  * Created on 11 de octubre de 2014, 10:38 AM
  6.  */
  7.  
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <cstring>
  11.  
  12. #include "funcs.h"
  13.  
  14. using namespace std;
  15.  
  16. /*
  17.  *
  18.  */
  19. int main(void) {
  20.     void *autores, *separados;
  21.     leeAutores(autores);
  22.     imprimirAutores(autores);
  23.     leeVentas(autores);
  24.    
  25.    
  26.     //pagoAutores(autores/*,????*/);
  27.     //separarAutores(autores,separados/*,????*/);
  28.     //reporte(autores,separados);
  29.     return 0;
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. /*
  42.  * File:   funcs.h
  43.  * Author: alulab14
  44.  *
  45.  * Created on 11 de octubre de 2014, 11:02 AM
  46.  */
  47.  
  48. #ifndef FUNCS_H
  49. #define FUNCS_H
  50.  
  51. void leeAutores(void *&);
  52. void imprimirAutores(void *);
  53.  
  54. void **colocarAutor(int *,char *,double *);
  55. void **memoriaExacta(void **,int);
  56.  
  57.  
  58. void leeVentas(void *&);
  59. void imprimeVentas(void *, int []);
  60.  
  61.  
  62.  
  63. int buscarAutor(void *,int);
  64. void inicializarArreglo(int [], int);
  65. void agregarLibro(void **,char *, int , int[], int);
  66. void crearListaAut(void **&,char *,int);
  67. int buscarLibro(char **, char *, int);
  68. void aumentarEspacios(void **, int);
  69.  
  70.  
  71.  
  72.  
  73. #endif  /* FUNCS_H */
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. #include <cstdlib>
  84. #include <cstdio>
  85. #include <cstring>
  86.  
  87. #include "funcs.h"
  88.  
  89. void leeAutores(void *&autores){
  90.     void *auxAutores[500];       int numAutores = 0;  
  91.  
  92.     int buffCodAutor, i = 0;
  93.     while (1){
  94.         // Leo todo normal
  95.         if (scanf("%d",&buffCodAutor)==0) break;
  96.         double buffPorcAutor;   char buffPal[30], buffNombAutor[100];
  97.        
  98.         scanf("%s",buffNombAutor);  
  99.         while (1){
  100.             if(scanf("%lf",&buffPorcAutor)==1) break;
  101.             scanf("%s",buffPal);
  102.             if (i > 1) strcat(buffNombAutor,"-");
  103.             else strcat(buffNombAutor,"/");
  104.             strcat(buffNombAutor,buffPal);
  105.             i++;
  106.         }
  107.         i = 0; //Reinicio el contador de apellidos
  108.         // Le doy memoria exacta a todos
  109.         double *porcAutor;      char *nombAutor;    int *codAutor;
  110.         codAutor = new int;
  111.         *codAutor = buffCodAutor;
  112.        
  113.         nombAutor = new char[strlen(buffNombAutor)+1];
  114.         strcpy(nombAutor,buffNombAutor);
  115.        
  116.         porcAutor = new double;
  117.         *porcAutor = buffPorcAutor;
  118.        
  119.         auxAutores[numAutores] = colocarAutor(codAutor,nombAutor,porcAutor);
  120.         numAutores++;
  121.     }
  122.     autores = memoriaExacta(auxAutores,numAutores);    
  123. }
  124.  
  125. void **colocarAutor(int *codAutor,char *nombAutor,double *porcAutor){
  126.     void **regAutor = new void*[4];
  127.     regAutor[0] = codAutor;
  128.     regAutor[1] = nombAutor;
  129.     regAutor[2] = porcAutor;
  130.     regAutor[3] = NULL;
  131.     return regAutor;
  132. }
  133.  
  134. void **memoriaExacta(void **auxAutores,int cantDatos){
  135.     void **autores = new void*[cantDatos+1];
  136.     for(int i = 0; i < cantDatos; i++)
  137.         autores[i] = auxAutores[i];
  138.     autores[cantDatos] = NULL;
  139.     return autores;
  140. }
  141.  
  142. void imprimirAutores(void *autoresVoid){
  143.     void **autores = (void**) autoresVoid;
  144.     for(int i = 0; autores[i]; i++){
  145.         void **autor = (void**)autores[i];
  146.         int *cod = (int *)(autor[0]);
  147.         char *nomb = (char *)(autor[1]);
  148.         double *porc = (double *)(autor[2]);
  149.         printf("%12d %-50s %4.2lf \n",*cod,nomb,*porc);
  150.     }
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. void leeVentas(void *&autoresVoid){
  159.     int i;
  160.     void **autores = (void **)autoresVoid;
  161.     for (i = 0; autores[i]; i++);
  162.     int cantAut = i;
  163.    
  164.     int cantLibrosxAut[cantAut + 1];
  165.     inicializarArreglo(cantLibrosxAut,cantAut);
  166.    
  167.     while (1){
  168.         char buffCodLib[10];        int buffCantLib, buffCodAut;
  169.         scanf("%s %d %d",buffCodLib,&buffCodAut,&buffCantLib);
  170.         if (strcmp(buffCodLib,"*******") == 0) break;
  171.        
  172.        
  173.         int numAut;
  174.         numAut = buscarAutor(autores,buffCodAut);  
  175.         agregarLibro(autores,buffCodLib,buffCantLib,cantLibrosxAut,numAut);        
  176.     }
  177.     imprimeVentas(autores,cantLibrosxAut);    
  178. }
  179.  
  180. void inicializarArreglo(int arr[], int numDat){
  181.     for (int i = 0; i < numDat; i++)
  182.         arr[i] = 0;
  183. }
  184.  
  185. int buscarAutor(void *autores,int codAut){
  186.     void **ptrAutor = (void **)autores;  
  187.     for (int i = 0; ptrAutor[i]; i++){
  188.         void **ptrDatos = (void **)ptrAutor[i];
  189.         int *codigo = (int*)(ptrDatos[0]);
  190.         if (codAut == *codigo) return i;
  191.     }
  192.     return -1;    
  193. }
  194.  
  195. void agregarLibro(void **autores,char *codLib, int cantLib,
  196.                   int *cantLibrosxAut  , int numAut){
  197.     void **autor = (void **)autores[numAut];  
  198.     if (autor[3] == NULL){ // Primer libro de este autor
  199.         crearListaAut(autor,codLib,cantLib);
  200.         autores[numAut]=autor;
  201.         cantLibrosxAut[numAut]++;
  202.     }    
  203.     else{
  204.         char **libros = (char **)autor[3];
  205.         int * cantidad = (int *)autor[4];      
  206.         int n = buscarLibro(libros,codLib,cantLibrosxAut[numAut]);
  207.         if (n == -1){  // No lo encontro
  208.             if (cantLibrosxAut[numAut] % 5 == 0){
  209.                 aumentarEspacios(autor,cantLibrosxAut[numAut]);
  210.                 libros = (char**)(autor[3]);
  211.                 cantidad = (int *)(autor[4]);                
  212.             }
  213.             char *codigo=new char[strlen(codLib)+1];
  214.             strcpy(codigo,codLib);
  215.             libros[cantLibrosxAut[numAut]] = codigo;
  216.             cantidad[cantLibrosxAut[numAut]] = cantLib;
  217.             cantLibrosxAut[numAut]++;
  218.         }
  219.         else  // Encontro el libro
  220.             cantidad[n] = cantidad[n] + cantLib;
  221.     }
  222. }
  223.  
  224. int buscarLibro(char **libros, char *codLib, int cant){
  225.     for (int i = 0; i < cant; i++)
  226.         if (strcmp(libros[i],codLib)== 0) return i;
  227.     return -1;
  228. }
  229.  
  230. void aumentarEspacios(void **autor, int cantLibrosxAut){
  231.     char **libros = (char **)(autor[3]);
  232.     int *cantidad = (int *)(autor[4]);
  233.    
  234.     cantLibrosxAut += 5;
  235.     char **auxLib = new char*[cantLibrosxAut];
  236.     int *auxCant = new int[cantLibrosxAut];
  237.     for (int i = 0; i<cantLibrosxAut; i++){
  238.         auxLib[i] = libros[i];
  239.         auxCant[i] = cantidad[i] ;
  240.     }
  241.     delete[]libros;
  242.     delete[]cantidad;
  243.     autor[3] = auxLib;
  244.     autor[4] = auxCant;
  245. }
  246.  
  247. void crearListaAut(void **&autor,char *codLib,int cantLib){
  248.     void **aux = new void *[5];
  249.    
  250.     aux[0] = autor[0];
  251.     aux[1] = autor[1];
  252.     aux[2] = autor[2];
  253.    
  254.     // Espacio para nuevas estructuras
  255.     char **codigos = new char *[5];
  256.     int *cantidad = new int [5];
  257.    
  258.     char *cod = new char[strlen(codLib)+1];
  259.     strcpy(cod,codLib);
  260.  
  261.    
  262.     codigos[0] = cod;
  263.     cantidad[0] = cantLib;
  264.    
  265.     aux[3] = codigos;
  266.     aux[4] = cantidad;  
  267.    
  268.     delete[]autor;
  269.     autor = aux;
  270. }
  271.  
  272. void imprimeVentas(void *autoresVoid,int cantLibxAut[]){
  273.     void **autores = (void **) autoresVoid;
  274.     for (int i = 0; autores[i]; i++){
  275.         void **autor = (void **)(autores[i]);
  276.         int *cod = (int *)(autor[0]);
  277.         char *nomb = (char *)(autor[1]);
  278.         double *porc = (double *)(autor[2]);
  279.         printf("\n\n\n%12d %-50s %4.2lf \n",*cod,nomb,*porc);
  280.         if (autor[3]){        
  281.             char **libros = (char **)autor[3];      // arreglo de libros
  282.             int *cant = (int *)autor[4];
  283.             for (int j = 0; j < cantLibxAut[i]; j++)
  284.                 printf("  %-10s %d  ",libros[j],cant[j]);
  285.         }    
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement