【问题标题】:How should I approach the logic for writing a piece of code that determines savings for a subscription package我应该如何处理编写一段代码来确定订阅包节省的逻辑
【发布时间】:2015-02-23 23:21:08
【问题描述】:

我在计算手机套餐因图书问题而节省的费用时遇到了问题。我想指出我已经完成了第一部分的代码。这是我遇到麻烦的第二部分。好的,以下是书中的问题(针对上下文):

第 1 部分

移动电话服务提供商为其客户提供三种不同的订阅套餐:

套餐 A:每月 39.99 美元,提供 450 分钟。额外分钟数为每分钟 0.45 美元。

套餐 B:每月 59.99 美元,提供 900 分钟。额外分钟数为每分钟 0.40 美元。

套餐 C:每月 69.99 分钟,提供无限分钟。

编写一个计算客户每月账单的程序。它应该询问客户购买了哪个包裹以及使用了多少分钟。然后它应该显示到期的总金额。

第 2 部分

修改第 1 部分中的程序,使其同时显示套餐 A 的客户如果购买套餐 B 或 C 可以节省多少钱,以及套餐 B 的客户如果购买套餐 C 可以节省多少钱。如果没有正在保存,不应打印任何消息。

在第二部分,我如何计算计划的节省?我似乎无法正确实施它们。我最初的想法是从每月总账单中减去总额外分钟数的超额费用,但我无法让它像我想要的那样工作。这是我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{

    double minUsed, minLeft, extraMinCost, monthTotal, planSaveB, planSaveC;
    char choice;
    const double planCostA = 39.99, planCostB = 59.99, planCostC = 69.99, monthlyMinA = 450, monthlyMinB = 900;;
    

        cout<<"Enter your monthly package plan: Ex. A, B or C"<<endl<<endl;
        cin>>choice;
        cout<<endl;
        
        cout<<"Enter the amount of minutes you used: "<<endl;
        cin>>minUsed;
        cout<<endl;

        if (choice == 'a' || choice == 'A')
        {
            
            minLeft=monthlyMinA-minUsed;
            if (minLeft < 0)

            {
                extraMinCost=minLeft*(-0.45);
                monthTotal=planCostA+extraMinCost;
                planSaveB = (planCostB-extraMinCost)-(-planCostB);
                planSaveC = planCostC - (planCostC-extraMinCost);
                cout << "Your total bill amount is: " << setprecision(2) << fixed << "$" << monthTotal << endl<<endl;
                cout << "You could save " << setprecision(2) << fixed << "$" << planSaveB << " if you switch to plan B or ";
                cout << "save " << setprecision(2) << fixed << "$" << planSaveC << " If you switch to plan C." << endl << endl;;
            }
            else if (minLeft >= 0)
            {
                monthTotal = planCostA;

                cout << "Your total bill amount is: " << setprecision(2) << fixed << "$" << monthTotal << endl;
            }

        }
        
        /*else if (choice == 'b'|| choice == 'B')
            {

                minLeft=monthlyMinB-minUsed;

                if (minLeft < 0)

                {
                    extraMinCost=minLeft*(-0.40);
                    monthTotal=planCostB+extraMinCost;
                }
                else
                    monthTotal=planCostB;

            cout<<"Your total bill amount is: "<<setprecision(2)<<fixed<<"$"<<monthTotal<<endl;

}
        else if (choice == 'c' || choice == 'C')
            {
                
                
                monthTotal=planCostC;

                cout<<"Your total bill amount is: "<<setprecision(2)<<fixed<<"$"<<monthTotal<<endl;
                cout<<"Current plan has unlimited minutes!"<<endl;


            }*/
        


        cout<<choice;

    return 0;
}

相关部分是我的代码的选择部分。当我可以正确地获得该包的一部分时,我将修改其余部分。

【问题讨论】:

  • 您应该使用您正在使用的实际编程语言正确标记它(而不仅仅是使用您正在使用的工具/IDE用于您的编程。)
  • 对不起。刚刚添加了适当的标签

标签: c++ visual-studio-2013


【解决方案1】:

我想你可能想写一个像

这样的函数
float getPlanACost( int minutes ) { ... }

返回如果客户有计划 A 并使用“分钟”分钟,他们将被计费的金额。然后为 B 计划和 C 计划编写类似的函数。使用这些函数打印您的每月账单,并根据要求计算可用的节省。

软件开发的很大一部分是将大问题分解为小问题。

【讨论】:

    【解决方案2】:

    您可以在 if 语句中使用 switch 语句,这将使您的代码更容易理解和更短。

    例子:

    switch (package)
        {
        case 'A':
        {
            if (time < 450)
                total = packageA;
            else
                total = ((time - 450)*rate_A) + packageA;
            break;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多