Accedi

Temperature Campania

PROBLEMA

Scrivere un programma che lette da in-put le temperature inerenti alle 5 città della regione Campania restituisce a video:

  1. La temperatura della città solo se è minore di 0 oppure maggiore di 25.
  2. La media delle temperature.
  3. Si utilizzi per la stesura del programma il costrutto enum

CODICE SORGENTE


#include <stdio.h>    //library
#include <stdlib.h>    //library


int main(int argc, char *argv[]) {    //start body
    enum citta    //Enum of city
    {
        napoli=1,    //fist name
        avellino,    //second name
        benevento,    //third name    
        salerno,    //fourth name
        caserta    //fifth name
    };
    int menu_1, menu_2, menu_3, a=0, b=0, c=0, d=0, e=0;    //variables declaration
    float np=0, av=0, bn=0, sa=0, ce=0;    //variables declaration
    float media;    //variables declaration
    
    do {    //if the choise isn't in the range
        system("cls");    //clear all previous text
        printf("1. Campania\n");    //text on video
        printf("2. Conferma\n");    //text on video
        printf("3. Esci\n");    //text on video
        printf("Inserire una preferenza: ");    //text on video
        scanf("%d", &menu_1);    //input of first menu
        switch (menu_1)    //switch first menu
        {
            case 1:    //case 1 first menu menu
                do{    //if the case is come back
                    system("cls");    //clear all previous text
                    printf("1. Napoli");    //text on a video
                    if (a==1)    //if the temeprature of Napoli has already been entered
                    {
                        printf("\t[%.2f C]\n", np);    //text on video
                    }
                    else    //if the temperautre of Napoli has not already been entered
                    {
                        printf("\t[Da inserire]\n");    //text on video    
                    }
                    printf("2. Avellino");    //text on video
                    if (b==1)    //if the temeprature of Napoli has already been entered
                    {
                        printf("\t[%.2f C]\n", av);    //text on video    
                    }
                    else    //if the temperautre of Napoli has not already been entered
                    {
                        printf("\t[Da inserire]\n");    //text on video    
                    }
                    printf("3. Benevento");    //text on video
                    if (c==1)    //if the temeprature of Napoli has already been entered
                    {
                        printf("\t[%.2f C]\n", bn);    //text on video    
                    }
                    else    //if the temperautre of Napoli has not already been entered
                    {
                        printf("\t[Da inserire]\n");    //text on video    
                    }
                    printf("4. Salerno");    //text on video
                    if (d==1)    //if the temeprature of Napoli has already been entered
                    {
                        printf("\t[%.2f C]\n", sa);    //text on video    
                    }
                    else    //if the temperautre of Napoli has not already been entered
                    {
                        printf("\t[Da inserire]\n");    //text on video    
                    }
                    printf("5. Caserta");    //text on video
                    if (e==1)    //if the temeprature of Napoli has already been entered
                    {
                        printf("\t[%.2f C]\n", ce);    //text on video    
                    }
                    else    //if the temperautre of Napoli has not already been entered
                    {
                        printf("\t[Da inserire]\n");    //text on video    
                    }
                    printf("6. Indietro\n");    //text on video
                    printf("Inserire una preferenza: ");    //text on video
                    scanf("%d", &menu_2);    //input value
                    switch (menu_2)    //switch second menu
                    {
                        case napoli:    //case 1 second menu
                            if(a==1)    //if the value is already existing
                            {
                                do{    //if the choise isn't in the range
                                    system("cls");    //clear all the previous text
                                    printf("[!]Vuoi davvero reinserire la temperatura?\n");    //text on video
                                    printf("1. yes\n");    //text on video
                                    printf("2. no\n");    //text on video
                                    scanf("%d", &menu_3);    //input value
                                }while(menu_3!=1 && menu_3!=2);    //if the choise isn't in the range
                                switch (menu_3)    //switch third menu
                                {
                                    case 1:    //case 1 third menu
                                        system("cls");    //clear all the previous text
                                        printf("Citta' Napoli\n");    //text on video
                                        printf("Inserire temperatura:");    //text on video
                                        scanf("%f", &np);    //input value
                                        a=1;
                                        break;    //out from case
                                    case 2:    //case 2 third menu
                                        printf("TI POTEVI RISPARMIARE IL CLICK.");    //text on video
                                        break;    //out from case
                                    default:    //case default third menu
                                        break;    //out from case
                                }
                            }
                            else    //if the value is not already existing
                            {
                                system("cls");    //clear all the previous text
                                printf("Citta' Napoli\n");    //text on video
                                printf("Inserire temperatura: ");    //text on video
                                scanf("%f", &np);    //input value
                                a=1;
                            }
                            break;    //out from case
                        case avellino:    //case 2 second menu
                            if(b==1)    //if the value is already existing
                            {
                                do{    //if the choise isn't in the range
                                    system("cls");    //clear all the previous text
                                    printf("[!]Vuoi davvero reinserire la temperatura?\n");    //text on video
                                    printf("1. yes\n");    //text on video
                                    printf("2. no\n");    //text on video
                                    scanf("%d", &menu_3);    //input value
                                }while(menu_3!=1 && menu_3!=2);    //if the choise isn't in the range            
                                switch (menu_3)    //switch third menu
                                {
                                    case 1:    //case 1 third menu
                                        system("cls");    //clear all the previous text
                                        printf("Citta' Avellino\n");    //text on video
                                        printf("Inserire temperatura:");    //text on video
                                        scanf("%f", &av);    //input value
                                        b=1;
                                        break;    //out from case 
                                    case 2:    //case 2 third menu
                                        printf("TI POTEVI RISPARMIARE IL CLICK.");    //text on video
                                        break;    //out from case
                                    default:    //case default third menu
                                        break;    //out from case
                                }    
                            }
                            else    //if the value is not already existing
                            {
                                system("cls");    //clear all the previous text
                                printf("Citta' Avellino\n");    //text on video
                                printf("Inserire temperatura: ");    //text on video
                                scanf("%f", &av);    //input value
                                b=1;
                            }
                            break;
                        case benevento:    //case 3 second menu
                            if(c==1)    //if the value is already existing
                            {
                                do{    //if the choise isn't in the range
                                    system("cls");    //clear all the previous text
                                    printf("[!]Vuoi davvero reinserire la temperatura?\n");    //text on video
                                    printf("1. yes\n");    //text on video
                                    printf("2. no\n");    //text on video
                                    scanf("%d", &menu_3);    //input value
                                }while(menu_3!=1 && menu_3!=2);    //if the choise isn't in the range            
                                switch (menu_3)    //switch third menu
                                {
                                    case 1:    //case 1 third menu
                                        system("cls");    //clear all the previous text
                                        printf("Citta' Benevento\n");    //text on video
                                        printf("Inserire temperatura:");    //text on video
                                        scanf("%f", &bn);    //input value
                                        c=1;
                                        break;    //out from case
                                    case 2:    //case 2 third menu
                                        printf("TI POTEVI RISPARMIARE IL CLICK.");    //text on video
                                        break;    //out from case
                                    default:    //case default third menu
                                        break;    //out from case
                                }
                            }
                            else    //if the value is not already existing
                            {
                                system("cls");    //clear all the previous text
                                printf("Citta' Benevento\n");    //text on video
                                printf("Inserire temperatura: ");    //text on video
                                scanf("%f", &bn);    //input value
                                c=1;
                            }
                            break;
                        case salerno:    //case 4 second menu
                            if(d==1)    //if the value is already existing
                            {
                                do{    //if the choise isn't in the range
                                    system("cls");    //clear all the previous text
                                    printf("[!]Vuoi davvero reinserire la temperatura?\n");    //text on video
                                    printf("1. yes\n");    //text on video
                                    printf("2. no\n");    //text on video
                                    scanf("%d", &menu_3);    //input value
                                }while(menu_3!=1 && menu_3!=2);    //if the choise isn't in the range            
                                switch (menu_3)    //switch third menu
                                {
                                    case 1:    //case 1 third menu
                                        system("cls");    //clear all the previous text
                                        printf("Citta' Salerno\n");    //text on video
                                        printf("Inserire temperatura:");    //text on video
                                        scanf("%f", &sa);    //input value
                                        d=1;
                                        break;    //out from case
                                    case 2:    //case 2 third menu
                                        printf("TI POTEVI RISPARMIARE IL CLICK.");    //text on video
                                        break;    //out from case
                                    default:    //case default third menu
                                        break;    //out from case
                                }
                            }
                            else    //if the value is not already existing
                            {
                                system("cls");    //clear all the previous text
                                printf("Citta' Salerno\n");    //text on video
                                printf("Inserire temperatura: ");    //text on video
                                scanf("%f", &sa);    //input value
                                d=1;
                            }
                            break;    //out from case
                        case caserta:    //case 5 second menu
                            if(e==1)    //if the value is already existing
                            {
                                do{    //if the choise isn't in the range
                                    system("cls");    //clear all the previous text
                                    printf("[!]Vuoi davvero reinserire la temperatura?\n");    //text on video
                                    printf("1. yes\n");    //text on video
                                    printf("2. no\n");    //text on video
                                    scanf("%d", &menu_3);    //input value
                                }while(menu_3!=1 && menu_3!=2);    //if the choise isn't in the range            
                                switch (menu_3)    //switch third menu
                                {
                                    case 1:    //case 1 third menu
                                        system("cls");    //clear all the previous text
                                        printf("Citta' Caserta\n");    //text on video
                                        printf("Inserire temperatura:");    //text on video
                                        scanf("%f", &ce);    //input value
                                        e=1;
                                        break;    //out from case
                                    case 2:    //case 2 third menu
                                        printf("TI POTEVI RISPARMIARE IL CLICK.");    //text on video
                                        break;    //out from case
                                    default:    //case default third menu
                                        break;    //out from case
                                }
                            }
                            else    //if the value is not already existing
                            {
                                system("cls");    //clear all the previous text
                                printf("Citta' Caserta\n");    //text on video
                                printf("Inserire temperatura: ");    //text on video
                                scanf("%f", &ce);    //input value
                                e=1;
                            }
                            break;    //out from case
                        case 6:    //case 6 second menu
                            break;    //out from case
                        default:    //case default
                            system("cls");    //clear all the previous text
                            printf("Valore non valido");    //text on video
                            break;    //out from case
                    }
            }while(menu_2!=6);    //if the case is come back    
                break;    //out from case
            case 2:    //case 2 first menu menu
                system("cls");                                                                                                
                if (np<0 || np>25)    //value included                                                                                            
                {
                    printf("Napoli: \t%.2f", np);    //text on video                                                                            
                }
                if (av<0 || av>25)    //value included                                                                                    
                {
                    printf("\nAvellino: \t%.2f", av);    //text on video                                                                        
                }
                if (bn<0 || bn>25)    //value included                                                                                            
                {
                    printf("\nBenevento: \t%.2f", bn);    //text on video                                                                        
                }
                if (sa<0 || sa>25)    //value included                                                                                            
                {
                    printf("\nSalerno: \t%.2f", sa);    //text on video                                                                        
                }
                if (ce<0 || ce>25)    //value included                                                                                            
                {
                    printf("\nCaserta: \t%.2f", ce);    //text on video                                                                        
                }
                media=(np+av+bn+sa+ce)/5; //value calculation
                printf("\nLa media e': \t%.2f\n", media);    //text on video                                                                            
                break;    //out from case
            case 3:    //case 3 first menu menu
                exit(EXIT_SUCCESS);    //close program
                break;    //out from case
            default:    //case default first menu menu
                system("cls");    //clrea all the previous text
                printf("Valore non valido");    //text on video
                break;    //out from case
        }
    }while(menu_1!=2);    //if the choise isn't in the range
    system("PAUSE");    //pause    
    return 0;    //all OK!
}    // end body