我正在为作业分配创建一个小程序。该程序正确运行,但计算不正确。计算汽车支付金额无法正确计算
我使用来计算付款量的公式是:
支付=(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