Last active
September 28, 2016 19:54
-
-
Save Steffo99/26b7f6993ebcd9c97b2a5d6b4dc4a0b3 to your computer and use it in GitHub Desktop.
Homework...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| #define CHIAMATE_MASSIME 100 | |
| #define NUMERI_MASSIMI 200 | |
| struct Numero | |
| { | |
| string cifre; | |
| int frequenza; | |
| }; | |
| struct Chiamata | |
| { | |
| Numero* ricevente; | |
| Numero* chiamante; | |
| int durata; | |
| }; | |
| //Trova il puntatore al numero con quelle cifre nell'elenco; se non lo trova, aggiungilo all'elenco | |
| Numero* trova_o_crea(Numero* elenco[], int &lunghezza, string ricerca) | |
| { | |
| for(int i = 0; i < lunghezza; i++) | |
| { | |
| if(elenco[i]->cifre == ricerca) | |
| { | |
| //Se lo trova, restituisci il puntatore e incrementa di 1 la sua frequenza | |
| elenco[i]->frequenza++; | |
| return elenco[i]; | |
| } | |
| } | |
| //Se non lo trova, aggiungilo all'elenco e restituisci il puntatore | |
| Numero* risultato = new Numero(); | |
| risultato->cifre = ricerca; | |
| risultato->frequenza = 1; | |
| elenco[lunghezza] = risultato; | |
| lunghezza++; | |
| return risultato; | |
| } | |
| //Ordina il registro delle chiamate in base alla durata | |
| void ordina(Chiamata registro[], int lunghezza) | |
| { | |
| //Selection sort; non è dei più efficienti ma pazienza, lo avevo già fatto in quell'altro | |
| for(int i = 0; i < lunghezza; i++) | |
| { | |
| //Trova la posizione del massimo | |
| int max_found = i; | |
| for(int j = i+1; j < lunghezza; j++) | |
| { | |
| if(registro[j].durata > registro[max_found].durata) | |
| { | |
| max_found = j; | |
| } | |
| } | |
| //Inverti il massimo con il numero attuale | |
| Chiamata temp = registro[i]; | |
| registro[i] = registro[max_found]; | |
| registro[max_found] = temp; | |
| } | |
| } | |
| //Ordina l'elenco telefonico in base alla frequenza delle chiamate | |
| void ordina(Numero* elenco[], int lunghezza) | |
| { | |
| //Selection sort; non è dei più efficienti ma pazienza, lo avevo già fatto in quell'altro | |
| for(int i = 0; i < lunghezza; i++) | |
| { | |
| //Trova la posizione del massimo | |
| int max_found = i; | |
| for(int j = i+1; j < lunghezza; j++) | |
| { | |
| if(elenco[j]->frequenza > elenco[max_found]->frequenza) | |
| { | |
| max_found = j; | |
| } | |
| } | |
| //Inverti il massimo con il numero attuale | |
| Numero* temp = elenco[i]; | |
| elenco[i] = elenco[max_found]; | |
| elenco[max_found] = temp; | |
| } | |
| } | |
| int main() | |
| { | |
| Numero* elenco[NUMERI_MASSIMI]; //Elenco di tutti i numeri telefonici esistenti | |
| int lunghezza_elenco; | |
| Chiamata registro[CHIAMATE_MASSIME]; //Registro di tutte le chiamate effettuate | |
| int lunghezza_registro; | |
| //Menu | |
| while(true) | |
| { | |
| //Opzione selezionata | |
| char selezione; | |
| //Input nel caso venga inserita una nuova chiamata | |
| string chiamante; | |
| string ricevente; | |
| int durata; | |
| //Menu | |
| cout << "Azioni disponibili: \n"; | |
| cout << "(a)ggiungi una nuova chiamata\n"; | |
| if(lunghezza_registro >= 3) | |
| { | |
| cout << "visualizza le 3 chiamate più (l)unghe\n"; | |
| } | |
| if(lunghezza_elenco >= 3) | |
| { | |
| cout << "visualizza i 3 numeri più (f)requenti\n"; | |
| } | |
| cout << "(e)sci\n"; | |
| cin >> selezione; | |
| switch(selezione) | |
| { | |
| case 'a': | |
| //Input utente | |
| cout << "Inserimento chiamata #" << lunghezza_registro << "\n"; | |
| cout << "Inserisci il numero chiamante: "; | |
| cin >> chiamante; | |
| cout << "Inserisci il numero ricevente: "; | |
| cin >> ricevente; | |
| cout << "Durata chiamata: "; | |
| cin >> durata; | |
| //Elaborazione | |
| registro[lunghezza_registro].chiamante = trova_o_crea(elenco, lunghezza_elenco, chiamante); | |
| registro[lunghezza_registro].ricevente = trova_o_crea(elenco, lunghezza_elenco, ricevente); | |
| registro[lunghezza_registro].durata = durata; | |
| lunghezza_registro++; | |
| break; | |
| case 'l': | |
| cout << "Elenco chiamate più lunghe:\n"; | |
| cout << registro[0].chiamante->cifre << " -> " << registro[0].ricevente->cifre << " | " << registro[0].durata << "\n"; | |
| cout << registro[1].chiamante->cifre << " -> " << registro[1].ricevente->cifre << " | " << registro[1].durata << "\n"; | |
| cout << registro[2].chiamante->cifre << " -> " << registro[2].ricevente->cifre << " | " << registro[2].durata << "\n"; | |
| break; | |
| case 'f': | |
| cout << "Elenco numeri più frequenti:\n"; | |
| cout << elenco[0]->frequenza << " | " << elenco[0]->cifre << "\n"; | |
| cout << elenco[1]->frequenza << " | " << elenco[1]->cifre << "\n"; | |
| cout << elenco[2]->frequenza << " | " << elenco[2]->cifre << "\n"; | |
| break; | |
| case 'e': | |
| return 0; | |
| default: | |
| cout << "Input non valido.\n"; | |
| } | |
| //Ordinamento automatico dei numeri e delle chiamate | |
| ordina(elenco, lunghezza_elenco); | |
| ordina(registro, lunghezza_registro); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment