我的分配是使用3個功能與參數,如下所示:C++破程序分成功能
功能calcRegBill - 接受使用分鐘數一個整數參數。確定並返回到期總額。
函數calcPremBill - 接受兩個整數參數,用於日分鐘數和使用的夜分鐘數。確定並返回到期總額。
function printBill - 接受4個參數:一個字符串帳號,一個字符服務代碼,一個整數使用的總分鐘數以及一個到期金額。請注意,這是一個通用的打印賬單功能,它打印無論是定期或溢價賬單,使用以下格式:
帳號:XXXX
服務類型:正常(或者溢價的,這取決於字符接收)
總分鐘:XXX
應付金額:$ XXX.XX
你的主要功能將提示個用戶電子帳號和服務代碼。根據服務代碼,main會要求正確的分鐘數,然後根據需要調用上面的函數來完成工作。此外,您必須:
將一個循環合併到您的程序中,根據需要多次運行帳單。您可以通過定點控制迴路或計數器控制迴路來完成此操作。
我已經建立了程序,並在程序的主要功能中對它進行了測試。我對如何將它分解成3個獨立的功能感到非常困惑,並且仍然有效。我是一個C++的總noob
這是迄今爲止的程序,我開始添加功能,但我不相信他們是正確的。
// 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;
}
不確定在這裏傳達問題。有什麼問題?你不明白功能是如何工作的嗎?你不明白如何將參數傳遞給一個函數並捕獲返回值?這三個功能的概念分解看起來非常直截了當,否則。 –
的確,我不明白如何將參數傳遞給函數並捕獲返回值。我正在使用一本教科書,但我覺得它很混亂! – user2280702
啊,好的。你的導師*真的需要涵蓋一些基本的東西 - 一本教科書不太合適。我很樂意在聊天中爲您提供幫助,因爲這是一個應該在交互式環境中教授的主題,而不是一本書。讓我看看我是否無法從這個評論中找到產生聊天室的方法... –