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

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

本文关键字:包节省 代码 何处理 处理 我应该 一段      更新时间:2023-10-16

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

第1部分

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

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

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

套餐C:每月69.99分钟,不限时间。

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

第2部分

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

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

#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并使用了"分钟"分钟,则返回将向其计费的金额。然后为计划B和计划C编写类似的函数。使用这些函数打印您的月度账单,并根据要求计算可用的节省。

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

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

示例:

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