Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* estadisticas.cc */
- // #include <howarto>
- #include "utils.PRO2"
- #include <list>
- #include "listIOPar.hh"
- int main() {
- list<Par> lista;
- llegir_llista_i_escriure_estadistica(lista);
- }
- /* listIOPar.hh */
- #ifndef LISTIOPAR_HH
- #define LISTIOPAR_HH
- #include <list>
- #include "utils.PRO2"
- struct Par {
- int x;
- int y;
- };
- void llegir_llista_i_escriure_estadistica(list<Par>& L){
- list<Par>::iterator it = L.begin();
- int x = readint();
- int numeros_relevantes = 0;
- double suma_numeros_relevantes = 0;
- bool primera_vez = true;
- int minimo, maximo;
- while (x == -1 or x == -2) {
- Par par;
- par.x = x;
- par.y = readint();
- // 2 casos: En x == -1 se introducirá en las estadísticas al segundo elemento del par
- // mientras que en x == -2 se sacará un elemento de la lista que tenga el mismo segundo elemento (en caso de haberlo)
- if (x == -1) {
- if (primera_vez or L.empty()) {
- minimo = par.y;
- maximo = par.y;
- primera_vez = false;
- }
- if (par.y < minimo) minimo = par.y;
- if (par.y > maximo) maximo = par.y;
- L.insert(it, par);
- suma_numeros_relevantes += par.y;
- ++numeros_relevantes;
- cout << "min: " << minimo << "; " << "max: " << maximo << "; "
- << "media: " << suma_numeros_relevantes/numeros_relevantes << endl;
- }
- else { // Queremos quitar un elemento.
- list<Par>::iterator it2 = L.begin();
- if (par.y == minimo or par.y == maximo) {
- if (par.y == minimo) minimo = maximo;
- else if (par.y == maximo) maximo = minimo;
- while (it2 != L.end() and not L.empty()) {
- if ((*it2).y != par.y and (*it2).y > maximo) maximo = (*it2).y;
- if ((*it2).y != par.y and (*it2).y < minimo) minimo = (*it2).y;
- if ((*it2).y == par.y) {
- it2 = L.erase(it2);
- suma_numeros_relevantes -= par.y;
- --numeros_relevantes;
- }
- else ++it2;
- }
- }
- else {
- bool encontrado = false;
- while (not encontrado and it2 != L.end() and not L.empty()) {
- if ((*it2).y == par.y) {
- it2 = L.erase(it2);
- suma_numeros_relevantes -= par.y;
- --numeros_relevantes;
- }
- else ++it2;
- }
- }
- if (not L.empty()) {
- cout << "min: " << minimo << "; " << "max: " << maximo << "; "
- << "media: " << suma_numeros_relevantes/numeros_relevantes << endl;
- }
- else cout << 0 << endl;
- }
- x = readint();
- }
- }
- void escriure_llista_int(const list<Par> &L) {
- if (not L.empty()) {
- list<Par>::const_iterator it;
- cout << "[Primero] ";
- for (it = L.begin(); it != L.end(); ++it){
- cout << (*it).x << ' ' << (*it).y << endl; // si la lista no es de enteros
- } // quedará mejor si hacemos endl para cada elemento
- cout << "[último]";
- }
- cout << endl;
- }
- #endif // LISTIOPAR_HH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement