2013-02-20 112 views
0

我正在爲作業分配創建一個小程序。該程序正確運行,但計算不正確。計算汽車支付金額無法正確計算

我使用來計算付款量的公式是:

支付=(INTRATE *(1 + INTRATE)^ N /((1 + INTRATE)^ N-1))* L

其中「N」是支付的數量,「L」是本金。我寫的代碼是這樣的:

monthlyPayment = (intRate * pow ((1 + intRate), numberPayments)/(intRate * pow ((1 +  intRate), numberPayments)-1))*principal; 

完整的代碼如下。

#include <iostream> 
#include <string> 
#include <cmath> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    double principal, intRate, paybackAmount, interestPaid, monthlyPayment; 
    int numberPayments; 

    // Change the panel color. 
    system ("color F0"); 

    cout << "\n"; 
    cout << "This application will calculate your loan amounts." << endl; 
    cout << "Please enter the data below." << endl; 
    cout << "\n"; 

    cout << "Loan Amount: "; 
    cin >> principal; 
    cout << "Monthly Interest Rate: "; 
    cin >> intRate; 
    cout << "Number of Payments: "; 
    cin >> numberPayments; 


    cout << "\n\n\n"; 

    monthlyPayment = (intRate * pow ((1 + intRate), numberPayments)/(intRate *  pow ((1 + intRate), numberPayments)-1))*principal; 
    paybackAmount = monthlyPayment * numberPayments; 

    cout << fixed << setprecision(2) << showpoint << left << setw(24) << "Loan  Amount:" << "$" << setw(11) << right << principal << endl; 
    cout << fixed << setprecision(1) << showpoint<< left << setw(24) << "Monthly  Interest Rate:" << setw(11) << right << intRate << "%" << endl; 
    cout << fixed << setprecision(0) << left << setw(24) << "Number of Payments:"  << setw(12) << right << numberPayments << endl; 
    cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Monthly  Payment:" << "$" << setw(11) << right << monthlyPayment << endl; 
    cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Amount  Paid Back:" << "$" << setw(11) << right << paybackAmount << endl; 
    cout << fixed << setprecision(2) << showpoint<< left << setw(24) << "Interest  Paid:" << "$" << right << setw(11) << paybackAmount - principal << "\n\n" << endl; 


    system("pause"); 
} 

在此先感謝您的幫助!

+1

此無關的問題,但經過一段時間'使用命名空間的這個習慣性病;'。參考'cout'作爲'std :: cout'。使用合格的名字將爲您節省大量的痛苦。 – 2013-02-20 17:43:43

+0

@PeteBecker可能每個使用'std'的C++程序都包含'using namespace std;'。這可能不是一個很好的做法(對於大型項目而言,這是不負責任的行爲),但它肯定會使代碼更漂亮。 – Dukeling 2013-02-20 18:19:55

+0

@Dukeling--美在觀察者的眼中。 – 2013-02-20 18:28:06

回答

1

根據您的等式,您將分子和分母乘以intRate,此時應該只乘以分子。

你也從第二個pow的結果減去1,而不是從numberPayments減去1。

(intRate * pow ((1 + intRate), numberPayments)-1) 
//^Why is this here?   Wrong place^

你真正想要的是:

monthlyPayment = (intRate * pow(1+intRate, numberPayments)/
          pow(1+intRate, numberPayments-1)) * principal; 
+0

這是一個頭腦發熱的錯誤。謝謝。但是,計算仍然是錯誤的?我得到... 貸款金額:$ 10000.00包裝 月利率:1.0% 付款次數:36 每月支付:$ 10000.00包裝 金額償還:$ 360000.00 付利息:$ 350000.00 – 2013-02-20 17:17:51

+0

@KevinSchultz參見編輯。還有第二個錯誤。 – 2013-02-20 17:19:19

+0

@Loki Astari - 男士感謝您的幫助!我做了改變,但仍然拿出了錯誤的數學。現在我得到....貸款金額:10000美元。00 月利率:1.0% 付款次數:36 每月支付:$ 20000.00 金額償還:$ 720000.00 付利息:$ 710000.00 – 2013-02-20 17:33:10

0

我認爲你有額外的INTRATE

`(intRate * pow ((1 +  intRate), numberPayments)-1))*principal;` 
    ^^^^^^^^ 
0

似乎是一個額外的參數中已經下滑:

((1+intRate)^N-1)) * L => (intRate * pow ((1 + intRate), numberPayments)-1))*principal 
//       ^^^^^^^^^ Look Odd. 

另外:

(1+intRate)^N-1 

您表示這是:

pow ((1 + intRate), numberPayments)-1 

我會做的是不同的:

pow ((1 + intRate), (numberPayments-1))