我是一名编码新手,他对如何使这个程序工作感到困惑和困惑。
显示菜单以显示不同的数据计划。计划1每月39.99美元,包括450分钟。额外的分钟是每分钟0.45美元。计划2每月59.99美元,900分钟。额外的分钟是每分钟0.40美元。计划3每月69.99美元,无限制分钟。
•客户的帐号 •客户的计划编号• 客户使用的总分钟数 •应付总金额(格式化)
输入验证:确保用户仅选择包1,2或3。
我使用三种不同的搜索查询变体搜索了stackoverflow:'数据计划'手机数据计划'如何计算数据计划'我确实找到了这篇文章Comparing Cell Phone Plans in C++,这是一个有点类似的程序,但不够相似。我也搜索了它,发现了一些类似的程序,但它们更有帮助,但是太复杂,对我没有足够的帮助。
我处于停滞状态,并且不知道如何使用计划一和计划二上使用的分钟数来计算总账单,这些分钟超过了这两个计划的分配时间。
这是我到目前为止所拥有的:
let jsonString = "{\"boolean_key\" : true, \"int_key\" : 1}"
let jsonData = jsonString.data(using: .utf8)!
let json = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [String:Any]
print(type(of: json["boolean_key"]!)) // __NSCFBoolean
json["boolean_key"] is Double // true
json["boolean_key"] is Int // true
json["boolean_key"] is Bool // true
print(type(of: json["int_key"]!)) // __NSCFNumber
json["int_key"] is Double // true
json["int_key"] is Int // true
json["int_key"] is Bool // true
print(type(of: json["boolean_key"]!) == type(of: NSNumber(value: true))) // true
print(type(of: json["boolean_key"]!) == type(of: NSNumber(value: 1))) // false
print(type(of: json["int_key"]!) == type(of: NSNumber(value: 0))) // true
print(type(of: json["int_key"]!) == type(of: NSNumber(value: true))) // false
答案 0 :(得分:0)
以下是如何根据使用的分钟计算每个计划的成本的示例。
我使用0分钟以每分钟0的成本代表无限分钟,因为它使数学变得容易,并使用函数返回那些分钟的显示值,因此0 =“无限制”。
#include <iostream>
#include <string>
double calculateCost(double planCost,
double minuteCost,
int minutesIncluded,
int minutesUsed)
{
double cost = planCost;
int overage = minutesUsed - minutesIncluded;
if (overage > 0)
{
cost += minuteCost * overage;
}
return cost;
}
std::string includedMinutesString(int numMinutes)
{
if (numMinutes > 0)
{
return std::to_string(numMinutes);
}
return "unlimited";
}
int main()
{
{
double planCost = 39.99;
double minuteCost = 0.45;
int minutesIncluded = 450;
int minutesUsed = 1000;
std::cout << "Plan Cost: " << planCost << " with " <<
includedMinutesString(minutesIncluded) << " minutes included\n";
std::cout << "Cost with " << minutesUsed << " minutes used is " <<
calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
}
std::cout << "\n";
{
double planCost = 59.99;
double minuteCost = 0.40;
int minutesIncluded = 900;
int minutesUsed = 1000;
std::cout << "Plan Cost: " << planCost << " with " <<
includedMinutesString(minutesIncluded) << " minutes included\n";
std::cout << "Cost with " << minutesUsed << " minutes used is " <<
calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
}
std::cout << "\n";
{
double planCost = 69.99;
double minuteCost = 0;
int minutesIncluded = 0;
int minutesUsed = 1000;
std::cout << "Plan Cost: " << planCost << " with " <<
includedMinutesString(minutesIncluded) << " minutes included\n";
std::cout << "Cost with " << minutesUsed << " minutes used is " <<
calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
}
return 0;
}