Thursday, March 15, 2012

Parking Charges

Minimum fee: $2.00 up to 3 hour
Additional fee: $0.50 per hour for each hour or part thereof over three hour
Maximum charge: $10.00 for 24 hours
Assumption: No car parked over 24 hours
/*
   title: parkingCharges
   author: aeriqusyairi
   date: Jan22 2012
   source: exercise 5.9 Chapter 5
*/
#include<stdio.h>

float calculateCharges(float);

int main(){
   int i, j;
   float c1 = 0, c2 = 0, c3 = 0,h1 = 0, h2 = 0, h3 = 0, hours = 0, charges = 0;
   
   printf("Enter the hours parked for each customer...\n");
   
   for(i = 1; i <= 3; i++){
      printf("Customer %d: ", i);

      switch(i){
         case 1: scanf("%f", &h1); 
                 c1 = calculateCharges(h1);
            break;
         case 2: scanf("%f", &h2); 
                 c2 = calculateCharges(h2);
            break;
         case 3: scanf("%f", &h3); 
                 c3 = calculateCharges(h3);
            break;  
      }     
   }   
   hours = h1 + h2 + h3;
   charges = c1 + c2 + c3;
    
   printf("%s%10s%10s\n", "Car", "Hours", "Charge");
   for(j = 0; j < 3; j++){
      switch(j){
         case 0: printf("%d.%10.1f%10.2f\n", j + 1, h1, c1); 
            break;
         case 1: printf("%d.%10.1f%10.2f\n", j + 2, h2, c2); 
            break;
         case 2: printf("%d.%10.1f%10.2f\n", j + 3, h3, c3); 
            break;           
      }      
   } 
   printf("%s%7.1f%10.2f\n", "TOTAL", hours, charges);
   
   system("pause");
   return 0;
}

float calculateCharges(float hour){
   float i = 0;
      
   if(hour <= 3)
      return 2;
   else if(hour > 3 && hour <= 24){
      i = ((hour - 3) * 0.5) + 2;
      if(i > 10)
         i = 10;
      return i;      
   }         
}

No comments:

Post a Comment