【问题标题】:How to display differences in prices using switch statements in C++如何在 C++ 中使用 switch 语句显示价格差异
【发布时间】:2015-06-18 02:26:08
【问题描述】:

对于一个课堂项目,我必须开发一个程序来计算移动设备服务的成本。用户可以选择 3 个包。服务的成本由基本价格和要使用的消息单元数决定。在用户选择一个包后,我必须让他们知道包的价格,并让他们知道其他包是否更便宜,并通过使用 switch 语句显示价格差异。我已经写了一些比较这些包的 switch 语句,但是如果有一个比所选包更便宜的包,我如何显示和计算它们会节省多少?我也是初学者,请放心。

int main() {
    bool finished = false;

    do {
        // to keep it simple
        double choice_a = 9.95;
        double choice_b = 19.95;
        double choice_c = 39.95;
        char choice;
        int message_units;
        double price;
        bool selected = false;
        // this loop shows the options initially
        do {
            cout << "Which package do you choose (enter A, B or C)" << endl;
            // you will need to check this
            cin >> choice;
            // keeping it simple
            if (choice == 'A') { price = choice_a; selected = true; }
            else if (choice == 'B') { price = choice_b; selected = true; }
            else if (choice == 'C') { price = choice_c; selected = true; }
            cout << endl;
        }
        // loops until something was selected
        while (selected == false);

        // user enters how many units is wanted
        cout << "How many message units (enter 1 - 672)" << endl;
        // again check this (if homework requires checking input)
        cin >> message_units;

        // Calculating message units
        if(message_units > 5){
            price += 100 * (message_units - 5);
        }
        if(message_units > 15){
            price += 50 * (message_units - 15);
        }

        // Total Price Output
        cout << "Your total cost is " << price/100 << endl

        // Is user done?
        char done;
        cout << "Do you want to enter another? press enter to continue.
        cin >> done;

        // check
        if (done != ' ') {
            finished = true;
        }
    }
    while (finished = false);
}

switch (choice)
{
    case 'A':
    if(choice_b < choice_a);
    cout << "You can save by switching to package B" << endl;
    else if(choice_c < choice_a);
    cout << "You can save by switching to package C" << endl;
    break;
    case 'B':
    if(choice_a < choice_b);
    cout << "You can save by switching to package A" << endl;
    else if(choice_c < choice_b);
    cout << "You can save by switching to package C" << endl;
    break;
    case 'C':
    if(choice_a < choice_c);
    cout << "You can save by switching to package A" << endl;
    else if(choice_b < choice_c);
    cout << "You can save by switching to package B" << endl;
    break;
}

【问题讨论】:

    标签: c++ switch-statement


    【解决方案1】:
     #include <iostream>
    
     using namespace std;
    
     int main() {
        bool finished = false;
        char choice;
        double choice_a = 9.95;
        double choice_b = 19.95;
        double choice_c = 39.95;
        int message_units;
        double price;
        bool selected = false;
    
        do {
            // to keep it simple
            // this loop shows the options initially
            do {
                cout << "Which package do you choose (enter A, B or C)" << endl;
                // you will need to check this
                cin >> choice;
                // keeping it simple
                if (choice == 'A') { price = choice_a; selected = true; }
                else if (choice == 'B') { price = choice_b; selected = true; }
                else if (choice == 'C') { price = choice_c; selected = true; }
                cout << endl;
            }
            // loops until something was selected
            while (selected == false);
    
            // user enters how many units is wanted
            cout << "How many message units (enter 1 - 672)" << endl;
            // again check this (if homework requires checking input)
            cin >> message_units;
    
            // Calculating message units
            if(message_units > 5){
                price += 100 * (message_units - 5);
            }
            if(message_units > 15){
                price += 50 * (message_units - 15);
            }
    
            // Total Price Output
            cout << "Your total cost is " << price/100 << endl;
    
             switch (choice)
             {
             case 'A':
             if(choice_b < choice_a)
             cout << "you can save by switching to package B it saves: " << choice_a - choice_b << " if you choose B" << endl;
             else if(choice_c < choice_a)
             cout << "You can save by switching to package C is saves: " << choice_a - choice_c << endl;
             break;
             case 'B':
             if(choice_a < choice_b)
             cout << "you can save by switching to package A it saves: " << choice_b - choice_a << " if you choose a" << endl;
             else if(choice_c < choice_b)
             cout << "you can save by switching to package C it saves: " << choice_b - choice_c << " if you choose c" << endl;
             break;
             case 'C':
             if(choice_a < choice_c)
             cout << "you can save by switching to package A it saves: " << choice_c - choice_a << " if you choose a" << endl;
             else if(choice_b < choice_c)
             cout << "you can save by switching to package A it saves: " << choice_c - choice_b << " if you choose b" << endl;
             break;
             }
    
            // Is user done?
            char done;
            cout << "Do you want to enter another? press enter to continue.";
            cin >> done;
    
            // check
            if (done != ' ') {
                finished = true;
            }
    
        }
        while (finished == false);
    
    
    }
    

    很抱歉没有解释,但我会尽力解释 根据您的代码。我修复了一些错误的语法并尝试匹配问题 你已经发布了,所以我定义了你在第一个之外创建的所有变量 外部 bool 以便 switch 语句可以工作,我在 switch 语句中编辑一些代码来计算节省的数量并根据条件显示它们,对不起英语不好。

    【讨论】:

    • 鉴于 OP 表示他们是初学者,如果您详细解释您的答案,而不是仅仅发布解决问题的代码,这可能对学习过程很有用。
    • @谢谢伙计,你们会得到报酬来提供帮助还是你们就是那么喜欢编程?
    • 都是因为我喜欢编程和帮助别人:)
    • 好吧,我的代码的计算消息单位部分仍然没有给我正确的输出。当我运行它时,小数点不在正确的位置。对于我的项目,如果输入的消息单元超过五个,我必须将每个消息单元 1.00 添加到包 A。如果输入超过 15 个消息单元,我还必须将每个消息单元添加 0.50 到包 B。
    【解决方案2】:

    假设您正在比较相同数量的两者,

    储蓄= [(选择价格)-(更便宜的期权价格)]×数量

    如果您进行此计算并将其分配给名为 Savings 的双精度类型,则可以将其打印出来。

    cout

    对每个 if 语句执行此操作很简单,但当然还有更好的方法来执行此操作。例如调用一个函数来为两个选项进行这个简单的计算。

    编辑 1:为糟糕的格式道歉,这是在我的手机上。

    编辑 2:还注意到您的控制循环语法不正确。这是一个很好的资源here

    编辑 3:实际上还有许多其他问题,包括您的 switch 语句在 main 之外。

    实际上,这条线没有意义。

    // 计算消息单位 if(message_units > 5){ price += 100 * (message_units - 5); } if(message_units > 15){ 价格 += 50 * (message_units - 15); }

    建议不要用具有不同含义的值覆盖变量。这令人困惑。最初,价格似乎是每数量的价格。然后看起来您尝试将总成本分配给它。

    (我假设;它实际上并没有这样做。相反,它会将除前 5 个之外的每个单位的价格添加到您在顶部声明的单个单位价格中。对于大于 15 的块也是如此。 )

    编辑 4:cmets 中的每个请求:

    如果我认为您正在尝试做的事情是正确的,请替换此块:

    // Calculating message units
        if(message_units > 5){
            price += 100 * (message_units - 5);
        }
        if(message_units > 15){
            price += 50 * (message_units - 15);
        }
    

    与:

    double Total_Cost;
    if(message_units => 5 && message_units<15){
        Total_Cost = price * (message_units - 5);
    }
    else if(message_units => 15){
        Total_Cost = price * (message_units - 15);
    }
    else{
        Total_Cost = price * message_units;
    }
    

    即“如果您批量购买,您将免费获得前 5 个(或 15 个)”。否则,它只是价格乘以单位数量。这似乎有点奇怪,但这就是它的作用。

    【讨论】:

    • 我的公式应该是什么样子,因为当我输出价格时,小数点不对?
    • 它没有用。对于我的项目,如果输入的消息单元超过五个,我必须将每个消息单元 1.00 添加到包 A。如果输入超过 15 个消息单元,我还必须将每个消息单元添加 0.50 到包 B。
    • 我明白了。我的误解。你是在顶部以美分声明价格吗?希望这至少有所帮助。
    • 是的,它在顶部。几天来,我一直在努力使方程式正确。既然您知道我必须计算什么,您能否告诉我我的方程式应该是什么样的。每次我运行它时,小数点都在错误的位置。例如,如果用户选择了包裹 A 并选择了 6 个单位,则正确的价格输出应为 10.95
    • 如果你告诉我你现在得到的话会更容易识别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多