gpt4 book ai didi

c++ - 如何向该程序添加循环?

转载 作者:行者123 更新时间:2023-11-28 03:50:35 25 4
gpt4 key购买 nike

我的家庭作业问题:

During the tax season, every Friday, J&J accounting firm privides assistance to people who prepare their own tax returns. Their charges are as follows.

a. If a person has low income (<=25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.

b. For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.

(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 x 0.40 x (45 / 60) = $21.00.)

Write a program that prompts the user to enter the hourly rate, the total consulting time, and whether the person has low income. The program should output the billing amount. The program must contain a function that takes as input the hourly rate, the total consulting time, and a value indicating whether the person has low income. The function should return the billing amount. The program may prompt the user to enter the consulting time in minutes.

这是我写的代码:

#include <iostream>

using namespace std;

double calculate(char iRate, double cTime, double hRate);

int main()
{
double hRate = 0.0, cTime = 0.0;
char iRate, l('l'), h('h');

cout << "Please enter the hourly rate: ";
cin >> hRate;
cout << "Please enter total consulting time in minutes: ";
cin >> cTime;
cout << "Is the income rate low or high (l - Low | h - High): ";
cin >> iRate;

cout << calculate(iRate, cTime, hRate) << endl;

system("pause");
return 0;
}

double calculate(char iRate, double cTime, double hRate)
{
double bAmount = 0.0;
if (iRate == 'l' && cTime > 30)
bAmount = (hRate * 40) * ((cTime)- 30)/6000;
else if (iRate == 'h' && cTime > 20)
bAmount = (hRate * 70) * ((cTime)- 30)/6000;

return bAmount;
}

我不确定如何向其添加循环以使其运行 3 次?

最佳答案

为了代码清晰,我会做的是将您的逻辑从您的 main 中分离到另一个函数中

void gatherInputAndCalculate()
{
double hRate = 0.0, cTime = 0.0;
char iRate, l('l'), h('h');

cout << "Please enter the hourly rate: ";
cin >> hRate;
cout << "Please enter total consulting time in minutes: ";
cin >> cTime;
cout << "Is the income rate low or high (l - Low | h - High): ";
cin >> iRate;

cout << calculate(iRate, cTime, hRate) << endl;

system("pause");
}

然后在你的 main 中循环

int main()
{
for(int i = 0; i < 3; i++)
{
gatherInputAndCalculate();
}
return 0;
}

现在,这并不是说您不能将整个逻辑 block 包装在 for 循环中,但为了清晰/调试目的,将合并的逻辑 block 包装到函数中是一个很好的做法。它不会像这样简单的任务发挥作用,但在现实世界中,你的代码库有数十万行,它真的很有帮助,所以现在把好的做法记下来将有助于轻松过渡!

免责声明 我没有以任何方式、形状或形式检查您的代码的正确性。我只是采用了您在主要功能中的逻辑并将其放入它自己的功能中。其中预先存在的任何错误都将出现在我提供的功能中。类似的事情超出了这个问题的范围。 (并不是说如果我看到一个我就不会指出一个,但还有另一件事是我们现实世界的程序员正在努力解决的问题:有时我们不被允许修复错误,因为我们得到了足够的资金仅用于修复其中的一小部分,我们被告知要修复哪些。当您是那种喜欢始终把事情做好的人时,这可能会令人沮丧。)

关于c++ - 如何向该程序添加循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5637719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com