Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Dario
- * 4 Liceo
- * 15/11/2015
- * Compiti per il 20/11/2015
- */
- #include <iostream>
- using namespace std;
- /*
- * Avrei potuto rendere il programma pi� semplice (meno funzioni) per� ho cercato di renderlo
- * pi� "modulare" possibile in modo da ridurre al minimo la ripetizione del codice nel sorgente
- *
- * char(138) => �
- */
- int stampaIntro(){
- int n;
- cout << endl << "**********************" << endl;
- cout << "Inserire quanti numeri si desidera utilizzare per l'operazione scelta: ";
- cin >> n;
- return n;
- }
- float rawSomma(int n){
- float temp, somma;
- for(int k=1; k<n+1; k++){
- cout << "Inserire un numero (" << k << "/" << n << "): ";
- cin >> temp;
- somma += temp;
- }
- return somma;
- }
- float somma(){
- int n = stampaIntro();
- return rawSomma(n);
- }
- float media(){
- int n = stampaIntro();
- return rawSomma(n)/n;
- }
- float massimo(){
- int n = stampaIntro();
- float temp, massimo;
- for(int k=1; k<n+1; k++){
- cout << "Inserire un numero (" << k << "/" << n << "): ";
- cin >> temp;
- if(k==0){
- massimo = temp;
- }else{
- if(temp>massimo){
- massimo = temp;
- }
- }
- }
- return massimo;
- }
- float minimo(){
- int n = stampaIntro();
- float temp, minimo;
- for(int k=1; k<n+1; k++){
- cout << "Inserire un numero (" << k << "/" << n << "): ";
- cin >> temp;
- if(k==0){
- minimo = temp;
- }else{
- if(temp<minimo){
- minimo = temp;
- }
- }
- }
- return minimo;
- }
- int main(){
- int n;
- float risultato;
- cout << "Selezionare quale operazione si desidera effettuare: " << endl << endl;
- cout << " 1. Somma" << endl;
- cout << " 2. Media" << endl;
- cout << " 3. Massimo" << endl;
- cout << " 4. Minimo" << endl;
- cout << "Operazione scelta: ";
- cin >> n;
- switch(n){
- case 1: // Somma
- risultato = somma();
- break;
- case 2: // Media
- risultato = media();
- break;
- case 3: // Massimo
- risultato = massimo();
- break;
- case 4: // Minimo
- risultato = minimo();
- break;
- default: // altro
- cout << endl << "L'operazione scelta non " << char(138) << " valida.";
- return 0; // Interrompe il programma
- }
- cout << "**********************" << endl;
- cout << "Il risultato dell'operazione scelta " << char(138) << " " << risultato;;
- }
Advertisement
Add Comment
Please, Sign In to add comment