Write a program that computes the cost of a long distance call. The cost of the call is determined according to .
• A call made between 8 AM and 6 PM is at a rate of 6 rupees per minute.
• A call made before 8 AM or after 6 PM is charged at a rate of 3.75 rupees.
if call starts at any time between 8:00 AM and 6:00 PM, and it ends after 6:00 PM then it will be charged at the rate of 6 rupees per min for the time before 6:00 PM and for rest of time the rate will be 3.75 rupees per min For example if call starts at 5:55 PM and ends at 6:05 PM then charges on this call be 48.75(30 rupees for first 5 mins and 18.75 rupees for rest of the time) if a call starts at time before 8:00 AM but ends after 8:00 AM then it will charged at rate of 3.75 rupees for time before 8:00 AM and for remaining minutes after 8:00 AM the rate will 6 rupees per min. For example if a call starts at 7:49 AM and ends at 8:01 AM the charge on that call will be 47.25 rupees (41.25 for first 11 min and 6 rupees for last min).
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
int time2min(string& str) {
istringstream istr(str);
int h, m;
string ampm;
char ch;
int res;
istr >> h >> ch >> m >> ampm;
res = h*60;
res += m;
if (ampm == "PM") {
res += 12*60;
}
return res;
}
int main() {
string str;
int t1=time2min(string("8:00 AM"));
int t2=time2min(string("6:00 PM"));
int start, end;
cout << "Enter start time: ";
getline(cin, str);
start = time2min(str);
cout << "Enter end time: ";
getline(cin, str);
end = time2min(str);
double cost = 0.0;
if (end <= t1 || start >= t2) {
cost = (end - start) * 3.75;
}
else if (t1 <= start && end <= t2) {
cost = (end - start) * 6;
}
else if (start <= t1) {
cost = (t1 - start) * 3.75;
cost += (end - t1) * 6;
}
else {
cost = (t2 - start) * 6;
cost += (end - t2) * 3.75;
}
cout << "The cost is " << fixed << setprecision(2) << cost;
return 0;
}
Comments
Thank you so much. It's really helpful.
Leave a comment