getcharge.c (1642B)
1 /* Esercizio 5.9 */ 2 3 #include <stdio.h> 4 5 float calculateCharges(float); 6 7 int main() 8 { 9 int i; 10 float t_hours = 0.0, t_charge = 0.0; 11 float c1_hours, c2_hours, c3_hours; 12 float c_charge; /* for all clients */ 13 14 for(i = 1; i <= 3; i++) { 15 printf("Client %d, get hours: ", i); 16 switch(i) { 17 case 1: 18 scanf("%f", &c1_hours); 19 break; 20 case 2: 21 scanf("%f", &c2_hours); 22 break; 23 case 3: 24 scanf("%f", &c3_hours); 25 break; 26 } /* end switch (i) */ 27 } /* end for (i) */ 28 29 printf("\nCar\tHours\tCharge\n"); 30 for(i = 1; i <= 3; i++) { 31 switch(i) { 32 case 1: 33 c_charge = calculateCharges(c1_hours); 34 printf("%d\t%.1f\t%.2f\n", i, c1_hours, c_charge); 35 t_charge += c_charge; 36 t_hours += c1_hours; 37 break; 38 case 2: 39 c_charge = calculateCharges(c2_hours); 40 printf("%d\t%.1f\t%.2f\n", i, c2_hours, c_charge); 41 t_charge += c_charge; 42 t_hours += c2_hours; 43 break; 44 case 3: 45 c_charge = calculateCharges(c3_hours); 46 printf("%d\t%.1f\t%.2f\n", i, c3_hours, c_charge); 47 t_charge += c_charge; 48 t_hours += c3_hours; 49 } /* end switch (i) */ 50 } /* end for (i) */ 51 52 printf("TOTAL\t%.1f\t%.2f\n", t_hours, t_charge); 53 54 return 0; 55 } /* E0F main */ 56 57 /* calcultate client charge */ 58 float calculateCharges(float x) 59 { 60 /* x = hours 61 t = charge */ 62 63 float t = 1.50; 64 int i; 65 66 if (x > 3.0) { 67 for(i = 3; i <= x * 100.0 / 100.0; i++) { 68 t += 0.50; 69 } 70 if(x - (i - 1)) 71 t += 0.50; 72 } 73 else 74 t += 0.50; 75 76 if( t > 10.0) 77 t = 10.0; 78 79 return t; 80 81 } /* E0F calculateChages */ 82