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

How should I approach the logic for writing a piece of code that determines savings for a subscription package

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

Part 1

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

Package A: For .99 per month 450 minutes are provided. Additional minutes are [=11=].45 per minute.

Package B: For .99 per month 900 minutes are provided. Additional minutes are [=11=].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' 分钟,returns 客户将被收取的费用。然后为 Plan B 和 Plan C 编写类似的函数。使用这些函数打印您的每月账单,并根据要求计算可用的储蓄。

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

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

示例:

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