Advertisement
Guest User

Untitled

a guest
May 28th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cstring>
  6. #include <fstream>
  7. #include <cmath>
  8. #include "Arboles.h"
  9.  
  10. using namespace std;
  11.  
  12. void *memoriaExacta(void **arr,int cant){
  13.     void **cadena = new void*[cant+1];
  14.     for(int i = 0;i < cant ;i++){
  15.         cadena[i] = arr[i];
  16.     }
  17.     cadena[cant] = NULL;
  18.     return cadena;
  19. }
  20.  
  21. void *leerPrimeraParte(ifstream& inFile){
  22.     int codAlumno;
  23.     double tarifa;
  24.     char *nombreCompleto,buffNombre[500];
  25.    
  26.     if(!(inFile >> codAlumno)){
  27.         inFile.clear();
  28.         return NULL;
  29.     }
  30.     inFile >> buffNombre;
  31.     inFile >> tarifa;
  32.  
  33.     double totalPagar = 0.0;
  34.     double *ptrTotal = new double;
  35.     *ptrTotal = totalPagar;
  36.  
  37.     nombreCompleto = new char[strlen(buffNombre) + 1];
  38.     strcpy(nombreCompleto,buffNombre);
  39.  
  40.     int *ptrCod = new int;
  41.     *ptrCod = codAlumno;
  42.     double *ptrTarifa = new double;
  43.     *ptrTarifa = tarifa;
  44.  
  45.     void **dato = new void*[4];
  46.  
  47.     dato[0] = ptrCod;
  48.     dato[1] = nombreCompleto;
  49.     dato[2] = ptrTarifa;
  50.     dato[3] = ptrTotal;
  51.     return dato;
  52. }
  53.  
  54. void inicializar_ABB(void *&ABB){
  55.     ABB = NULL;
  56. }
  57.  
  58. int comparar(void *reg1,void *reg2){
  59.     void **regD1 = (void **)reg1;
  60.     void **regD2 = (void **)reg2;
  61.    
  62.     int *dat1 = (int *)regD1[0];
  63.     int *dat2 = (int *)regD2[0];
  64.    
  65.     return *dat1 - *dat2;
  66. }
  67.  
  68. void ABB_insertar(void *&ABB,void *dato){
  69.     void **ptrRec = (void **)ABB;
  70.    
  71.     if(ptrRec == NULL){
  72.         //cout << "Dato 1 "<<palabra <<endl;
  73.         void **ptrNuevo = new void*[3];
  74.         ptrNuevo[0] = dato;
  75.         ptrNuevo[1] = NULL;
  76.         ptrNuevo[2] = NULL;
  77.         ABB = ptrNuevo;
  78.         return ;
  79.     }else{
  80.         if(comparar(ptrRec[0],dato)<0){
  81.  
  82.             ABB_insertar(ptrRec[2],dato);
  83.         }else{
  84.             if(comparar(ptrRec[0],dato)>0){
  85.                 ABB_insertar(ptrRec[1],dato);
  86.             }else{
  87.                 return ;
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. void imprimir(void *ABB,ofstream& outFile){
  94.     if(ABB!=NULL){
  95.         void **nodo = (void **)ABB;
  96.        
  97.         void **reg = (void **)nodo[0];
  98.         int *cod = (int *)reg[0];
  99.         char *nom = (char *)reg[1];
  100.         double *tar = (double *)reg[2];
  101.         double *pre = (double *)reg[3];
  102.         imprimir(nodo[1],outFile);
  103.         outFile << setw(12) << left << *cod << setw(60) << left << nom
  104.                 << setw(12)<< left <<fixed << setprecision(2) << *tar << fixed << setprecision(2) << *pre <<endl;
  105.         imprimir(nodo[2],outFile);
  106.     }
  107. }
  108.  
  109. void leerArbol(void *&ABB,void *(*ptrLeer)(ifstream& inFile),void (*ABB_insertar)(void *&ABB,void *dato),ifstream&inFile){
  110.     void *dato;
  111.     while(1){
  112.         dato = ptrLeer(inFile);
  113.         if(dato == NULL) break;
  114.         ABB_insertar(ABB,dato);
  115.     }
  116. }
  117.  
  118. void imprimirLibros(void *cursos){
  119.     void **arrCursos = (void **)cursos;
  120.     for(int i = 0;arrCursos[i];i++){
  121.         void**regCurso = (void **)arrCursos[i];
  122.         char *codCur = (char *)regCurso[0];
  123.         double *ptrPu = (double *)regCurso[1];
  124.         cout << setw(12) << left << codCur << fixed <<
  125.                 setprecision(2) << *ptrPu << endl;
  126.     }
  127. }
  128.  
  129. int cmpCodLib(const void *dato1,const void *dato2){
  130.     void ***regDato1 = (void ***)dato1;
  131.     void ***regDato2 = (void ***)dato2;
  132.     void **reg1 = (void **)regDato1[0];
  133.     void **reg2 = (void **)regDato2[0];
  134.     char *cad1 = (char *)reg1[0];
  135.     char *cad2 = (char *)reg2[0];
  136.     return strcmp(cad1,cad2);
  137. }
  138.  
  139. void leerLibros(void *&cursos,ifstream& inFile){
  140.     char buffCodLib[25],*codLib;
  141.     double pu;
  142.     void *buffCursos[500];
  143.     int cantLib = 0;
  144.     while(1){
  145.         inFile >> buffCodLib;  
  146.         if(strcmp(buffCodLib,"FIN")==0) break;
  147.         //cout << buffCodLib << endl;
  148.         inFile >> pu;
  149.         codLib = new char[strlen(buffCodLib) + 1];
  150.         strcpy(codLib,buffCodLib);
  151.         void **libros = new void*[2];
  152.         double* ptrPu = new double;
  153.         *ptrPu = pu;
  154.         libros[0] = codLib;
  155.         libros[1] = ptrPu;
  156.         buffCursos[cantLib] = libros;
  157.         cantLib++;
  158.     }
  159.     cursos = memoriaExacta(buffCursos,cantLib);
  160.     qsort(cursos,cantLib - 1,sizeof(void *),cmpCodLib);
  161.     //imprimirLibros(cursos);
  162. }
  163.  
  164. int compararDato(void *dato1,void *dato2){
  165.     void **da1 = (void **)dato1;
  166.     void **da2 = (void **)dato2;
  167.    
  168.     int* nom1 = (int *)da1[0];
  169.     int* nom2 = (int *)da2[0];
  170.    
  171.     return *nom1 - *nom2;
  172. }
  173.  
  174. void* buscarArbol(void *ABB,void* dato){
  175.     void **ptrRec = (void **)ABB;
  176.     if(ptrRec == NULL){
  177.         return NULL;
  178.     }else{
  179.         if(compararDato(ptrRec[0],dato)<0){
  180.             buscarArbol(ptrRec[2],dato);
  181.         }else{
  182.             if(compararDato(ptrRec[0],dato)>0){
  183.                 buscarArbol(ptrRec[1],dato);
  184.             }else{
  185.                 return ABB;
  186.             }
  187.         }
  188.     }
  189. }
  190.  
  191. double buscarPrecio(char *cod,void *Cursos){
  192.     void **arrCur = (void **)Cursos;
  193.     for(int i = 0;arrCur[i];i++){
  194.         void **regCur = (void **)arrCur[i];
  195.         char *codCur = (char *)regCur[0];
  196.         double *ptrMonto = (double *)regCur[1];
  197.         if(strcmp(codCur,cod)==0) return *ptrMonto;
  198.     }
  199.     return -1.0;
  200. }
  201.  
  202. void agregarCosto(void *ABB,void *Cursos,ifstream& inFile){
  203.     char *cod,buffCod[12];
  204.     int codAl, cant;
  205.    
  206.     while((inFile >> buffCod)){
  207.         inFile >> codAl >> cant;
  208.        
  209.         cod = new char[strlen(buffCod) + 1];
  210.         strcpy(cod,buffCod);
  211.        
  212.         double pu = buscarPrecio(cod,Cursos);
  213.        
  214.         if(pu!=-1.0){
  215.             void **dato = new void*[1];
  216.             int *ptrCodAl = new int;
  217.             *ptrCodAl = codAl;
  218.             dato[0] = ptrCodAl;
  219.             void *regAlm = buscarArbol(ABB,dato);
  220.             if(regAlm!=NULL){
  221.                 void **regAlumno = (void **)regAlm;
  222.                 void **nodo = (void **)regAlumno[0];
  223.                
  224.                 double *ptrMonto = (double *)nodo[3];
  225.                 double *ptrTarifa = (double *)nodo[2];
  226.                 cout << "Pu = "<<pu << " Tarifa= "<<*ptrTarifa<<endl;
  227.                 *ptrMonto +=(pu*(*ptrTarifa/100));
  228.                 ptrMonto = (double *)nodo[3];
  229.                
  230.             }else cout << "MAL MAL\n";
  231.         }else cout << "MAL MAL\n";
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement