我应该如何处理编写一段确定订阅包节省的代码的逻辑
How should I approach the logic for writing a piece of code that determines savings for a subscription package
我在计算图书问题的手机计划节省的费用时遇到了问题。我想指出的是,我已经完成了第一部分的代码。这是我遇到麻烦的第二部分。好的,这是本书的问题(上下文):
第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;
相关文章:
- R 包中的 TMB c++ 代码和 CRAN 检查
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 将代码移出类定义时未扩展参数包
- AWS 开发工具包 C++ 代码不适用于发布版本
- 在R软件包中包含TMB C 代码的指南
- 如何正确解包来自提升异常的错误代码
- gcc segfault在编译嵌套参数包代码上
- 是否可以根据C/C 代码上的Linux上的(动态更改的IP地址集)丢弃传入数据包
- 在包含 R/Cpp 代码的 R 包中使用 roxygen2 for R 函数
- 数据包丢失 (UDP) 的错误更正代码
- 我应该如何处理编写一段确定订阅包节省的代码的逻辑
- C++ 块代码在函数完成之前节省内存
- 有没有一种更简单的方法可以像在java中使用包一样在C++中重用代码?制作头文件比较复杂
- 为什么此代码工作/失败取决于在中创建的范围闭包
- 静态方法节省内存?(非托管代码)
- 无法弄清楚为什么此代码在发送数据包数据时会导致 SIGSEGV
- 我应该用cmake搜索什么Boost包来包含Boost Functional的代码
- LCOV从代码覆盖率分析中排除整个包
- 使用RStudio在R包中编译Rcpp代码时出错
- 在 R 包中生成和链接共享库 - 代码编译、链接,但不会加载