如果还有其他原因呢? 目前,我在当地的大学上了一门C++课程,并被调试了一次作业。在本作业的说明中,我被告知,这段代码唯一的错误是,第82-89行上嵌套的if-else-if语句的条件是多余的,但是,如果这些条件不保持不变,我看不到获得相同结果的其他方法……任何提示或类似的建议都将不胜感激 #include <iostream> #include <conio.h> #include <iomanip> using namespace std; const int BASE_COST = 10; const int LOW_LIMIT = 20; const int MID_LIMIT = 40; const int HIGH_LIMIT = 60; const double LOW_CHECKS = .10; const double MIDLOW_CHECKS = .08; const double MIDHIGH_CHECKS = .06; const double HIGH_CHECKS = .04; int main() { int numOfChecks; double multiplierValue; double totalFee; cout << fixed << showpoint; cout << setprecision(2); cout << "Please enter the number of checks you used this month: "; cin >> numOfChecks; if(numOfChecks < 0) { cout << "Number of checks can't be negative. Program ends.\n"; exit(1); //terminate the program with error code 1 } //the following line runs only if the program did not terminate, so start over if-else if(numOfChecks < LOW_LIMIT) multiplierValue = LOW_CHECKS; else if(numOfChecks < MID_LIMIT) multiplierValue = MIDLOW_CHECKS; else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT) multiplierValue = MIDHIGH_CHECKS; else if (numOfChecks >= HIGH_LIMIT) multiplierValue = HIGH_CHECKS; totalFee = BASE_COST + numOfChecks * multiplierValue; cout << "Your total for this month is $" << totalFee; _getch(); return 0; } #包括 #包括 #包括 使用名称空间std; 成本成本=10; const int LOW_LIMIT=20; const int MID_LIMIT=40; const int HIGH_LIMIT=60; 常数双低_检查=.10; 常数双中低检查=.08; 常数双中高检查=.06; 常数双高检查=0.04; int main() { 国际货币基金组织; 双倍增值; 双倍总费用; cout

如果还有其他原因呢? 目前,我在当地的大学上了一门C++课程,并被调试了一次作业。在本作业的说明中,我被告知,这段代码唯一的错误是,第82-89行上嵌套的if-else-if语句的条件是多余的,但是,如果这些条件不保持不变,我看不到获得相同结果的其他方法……任何提示或类似的建议都将不胜感激 #include <iostream> #include <conio.h> #include <iomanip> using namespace std; const int BASE_COST = 10; const int LOW_LIMIT = 20; const int MID_LIMIT = 40; const int HIGH_LIMIT = 60; const double LOW_CHECKS = .10; const double MIDLOW_CHECKS = .08; const double MIDHIGH_CHECKS = .06; const double HIGH_CHECKS = .04; int main() { int numOfChecks; double multiplierValue; double totalFee; cout << fixed << showpoint; cout << setprecision(2); cout << "Please enter the number of checks you used this month: "; cin >> numOfChecks; if(numOfChecks < 0) { cout << "Number of checks can't be negative. Program ends.\n"; exit(1); //terminate the program with error code 1 } //the following line runs only if the program did not terminate, so start over if-else if(numOfChecks < LOW_LIMIT) multiplierValue = LOW_CHECKS; else if(numOfChecks < MID_LIMIT) multiplierValue = MIDLOW_CHECKS; else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT) multiplierValue = MIDHIGH_CHECKS; else if (numOfChecks >= HIGH_LIMIT) multiplierValue = HIGH_CHECKS; totalFee = BASE_COST + numOfChecks * multiplierValue; cout << "Your total for this month is $" << totalFee; _getch(); return 0; } #包括 #包括 #包括 使用名称空间std; 成本成本=10; const int LOW_LIMIT=20; const int MID_LIMIT=40; const int HIGH_LIMIT=60; 常数双低_检查=.10; 常数双中低检查=.08; 常数双中高检查=.06; 常数双高检查=0.04; int main() { 国际货币基金组织; 双倍增值; 双倍总费用; cout,c++,if-statement,C++,If Statement,如果(numOfChecks>=MID\u LIMIT&&numOfChecks

如果(numOfChecks>=MID\u LIMIT&&numOfChecks看起来是多余的,则此部分
如果(numOfChecks
看起来是多余的。如果您保持范围检查的顺序,它可以简化为仅
如果(numOfChecks
,与它后面的一部分相同(只是不需要),因此整件看起来像:

//the following line runs only if the program did not terminate, so start over if-else
if (numOfChecks < LOW_LIMIT)
    multiplierValue = LOW_CHECKS;
else if (numOfChecks < MID_LIMIT)
    multiplierValue = MIDLOW_CHECKS;
else if (numOfChecks < HIGH_LIMIT)
    multiplierValue = MIDHIGH_CHECKS;
else
    multiplierValue = HIGH_CHECKS; 
//只有当程序没有终止时,才能运行下面的行,如果没有终止,请重新开始
if(numOfChecks<下限)
乘数值=低_检查;
否则如果(numOfChecks
事实上,所有条件都是多余的:使用算法:)

#include <iomanip>
#include <map>
#include <iostream>

namespace {
    using Threshold = unsigned;
    using Rate      = double;

    static Rate constexpr BASE_COST = 10.0;

    std::map<Threshold, Rate, std::greater<> > const tariffs {
        { 0, .10},
        {20, .08},
        {40, .06},
        {60, .04},
    };

    double fee(unsigned numOfChecks) {
        auto rate = tariffs.lower_bound(numOfChecks);
        return BASE_COST + rate->second * numOfChecks;
    }
}

int main() {
    unsigned numOfChecks;
    std::cout << "Please enter the number of checks you used this month: ";

    if (std::cin >> numOfChecks) {
        std::cout 
            << "\nYour total for this month is $"
            << std::fixed << std::showpoint << std::setprecision(2) << fee(numOfChecks)
            << std::endl;
    } else {
        std::cout << "Invalid input\n";
        return 1;
    }
}

嵌套的if-else if语句在第82-89行是冗余的
a)我们没有行号,b)该代码段有49行长c)我没有看到任何嵌套的if/else,或者考虑到
if
/
else
梯形图中的最后一个条件。根据前面的条件,您可以对exe的
numfchecks
值得出什么结论达到这一点吗?原始代码中有一些指令,这些指令高于实际代码,我在这里发布时将其删除,这可能就是为什么行数小于这些指令的原因,尽管对于嵌套的if/else,这是我的导师在代码末尾附近提到的一系列if/else if语句。@Borgleader为True,但re是这个问题真正适用的唯一一组if-else测试。并不难找到。@DanMašek考虑到b,可能有一个嵌套的if-in代码被意外地排除在问题之外。在这一点上,a和c是相关的点。这很有意义,我看不到任何其他方法来减少冗余,非常感谢你!!
Please enter the number of checks you used this month: 10
Your total for this month is $11.00

Please enter the number of checks you used this month: 20
Your total for this month is $11.60

Please enter the number of checks you used this month: 30
Your total for this month is $12.40

Please enter the number of checks you used this month: 40
Your total for this month is $12.40

Please enter the number of checks you used this month: 50
Your total for this month is $13.00

Please enter the number of checks you used this month: 60
Your total for this month is $12.40

Please enter the number of checks you used this month: 70
Your total for this month is $12.80

Please enter the number of checks you used this month: 80
Your total for this month is $13.20

Please enter the number of checks you used this month: 90
Your total for this month is $13.60

Please enter the number of checks you used this month: 100
Your total for this month is $14.00