问题如下:
Write a program that computes the cost of along-distance call. The cost of the call is determined according to thefollowing rate schedule: - Any call started between 8:00 A.M. and 6:00 P.M., Monday through Friday, is billed at a rate of $0.40 per minute.
- Any call starting before 8:00 A.M. or after 6:00 P.M., Monday through Friday, is charged at a rate of $0.25 per minute.
- Any call started on Saturday or Sunday is charged at a rate of $0.15 per minute.
The input will consist of - theday of the week - thetime the call started - thelength of the call in minutes The output will be the cost of the call.The time is to be input in 24-hour notation (e.g. the time 1:30 P.M. is input as 13:30). The day of the week will be read asone of the following pairs of character values, which are stored in twovariables of type char: Mo Tu We Th Fr Sa Su Be sure to allow the user to use eitheruppercase or lowercase letters or a combination of the two. The number ofminutes will be input as a value of type int.Your program should include a loop that lets the user repeat this calculationuntil the user says she or he is done.
我试过的答案是:
#include <iostream>
using namespace std;
int main()
{
int start_time, minutes;
double cost;
char first, second, choice('Y');
do
{
cout<<"When did the call start?\n";
cout<<"Please enter the time in 24-hour notation.\n";
cin>>start_time;
cout<<"How long did the call last?\n";
cin>>minutes;
cout<<"Please enter the first two letters of the day of the call.\n";
cin>>first>>second;
if ((((first=='M')||(first=='m'))&&((second=='O')||(second=='o'))) || (((first=='T')||(first=='t'))&&((second=='U')||(second=='u'))) || (((first=='W')||(first=='w'))&&((second=='E')||(second=='e'))) || (((first=='T')||(first=='t'))&&((second=='H')||(second=='h'))) || (((first=='F')||(first=='f'))&&((second=='R')||(second=='r'))))
{
if ((start_time>=800)||(start_time<=1800))
{
cost=minutes*.40;
}
else
{
cost=minutes*.25;
}
}
else if ((((first=='S')||(first=='s'))&&((second=='A')||(second=='a'))) || (((first=='S')||(first=='s'))&&((second=='U')||(second=='u'))))
{
cost=minutes*.15;
}
cout<<"The cost of the call is "<<cost<<".\n";
cout<<endl;
cout<<"Do you want to rerun the program? Y or N\n";
cin>>choice;
cout<<endl;
}
while ((choice=='Y')||(choice=='y'));
return 0;
}
但依旧不完整,因为:
1. 无法从 59分钟跳到 00分钟
2. 不能同时包含两天的通话时间(比如从星期五的11.00pm打到星期六1.00am是两小时,而这两小时却只算在星期五)
请问有什么解决方法?另外这是否和<chrono>有关?
|