2013-02-20 78 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))