Advertisement
Wander29

Untitled

Oct 10th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define EUR 1
  5. #define USD 1.15132
  6. #define JPY 130.190
  7. #define GBP 0.874162
  8.  
  9. typedef enum{  
  10.                 REPEAT,
  11.                 EXIT
  12.             }state;
  13.  
  14. void converti(float imp_in, float start_val, int end_val);
  15.  
  16. int main(){
  17.     state stato = REPEAT;
  18.     int scelta, val_end;
  19.     float prezzo;
  20.    
  21.     do
  22.     {
  23.         printf("### Convertitore Valute ###\nSceglia la valuta di partenza:\n1)€ (Euro)\n2)$ (Dollari)\n3)₤ (Sterlina)\n4)¥ (JPY)\n -> ");
  24.         while ( scanf("%d", &scelta) < 1  || (scelta < 0) || (scelta > 4) )
  25.         {
  26.             printf("ERROR: scelta non valida (capra)\n");
  27.             while (getchar() != '\n');
  28.         }
  29.  
  30.         printf("\nImporto: ");
  31.         while ( scanf("%f", &prezzo) < 1  || prezzo < 0 )
  32.         {
  33.             printf("ERROR: scelta non valida (capra)\n");
  34.             while (getchar() != '\n');
  35.         }
  36.  
  37.         printf("\nValuta in cui convertire l'importo?\n1)€ (Euro)\n2)$ (Dollari)\n3)₤ (Sterlina)\n4)¥ (JPY)\n -> ");
  38.         while ( scanf("%d", &val_end) < 1  || (val_end < 0) || (val_end > 4) )
  39.         {
  40.             printf("ERROR: scelta non valida (capra)\n");
  41.             while (getchar() != '\n');
  42.         }
  43.  
  44.         printf("%20s %20s", "Importo Iniziale", "Importo convertito\n");
  45.         switch(scelta)
  46.         {
  47.             case 1: //Euro
  48.                 converti(prezzo, EUR, val_end);
  49.                 break;
  50.             case 2: //Dollari
  51.                 converti(prezzo, USD, val_end);
  52.                 break;
  53.             case 3: //Sterline
  54.                 converti(prezzo, GBP, val_end);
  55.                 break;
  56.             case 4: //JPY
  57.                 converti(prezzo, JPY, val_end);
  58.                 break;
  59.         }
  60.         printf("\nVuoi effettuare un'altra conversione?\t< 1 > Yes\t< 14 > No\n");
  61.         scelta = 0;
  62.  
  63.         while ( scanf("%d", &scelta) < 1  || (scelta != 1 && scelta != 14) )
  64.         {
  65.             printf("ERROR: scelta non valida (capra)\n");
  66.             while (getchar() != '\n');
  67.         }
  68.         if (scelta == 14)
  69.             stato = EXIT;
  70.  
  71.         printf("\n___________________________________________________\n\n");
  72.  
  73.     } while (stato == REPEAT);
  74.  
  75.     return 0;
  76. }
  77.  
  78. void converti(float imp_in, float start_val, int end_val)
  79. {
  80.     float conv_val;
  81.  
  82.     switch(end_val)
  83.     {
  84.         case 1:
  85.             conv_val = EUR;
  86.             break;
  87.         case 2:
  88.             conv_val = USD;
  89.             break;
  90.         case 3:
  91.             conv_val = GBP;
  92.             break; 
  93.         case 4:
  94.             conv_val = JPY;
  95.             break;
  96.         default:
  97.             break;
  98.     }
  99.  
  100.     printf("%19.2f%19.2f", imp_in, round((imp_in / start_val * conv_val)*100)/100 );
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement