2013-04-14 26 views
0

我的分配是使用3個功能與參數,如下所示:C++破程序分成功能

  1. 功能calcRegBill - 接受使用分鐘數一個整數參數。確定並返回到期總額。

  2. 函數calcPremBill - 接受兩個整數參數,用於日分鐘數和使用的夜分鐘數。確定並返回到期總額。

  3. 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; 
} 
+0

不確定在這裏傳達問題。有什麼問題?你不明白功能是如何工作的嗎?你不明白如何將參數傳遞給一個函數並捕獲返回值?這三個功能的概念分解看起來非常直截了當,否則。 –

+0

的確,我不明白如何將參數傳遞給函數並捕獲返回值。我正在使用一本教科書,但我覺得它很混亂! – user2280702

+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; 

然後,你可以將先前在主計算amtDue到calcRegBill()函數的代碼,像這樣:

double calcRegBill(int minutes) 
{ 
    double bill; 

    if (a < 50) 
     bill = 10; 

    else 
     bill = 10+((minutes-50)*.20); 

    return bill; 
} 

這裏的關鍵是,而不是在主函數計算amtDue,你計算出它在calcRegBill功能,並返回到主函數。另外請注意,我將參數的名稱從a更改爲minutes。這改善了它在功能中的目的清晰度。

0

你的程序的一般結構大多是正確的,我不確定你在問什麼。

對於故障排除,編寫一個函數並測試它,而不用其他所有東西。例如,寫calcRegBill ...

然後寫一個非常簡單的主:

int main() { 
    cout << calcRegBill(3) << endl; 
    cout << calcRegBill(11) << endl; 
} 

弄來的預期值?如果是這樣,那麼繼續下一個功能。開發往往是將問題分解成更小的可管理問題。

+0

我不知道如何分解程序,我知道如何調用主函數內的函數,但我該如何處理我的開關(因爲現在有2個獨立的地方來計算每種類型的帳單,然後打印賬單在我的程序中,用戶輸入他們擁有的服務類型的代碼,然後分鐘和交換機打印輸出。我的問題是如何將它分解成我列出的3個函數仍然有程序正常工作? – user2280702

0

你需要分解你正在做的事情的流程。

  1. 首先,您收集信息。注意:您要查找的信息是相同的,無論它是優質還是常規帳單。你不需要這麼早分支。
  2. 爲了計算他們的賬單,你根據溢價或定期進行分支。無論哪種方式,您從中得到一個double,所以您可以有一個double變量,您將calcRegBillcalcPremBill的返回存儲到中。在分支之前聲明這個double變量,在兩個分支(正常或高級)內分配它。
  3. 然後,當您撥打printBill時,您可以傳遞此值以及它是哪種類型的帳單。