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");
}

在此先感谢您的帮助!

4

3 回答 3

1

intRate根据您的方程式,当您只应将分子相乘时,您将分子和分母都乘以 。

您还从第二个的结果中减去 1pow而不是从numberPayments.

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

你真正想要的是:

monthlyPayment = (intRate * pow(1+intRate, numberPayments) /
                            pow(1+intRate, numberPayments-1)) * principal;
于 2013-02-20T17:14:19.003 回答
0

我认为你有额外的 intRate

  `(intRate * pow ((1 +     intRate), numberPayments)-1))*principal;`
   ^^^^^^^^
于 2013-02-20T17:14:38.280 回答
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))
于 2013-02-20T17:23:19.070 回答