Advertisement
Guest User

Mezcla

a guest
Apr 8th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void LeerElemDetectandoFin(fstream &arch, string &comp, bool &finArch);
  8. void PasarElemDetectandoFin(fstream &archOrigen,fstream &archDestino, string &comp, bool &finArchOrigen);
  9. void Mezcla(fstream &aux1,fstream &aux2,fstream &arch);
  10. void Division(fstream &arch,fstream &aux1,fstream &aux2,bool &esVacio2);
  11.  
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     fstream arch,aux1,aux2;
  16.     bool esVacio2 = false;
  17.     while(!esVacio2)
  18.     {
  19.        arch.open(argv[1],ios::in);
  20.        aux1.open(argv[2],ios::out);
  21.        aux2.open(argv[3],ios::out);
  22.        Division(arch,aux1,aux2,esVacio2);
  23.        arch.close();
  24.        aux1.close();
  25.        aux2.close();
  26.        aux1.open(argv[2],ios::in);
  27.        aux2.open(argv[3],ios::in);
  28.        arch.open(argv[1],ios::out);
  29.        Mezcla(arch,aux1,aux2);
  30.        aux1.close();
  31.        aux2.close();
  32.        arch.close();            
  33.     }
  34.     return 0;
  35. }
  36.  
  37. void LeerElemDetectandoFin(fstream &arch, string &comp, bool &finArch)
  38. {
  39.      finArch = arch.eof();
  40.      if(!finArch)
  41.        getline(arch,comp);          
  42. }
  43.  
  44. void PasarElemDetectandoFin(fstream &archOrigen,fstream &archDestino, string &comp, bool &finArchOrigen)
  45. {
  46.      archDestino<<comp;
  47.      LeerElemDetectandoFin(archOrigen,comp,finArchOrigen);
  48. }
  49.  
  50. void Division(fstream &arch,fstream &aux1,fstream &aux2,bool &esVacio2)
  51. {
  52.      string valorActual,valorAnterior;
  53.      bool cambio = true;
  54.      esVacio2 = true;
  55.      if(!arch.eof()){
  56.          getline(arch,valorActual);
  57.          aux1<<valorActual;
  58.          valorAnterior = valorActual;            
  59.      }
  60.      while(!arch.eof()){
  61.          getline(arch,valorActual);
  62.          if(valorAnterior > valorActual)
  63.             cambio = !cambio;
  64.          if(cambio)
  65.            aux1<<valorActual;
  66.          else{
  67.               aux2<<valorActual;
  68.               esVacio2 = false;
  69.          }
  70.          valorAnterior = valorActual;                                                  
  71.      }
  72. }
  73.  
  74. void Mezcla(fstream &aux1,fstream &aux2,fstream &arch)
  75. {
  76.      string c1,c2;
  77.      bool finArch1,finArch2;
  78.      LeerElemDetectandoFin(aux1,c1,finArch1);
  79.      LeerElemDetectandoFin(aux2,c2,finArch2);
  80.      while(!finArch1 && !finArch2)
  81.          if(c1 < c2)
  82.               PasarElemDetectandoFin(aux1,arch,c1,finArch1);
  83.          else
  84.               PasarElemDetectandoFin(aux2,arch,c2,finArch2);
  85.      while(!finArch1)
  86.          PasarElemDetectandoFin(aux1,arch,c1,finArch1);
  87.      while(!finArch2)
  88.          PasarElemDetectandoFin(aux2,arch,c2,finArch2);                                  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement