2012-09-15 46 views
1

請看看下面的代碼計算美元就在貨幣改革計劃 - C++

#include <QtCore/QCoreApplication> 
#include <iostream> 

int main(int argc, char *argv[]) 
{ 
    using namespace std; 

    double purchaseAmount; 
    double paidAmount; 
    float balance; 

    int change, quarters, dimes, nickels, pennies, tenDollar, fiveDollar; // declare variables 

    cout << "Enter Total purchased amount" << endl; 
    cin >> purchaseAmount; 

    cout << "Enter Total paid amount" << endl; 
    cin >> paidAmount; 

    balance = paidAmount - purchaseAmount ; 

    tenDollar = balance/10; // calculate the number of Ten Dollars 
    change = tenDollar % 10 ; // calculate the change needed 
    change = balance * 100; 
    quarters = change/25; // calculate the number of quarters 
    change = change % 25; // calculate remaining change needed 
    dimes = change/10; // calculate the number of dimes 
    change = change % 10; // calculate remaining change needed 
    nickels = change/5; // calculate the number of nickels 
    pennies = change % 5; // calculate pennies 

    cout << "\nQuarters: " << quarters << endl; // display # of quarters 
    cout << " Dimes: " << dimes << endl; // display # of dimes 
    cout << " Nickels: " << nickels << endl; // display # of nickels 
    cout <<" Pennies: " << pennies << endl; // display # of pennies 
    cout <<" Ten dollar: " << tenDollar << endl; // display # of Ten dollar 
    //cout <<" Five dollar: " << fiveDollar << endl; // display # of Ten dollar 

    return (0); 

} 

我想在這裏做,計算剩下十塊錢,宿舍,硬幣,鎳的變化和便士。例如,當我用這種方式運行程序 -

Enter Total purchased amount 
9.75 
Enter Total paid amount 
20 

Quarters: 4 
Dimes: 0 
Nickels: 0 
Pennies: 0 
Ten dollar: 1 

這是錯誤的。這就是說,上面的輸出是錯誤的。相反它應該是

Enter Total purchased amount 
9.75 
Enter Total paid amount 
20 

Quarters: 1 
Dimes: 0 
Nickels: 0 
Pennies: 0 
Ten dollar: 1 

那麼我在這裏做錯了什麼?

感謝

+0

那麼我在這裏做錯了什麼? - 期待別人做你的功課我猜... – dtech

+0

不,我不是..我自己開始,尋求幫助。 – user15169

+0

我們似乎已經丟失了下面的代碼... –

回答

0

沒有太大變化你的代碼,你可以通過更換

change = balance * 100; 

change = ((balance) - floor(balance)) * 100; 

然而,把更多的想進入你的解決方案得到您想要的答案,而且我保證你會得到更多的積分,而不是簡單的解決方法。另外,你不敢打破約定,請使用名稱空間標準以外的主要位置您的

像這樣:

#include <iostream> 
using namespace std; // Place it here! Not inside main. 

int main() 
{ 
return 0; 
} 

注:我說想要的答案。

+0

因爲什麼時候在文件範圍內放置'using namespace std;'是個好主意,更不用說'約定'了? –

+1

因爲永遠。它不是文件範圍。其*文件範圍。惡魔之穴將其放入一個共享*標題*文件中。你提供的源文件是好的(我個人從來沒有使用它,但如果你打算,c/cpp是地方;最肯定的是從來沒有頭文件。 – WhozCraig

1

正如人們一再說過的,錯誤在於痛苦的重複代碼。考慮以下幾點:

int currencyCount(int& pennies, int penniesInDenomination) { 
    const int count = penniesInBase/penniesInDenomination; 
    pennies = pennies % penniesInDenomination; 

    return count; 
} 

這可以用於每個面額 - 重複和一行。這可以通過讓函數得到兩個值:新的餘額和該面額的計數。這種做法通過引用平衡來作弊,並且作爲將此稱爲獨立功能的「副作用」,它會根據返回的面額數量減少餘額。很明顯,你會想記錄這一點。

... 
const int numberOfQuarters = currencyCount(balance, 25); 
const int numberOfDimes = currencyCount(balance, 10); 
... 

你也可以把貨幣信息(如姓名和便士它所代表的數量)超過它的載體和循環中執行同樣的事情:

typedef std::pair<std::string, int> Currency; 
typedef std::vector<Currency> Currencies; 
typedef Currencies::const_iterator CIter; 

... 

for(CIter iter = currencies.begin(); iter != currencies.end(); ++iter) { 
    const int quantity = currencyCount(balance, iter->second); 
    std::cout << iter->first << ": " << quantity << std::endl; 
} 

以這種方式,你避免了重複的代碼和它涉及的錯誤。