In: Computer Science
A parking garage charges R7.50 minimum fee to park for
up to three and half
hours. The garage charges an additional R1.50 per hour for each
hour or part
thereof in excess of three hours. The maximum charge for any given
24- hour
period is R25.72. Write a program that calculates and prints the
parking
charges for each of three customers who parked their cars in this
garage at
some time. You should enter the hours parked for each customer.
Your
program should print the results in a neat tabular format and
should calculate
and print the total of receipts. The program should use the
function
calculateCharges to determine the charge for each customer. The
payment
amounts should be printed inside the body of main().
SOLUTION -:
C++ Code for the above problem is as given in screenshots below -:
C++ Code -:
In this code, we take charge of the R7.5 minimum fee to park up to three and a half hours.
Output -:
output_1-:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double calculateCharges (double hours)
{
double result;
// Minimum Charge for upto three and half hours
if (hours <= 3.50)
result = 7.50;
// Maximum Charge for 24 hours
// Here hours > 15 because
//when we calculate upto 15 then Charge is less than
25.72
else if (hours > 15.00)
result = 25.72;
else
// additional R1.50 charge per hour for each hour or
part
// thereof in excess of three hours.
result = 7.50 + ceil((hours - 3)) *
1.50;
return result;
}
int main()
{
double totalCharge = 0;
double totalHours = 0;
double charge;
double hoursFirst, hoursSecond, hoursThird;
cout << "Enter hours parked for all three
customers" << endl;
cin >> hoursFirst >> hoursSecond >>
hoursThird;
cout << left << setw(10) << "Car"
<< setw(10) << right << "Hours"
<< setw(10) << "Charge" <<
endl;
// Output for Customer 1
charge = calculateCharges(hoursFirst);
totalCharge += charge;
totalHours += hoursFirst;
cout << fixed << left << setw(10)
<< "Car 1" << setw(10) << right <<
setprecision(1) << hoursFirst
<< setw(10) <<
setprecision(2) << charge << endl;
// Output for Customer 2
charge = calculateCharges(hoursSecond);
totalCharge += charge;
totalHours += hoursSecond;
cout << fixed << left << setw(10)
<< "Car 2" << setw(10) << right <<
setprecision(1) << hoursSecond
<< setw(10) <<
setprecision(2) << charge << endl;
// Output for Customer 3
charge = calculateCharges(hoursThird);
totalCharge += charge;
totalHours += hoursThird;
cout << fixed << left << setw(10)
<< "Car 3" << setw(10) << right <<
setprecision(1) << hoursThird
<< setw(10) <<
setprecision(2) << charge << endl;
// Output for Total
cout << left << setw(10) << "TOTAL"
<< setw(10) << right << setprecision(1) <<
totalHours
<< setw(10) << setprecision(2)
<< totalCharge << endl;
}