mbah_bejo

Water BIll

Oct 25th, 2020
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1.   #include <stdio.h>
  2.  
  3.   #define DEMAND_CHG  35.00    /* basic water demand charge                     */
  4.   #define PER_1000_CHG 1.10    /* charge per thousand gallons used              */
  5.   #define LATE_CHG     2.00   /* surcharge assessed on unpaid balance          */
  6.   /* Function prototypes                                                       */
  7.   void instruct_water(void);
  8.   double comp_use_charge(int previous, int current);
  9.   double comp_late_charge(double unpaid);
  10.   void display_bill(double late_charge, double bill, double unpaid);
  11.   double getdataunpaid(void);
  12.   int getdataprevious(void);
  13.   int getdatacurrent(void);
  14.   double figurebill(double,double,double);
  15.  
  16.   int
  17.   main(void)
  18.   {
  19.           int    previous;    /* input - meter reading from previous quarter
  20.                                  in thousands of gallons                    */
  21.           int    current;     /* input - meter reading from current quarter */
  22.           double unpaid;      /* input - unpaid balance of previous bill    */
  23.           double bill;        /* output - water bill                        */
  24.           int    used;        /* thousands of gallons used this quarter     */
  25.           double use_charge;  /* charge for actual water use                */
  26.           double late_charge; /* charge for nonpayment of part of previous
  27.                                  balance                                    */
  28.           /* Display user instructions.                                     */
  29.           instruct_water();
  30.           /* Get data: unpaid balance, previous and current meter
  31.              readings.*/
  32.             unpaid= getdataunpaid();
  33.             previous=getdataprevious();
  34.             current=getdatacurrent();
  35.           /* Compute use charge.                                            */
  36.           use_charge = comp_use_charge(previous, current);
  37.           /* Determine applicable late charge                               */
  38.           late_charge = comp_late_charge(unpaid);
  39.           /* Figure bill.                                                   */
  40.           bill = figurebill(use_charge,unpaid,late_charge);
  41.           /* Print bill.                                                    */
  42.           display_bill(late_charge, bill, unpaid);
  43.           return (0);
  44.   }
  45.   /*
  46.    * Displays user instructions
  47.    */
  48.   void
  49.   instruct_water(void)
  50.   {
  51.         printf("This program figures a water bill ");
  52.         printf("based on the demand charge\n");
  53.         printf("($%.2f) and a $%.2f per 1000 ", DEMAND_CHG, PER_1000_CHG);
  54.         printf("gallons use charge.\n\n");
  55.         printf("A $%.2f surcharge is added to ", LATE_CHG);
  56.         printf("accounts with an unpaid balance.\n");
  57.         printf("\nEnter unpaid balance, previous ");
  58.         printf("and current meter readings\n");
  59.         printf("on separate lines after the prompts.\n");
  60.         printf("Press <return> or <enter> after ");
  61.         printf("typing each number.\n\n");
  62.   }
  63.   /*
  64.    * Computes use charge
  65.    * Pre: previous and current are defined.
  66.    */
  67.   double
  68.   comp_use_charge(int previous, int current)
  69.   {
  70.         int used;          /* gallons of water used (in thousands)             */
  71.         double use_charge; /* charge for actual water use                      */
  72.         used = current - previous;
  73.         use_charge = used * PER_1000_CHG;
  74.         return (use_charge);
  75.   }
  76.  
  77.   /*
  78.    * Computes late charge.
  79.    * Pre : unpaid is defined.
  80.    */
  81.   double
  82.   comp_late_charge(double unpaid)
  83.   {
  84.         double late_charge; /* charge for nonpayment of part of previous balance  */
  85.         if (unpaid > 0)
  86.               late_charge = LATE_CHG; /* Assess late charge on unpaid balance.    */
  87.         else
  88.               late_charge = 0.0;
  89.         return (late_charge);
  90.   }
  91.   /*
  92.    * Displays late charge if any and bill.
  93.    * Pre : late_charge, bill, and unpaid are defined.
  94.    */
  95.   void
  96.   display_bill(double late_charge, double bill, double unpaid)
  97.      {
  98.          if (late_charge > 0.0) {
  99.                printf("\nBill includes $%.2f late charge", late_charge);
  100.                printf(" on unpaid balance of $%.2f\n", unpaid);
  101.          }
  102.          printf("\nTotal due = $%.2f\n", bill);
  103.   }  
  104.   double
  105.   getdataunpaid(){
  106.     double unpaid,previous,current;
  107.      printf("Enter unpaid balance> $");
  108.           scanf("%lf", &unpaid);
  109.      
  110.           return(unpaid);
  111.   }
  112.   int
  113.   getdataprevious(){
  114.     int previous;
  115.      
  116.           printf("Enter previous meter reading> ");
  117.           scanf("%d", &previous);
  118.           return(previous);
  119.   }
  120.   int
  121.   getdatacurrent(){
  122.     int current;
  123.       printf("Enter current meter reading> ");
  124.           scanf("%d", &current);
  125.           return(current);
  126.   }
  127.   double figurebill(double X,double Y,double Z){
  128.     return (DEMAND_CHG + X + Y + Z);
  129.   }
Advertisement
Add Comment
Please, Sign In to add comment