我正在爲作業分配創建一個小程序。該程序正確運行,但計算不正確。計算汽車支付金額無法正確計算
我使用來計算付款量的公式是:
支付=(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");
}
在此先感謝您的幫助!
此無關的問題,但經過一段時間'使用命名空間的這個習慣性病;'。參考'cout'作爲'std :: cout'。使用合格的名字將爲您節省大量的痛苦。 – 2013-02-20 17:43:43
@PeteBecker可能每個使用'std'的C++程序都包含'using namespace std;'。這可能不是一個很好的做法(對於大型項目而言,這是不負責任的行爲),但它肯定會使代碼更漂亮。 – Dukeling 2013-02-20 18:19:55
@Dukeling--美在觀察者的眼中。 – 2013-02-20 18:28:06