我的任务是使用带参数的3个函数,如下所示:
function calcRegBill - 接受一个整数参数表示使用的分钟数。确定并返回到期总金额。
function calcPremBill - 接受两个整数参数,用于分钟数和夜间分钟数。确定并返回到期总金额。
function printBill - 接受4个参数:字符串帐号,字符服务代码,使用的整数分钟数和应付金额。请注意,这是一个通用的打印帐单功能,可以使用以下格式打印常规或高级帐单:
帐号:XXXX
服务类型:常规(或高级,取决于收到的字符)
总分钟数:XXX
到期金额:$ XXX.XX
您的主要功能将提示用户输入帐号和服务代码。根据服务代码,main将询问正确的分钟数,然后根据需要调用上面的函数来完成工作。此外,你必须:
在程序中加入一个循环,根据需要多次运行帐单。您可以通过哨兵控制回路或计数器控制回路来执行此操作。
我已经构建了程序并使用程序主要功能中的所有内容对其进行了测试。我真的很困惑如何将它分解为3个独立的功能并让它们仍然有用。我是C ++的总菜鸟
到目前为止,这是程序,我开始添加功能,但我不相信它们是正确的。
// Cell Bill Fun
// April 14, 2013
#include <iostream>
#include <iomanip>
using namespace std;
double calcRegBill(int a);
double calcPremBill(int b, int c);
void printBill(string acctNumber, char serviceCode, int d, double e);
int main()
{
//declare variables for question 4
char serviceCode;
int acctNumber;
int minutes;
int dayMinutes;
int nightMinutes;
int charge;
int dayFee;
int nightFee;
double amtDue;
//get input
cout << "Please enter your information to calculate your cell phone bill ";
cout << "What is your account number? (please enter a 4-digit number-example 1234): ";
cin >> acctNumber;
cout << "Do you have regular or premium service? Enter r for regular service, p for Premium.: ";
cin >> serviceCode;
//format output
cout<< setprecision(2) << fixed;
//output
switch (serviceCode)
{
case 'r':{
cout << "How many minutes did you use?: ";
cin >> minutes;
if (minutes <= 50)
amtDue = 10;
else if (minutes > 50)
amtDue=10+((minutes-50)*.20);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
case 'R':{
cout << "How many minutes did you use?: ";
cin >> minutes;
if (minutes <= 50)
amtDue = 10;
else if (minutes > 50)
amtDue=10+((minutes-50)*.20);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
case 'p':
cout << "How many daytime minutes did you use?";
cin >> dayMinutes;
if (dayMinutes <= 75)
dayFee = 0;
else if (dayMinutes > 75)
dayFee=((dayMinutes-75)*.10);
cout << "How many night time minutes did you use?";
cin >> nightMinutes;
if (nightMinutes <= 100)
nightFee = 0;
else if (nightMinutes > 100)
nightFee=((nightMinutes-100)*.05);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Premium" << endl;
cout <<"Total Minutes:" <<dayMinutes+nightMinutes << endl;
cout <<"Amount Due: $"<<25<<"+"<<dayFee<<"+"<<nightFee<<"= $"<<25+dayFee+nightFee << endl;
break;
case 'P':
cout << "How many daytime minutes did you use?";
cin >> dayMinutes;
if (dayMinutes <= 75)
dayFee = 0;
else if (dayMinutes > 75)
dayFee=((dayMinutes-75)*.10);
cout << "How many night time minutes did you use?";
cin >> nightMinutes;
if (nightMinutes <= 100)
nightFee = 0;
else if (nightMinutes > 100)
nightFee=((nightMinutes-100)*.05);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Premium" << endl;
cout <<"Total Minutes:" <<dayMinutes+nightMinutes << endl;
cout <<"Amount Due: $"<<25<<"+"<<dayFee<<"+"<<nightFee<<"= $"<<25+dayFee+nightFee << endl;
break;
default:
cout << "Invalid Service Code. Enter r for regular service, p for Premium.";
}
return 0;
}
double calcRegBill(int a)
{
}
double calcPremBill(int b, int c)
{
}
void printBill(string acctNumber, char serviceCode, int d, double e )
{
return;
}
答案 0 :(得分:1)
通过返回数据,函数可以通过请求数据(传递给它们的参数)以及对此数据进行操作来工作。
例如,在case 'r':
块中,您可能希望拥有以下代码而不是代码:
cout << "How many minutes did you use?: ";
cin >> minutes;
amtDue = calcRegBill(minutes);
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
然后,您可以将以前在main中的代码移动到calcRegBill()
函数中来计算amtDue,如下所示:
double calcRegBill(int minutes)
{
double bill;
if (a < 50)
bill = 10;
else
bill = 10+((minutes-50)*.20);
return bill;
}
这里的关键是,不是在main函数中计算amtDue,而是在calcRegBill
函数中计算它并将其返回到main函数。另请注意,我已将参数名称从a
更改为minutes
。这提高了其在功能中的目的的清晰度。
答案 1 :(得分:0)
你的程序的一般结构大多是正确的,我不确定你在问什么。
对于故障排除,请编写一个功能并在没有其他所有功能的情况下进行测试。例如,写calcRegBill ...
然后写一个非常简单的主要内容:
int main() {
cout << calcRegBill(3) << endl;
cout << calcRegBill(11) << endl;
}
您是否获得了预期的价值?如果是这样,那么继续下一个功能。开发通常是将问题分解为更小的可管理问题。
答案 2 :(得分:0)
你需要打破你正在做的事情。
double
,因此您可以拥有一个double
变量,用于存储从calcRegBill
或calcPremBill
返回的变量。在分支之前声明此double
变量,在您的两个分支(常规或高级)内分配它。printBill
时,您可以传递此值以及该帐单的类型。