Advertisement
howarto

7.1.4 LABO

Apr 29th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. /* estadisticas.cc */
  2. // #include <howarto>
  3. #include "utils.PRO2"
  4. #include <list>
  5. #include "listIOPar.hh"
  6.  
  7. int main() {
  8.     list<Par> lista;
  9.     llegir_llista_i_escriure_estadistica(lista);
  10. }
  11.  
  12. /* listIOPar.hh */
  13. #ifndef LISTIOPAR_HH
  14. #define LISTIOPAR_HH
  15.  
  16. #include <list>
  17. #include "utils.PRO2"
  18.  
  19. struct Par {
  20.     int x;
  21.     int y;
  22. };
  23.  
  24. void  llegir_llista_i_escriure_estadistica(list<Par>& L){
  25.     list<Par>::iterator it = L.begin();
  26.     int x = readint();
  27.     int numeros_relevantes = 0;
  28.     double suma_numeros_relevantes = 0;
  29.     bool primera_vez = true;
  30.     int minimo, maximo;
  31.     while (x == -1 or x == -2) {
  32.         Par par;
  33.         par.x = x;
  34.         par.y = readint();
  35.         // 2 casos: En x == -1 se introducirá en las estadísticas al segundo elemento del par
  36.         // mientras que en x == -2 se sacará un elemento de la lista que tenga el mismo segundo elemento (en caso de haberlo)
  37.         if (x == -1) {
  38.             if (primera_vez or L.empty()) {
  39.                 minimo = par.y;
  40.                 maximo = par.y;
  41.                 primera_vez = false;
  42.             }
  43.             if (par.y < minimo) minimo = par.y;
  44.             if (par.y > maximo) maximo = par.y;
  45.             L.insert(it, par);
  46.             suma_numeros_relevantes += par.y;
  47.             ++numeros_relevantes;
  48.             cout << "min: " << minimo << "; " << "max: " << maximo << "; "
  49.             << "media: " << suma_numeros_relevantes/numeros_relevantes << endl;
  50.         }
  51.         else {  // Queremos quitar un elemento.
  52.             list<Par>::iterator it2 = L.begin();
  53.             if (par.y == minimo or par.y == maximo) {
  54.                 if (par.y == minimo) minimo = maximo;
  55.                 else if (par.y == maximo) maximo = minimo;
  56.                 while (it2 != L.end() and not L.empty()) {
  57.                     if ((*it2).y != par.y and (*it2).y > maximo) maximo = (*it2).y;
  58.                     if ((*it2).y != par.y and (*it2).y < minimo) minimo = (*it2).y;
  59.                     if ((*it2).y == par.y) {
  60.                         it2 = L.erase(it2);
  61.                         suma_numeros_relevantes -= par.y;
  62.                         --numeros_relevantes;
  63.                     }
  64.                     else ++it2;
  65.                 }
  66.             }
  67.             else {
  68.                 bool encontrado = false;
  69.                 while (not encontrado and it2 != L.end() and not L.empty()) {
  70.                     if ((*it2).y == par.y) {
  71.                         it2 = L.erase(it2);
  72.                         suma_numeros_relevantes -= par.y;
  73.                         --numeros_relevantes;
  74.                     }
  75.                     else ++it2;
  76.                 }
  77.             }
  78.             if (not L.empty()) {
  79.                 cout << "min: " << minimo << "; " << "max: " << maximo << "; "
  80.                      << "media: " << suma_numeros_relevantes/numeros_relevantes << endl;
  81.             }
  82.             else cout << 0 << endl;
  83.         }
  84.         x = readint();
  85.     }
  86. }
  87.  
  88. void escriure_llista_int(const list<Par> &L) {
  89.   if (not L.empty()) {
  90.     list<Par>::const_iterator it;
  91.     cout << "[Primero] ";
  92.     for (it = L.begin(); it != L.end(); ++it){
  93.       cout << (*it).x << ' ' << (*it).y << endl; // si la lista no es de enteros
  94.     }                     // quedará mejor si hacemos endl para cada elemento
  95.     cout << "[último]";
  96.   }
  97.   cout << endl;
  98. }
  99.  
  100. #endif // LISTIOPAR_HH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement