gpt4 book ai didi

c++ - 计算汽车付款金额计算不正确

转载 作者:行者123 更新时间:2023-11-28 07:45:24 25 4
gpt4 key购买 nike

我正在为家庭作业创建一个小程序。程序运行正常,只是计算不正确。

我用来计算付款金额的公式是:

付款 = (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");
}

在此先感谢您的帮助!

最佳答案

您将分子和分母都乘以 intRate,而根据您的等式,您应该只乘以分子。

您还从第二个 pow 的结果中减去 1,而不是从 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;

关于c++ - 计算汽车付款金额计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com