-5

我必须创建一个程序,从用户那里读取他们进入停车场的时间和离开的时间。该程序使用这些信息来计算该人在那里停车的费用。不到 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 函数正常工作,并找出如何让所有这些在 main 函数中一起工作。感谢所有人迄今为止的帮助。

4

1 回答 1

-2

你几乎完成了,你需要在主函数中正确调用函数。在main函数中声明两个变量totalTimetotalCharge然后调用函数

    #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);

    }
于 2018-02-08T03:02:51.590 回答