Toll roads have different fees at different times of the day and on weekends. Write a function CalcToll() that has three arguments: the current hour of time (int), whether the time is morning (bool), and whether the day is a weekend (bool). The function returns the correct toll fee (double), based on the chart below.
Weekday Tolls
Weekend Tolls
Ex: The function calls below, with the given arguments, will return the following toll fees:
CalcToll(8, true, false) returns 2.95
CalcToll(1, false, false) returns 1.90
CalcToll(3, false, true) returns 2.15
CalcToll(5, true, true) returns 1.05
#include <iostream>
using namespace std;
double CalcToll(int time, bool morning, bool weekend) {
double price;
if (not morning)
time = time + 12;
if (weekend){
if (time >= 0 && time < 7){
price = 1.05;
}
if (time >= 7 && time < 20){
price = 2.15;
}
if(time >= 20 && time < 24){
price = 1.10;
}
}
else{
if (time >= 0 && time < 7){
price = 1.15;
}
if (time >= 7 && time < 10){
price = 2.95;
}
if(time >= 10 && time < 15){
price = 1.90;
}
if (time >= 15 && time < 20){
price = 3.95;
}
if(time >= 20 && time < 24){
price = 1.40;
}
}
return price;
}
int main() {
int hour;
bool m,w;
cout << "Please enter current hour of time: ";
cin >> hour;
cout << "Whether the time is morning (1 - yes, 0 - no): ";
cin >> m;
cout << "Whether the day is aweekend (1 - yes, 0 - no): ";
cin >> w;
cout << "\n********************************";
cout << "\n* The correct toll fee: " << CalcToll(hour,m,w) << " *";
cout << "\n********************************";
return 0;
}
Comments
Leave a comment