我必须创建一个程序,该程序从用户读取他们什么时候进入停车场以及剩下的时间。该程序使用此信息来弄清楚该人将其停在那里的费用。在不到30分钟的时间内,它是免费的。30分钟和最多2小时后,这是一笔3美元的基本费用,每分钟每分钟超过30分钟,每分钟5美分。超过2个小时,这是一个8美元的基本费用,每分钟超过2小时,每分钟每分钟10美分。到目前为止,我必须将用户输入的时间转换为所有分钟。现在,我一直坚持如何处理我的其余功能。我是编程的新手,功能中的论点仍然使我感到困惑。如果您能够提供帮助或提供任何反馈,请在评论部分中告诉如何实施参数以使代码正确运行。到目前为止,这是我的代码:
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes) {
double total = 0;
double cents = 0;
if(total_mins < 30){
return 0;
}
else if (total_mins <= 120){
cents = (total_mins - 30) * 0.05;
return total = 3 + cents;
}
else{
cents = (total_mins - 120) * 0.10;
return total = 4.5 + 8 + cents;
}
} // returns parking charge
void print_results(total_minutes, double charge)
{
cout << "The charge of parking was: " << parking_charge(total_minutes)
}
int main() {
int entry_time = 0;
int exit_time = 0;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
cout << print_results(total_minutes, charge);
}
我已使用工作停车充电功能更新了代码。我现在的目标是使print_results函数正常工作,并找出如何使所有这些功能在主函数中合作。感谢所有人到目前为止的帮助。
您几乎完成了,您需要在主函数中正确调用功能。在主函数totalTime
和totalCharge
中声明两个变量,然后调用函数
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes)
{
int charge;
if (total_minutes <= 30)
charge = 0;
else if (120 > total_minutes < 30)
charge = (3 + (0.05 * total_minutes));
else
charge = (8 + (0.10 * total_minutes));
} // returns parking charge
void print_results(double charge)
{
cout << "The charge of parking was: " << charge;
}
int main()
{
double charge,minutes;
int entry_time,exit_time;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
minutes=elapsed_time(entry_time,exit_time);
charge=parking_charge(minutes);
print_results( charge);
}