3DDario

Compiti Versione Impossibile Informatica 20/11/15

Nov 19th, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. /*
  2.  * Dario
  3.  * 4 Liceo
  4.  * 15/11/2015
  5.  * Compiti per il 20/11/2015
  6.  */
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. /*
  11.  * Avrei potuto rendere il programma pi� semplice (meno funzioni) per� ho cercato di renderlo
  12.  * pi� "modulare" possibile in modo da ridurre al minimo la ripetizione del codice nel sorgente
  13.  *
  14.  * char(138) => �
  15.  */
  16.  
  17. int stampaIntro(){
  18.     int n;
  19.     cout << endl << "**********************" << endl;
  20.     cout << "Inserire quanti numeri si desidera utilizzare per l'operazione scelta: ";
  21.     cin >> n;
  22.    
  23.     return n;
  24. }
  25.  
  26. float rawSomma(int n){
  27.     float temp, somma;
  28.    
  29.     for(int k=1; k<n+1; k++){
  30.         cout << "Inserire un numero (" << k << "/" << n << "): ";
  31.         cin >> temp;
  32.         somma += temp;
  33.     }
  34.     return somma;
  35. }
  36.  
  37. float somma(){
  38.    
  39.     int n = stampaIntro(); 
  40.     return rawSomma(n);
  41. }
  42.  
  43. float media(){
  44.     int n = stampaIntro();
  45.    
  46.     return rawSomma(n)/n;
  47. }
  48.  
  49. float massimo(){
  50.     int n = stampaIntro();
  51.     float temp, massimo;
  52.     for(int k=1; k<n+1; k++){
  53.         cout << "Inserire un numero (" << k << "/" << n << "): ";
  54.         cin >> temp;
  55.         if(k==0){
  56.             massimo = temp;
  57.         }else{
  58.             if(temp>massimo){
  59.                 massimo = temp;
  60.             }
  61.         }  
  62.     }
  63.    
  64.     return massimo;
  65. }
  66.  
  67. float minimo(){
  68.     int n = stampaIntro();
  69.     float temp, minimo;
  70.     for(int k=1; k<n+1; k++){
  71.         cout << "Inserire un numero (" << k << "/" << n << "): ";
  72.         cin >> temp;
  73.         if(k==0){
  74.             minimo = temp;
  75.         }else{
  76.             if(temp<minimo){
  77.                 minimo = temp;
  78.             }
  79.         }  
  80.     }
  81.    
  82.     return minimo;
  83. }
  84.  
  85. int main(){
  86.     int n;
  87.     float risultato;
  88.     cout << "Selezionare quale operazione si desidera effettuare: " << endl << endl;
  89.     cout << " 1. Somma" << endl;
  90.     cout << " 2. Media" << endl;
  91.     cout << " 3. Massimo" << endl;
  92.     cout << " 4. Minimo" << endl;
  93.     cout << "Operazione scelta: ";
  94.     cin >> n;
  95.     switch(n){
  96.         case 1: // Somma
  97.             risultato = somma();
  98.             break;
  99.         case 2: // Media
  100.             risultato = media();
  101.             break;
  102.         case 3: // Massimo
  103.             risultato = massimo();
  104.             break;
  105.         case 4: // Minimo
  106.             risultato = minimo();
  107.             break;
  108.         default: // altro
  109.             cout << endl << "L'operazione scelta non " << char(138) << " valida.";
  110.             return 0; // Interrompe il programma
  111.     }
  112.    
  113.     cout << "**********************" << endl;
  114.     cout << "Il risultato dell'operazione scelta " << char(138) << " " << risultato;;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment