c++ - 我应该如何处理编写一段确定订阅包节省的代码的逻辑

标签 c++ visual-studio-2013

我在计算手机套餐的节省金额时遇到了书籍问题。我想指出,我已经完成了第一部分的代码。这是我遇到麻烦的第二部分。好的,这里是书中的问题(用于上下文):

Part 1

A mobile phone service provider has three different subscription packages for its customers:

Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute.

Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute.

Package C: For 69.99 per month unlimited minutes provided.

Write a program that calculates a customer’s monthly bill. It should ask which package the customers has purchased ans how many minutes were used. It should then display the total amount due.

Part 2

Modify the program in part 1 so that it also displays how much money Package A customers would save if they purchased packages B or C, and how much money Package B customers would save if they purchased Package C. If there would be no saving, no message should be printed.

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

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

相关部分是我代码的选择部分。当我能把那个包的一部分弄好时,我会修改其余部分。

最佳答案

我想你可能想写一个像

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

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

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

关于c++ - 我应该如何处理编写一段确定订阅包节省的代码的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28685501/

相关文章:

c++ - QML 引擎无法正确导入 C++ 模型

c++ - 库设计困惑.. "public"/"private"(template) headers, library files..?

c++ - 如果只有一个成员没有默认构造函数,为什么 union 有一个已删除的默认构造函数?

unit-testing - 在 Visual Studio 中启动单元测试非常慢,重复加载/卸载相同的 dll

entity-framework - Visual Studio 2013 和 Entity Framework

c++ - 如何摆脱派生类中的重复代码?

c++ - 在 gui 中处理子级到父级鼠标和键盘事件的最佳方法?

asp.net - 为什么 ASP.NET Web 配置工具被排除在 Visual Studio 2013 之外?

visual-studio-2013 - 自定义工具 'DataServicesCoreClientGenerator'失败。 ...错误0005 : The 'OpenType' attribute is not allowed

asp.net-mvc - MVC中VS2013迁移到VS2015后出现的问题