c++ - 复利算法产生奇怪的答案? (已经进行了 5-7 小时的故障排除)

标签 c++

C++ 的新手。尽我最大的努力来解决这个问题。

read 语句后的额外 cout 用于故障排除,以确保它确实在读取用户输入。

我觉得这是一个愚蠢的问题,但我一直在搜索论坛以找到与我的问题类似的问题。

我这辈子都想不出如何让这个程序开始计算正确的利息。与在线利息计算器进行比较时,它得出的数字与程序计算的数字截然不同。

我花了很多时间解决这个问题并发短信给不同的算法,但我还没有脱颖而出。我通常不喜欢寻求帮助,因为我通过挣扎来学习,但这个让我陷入困境......

所以我认为我可能在使用的库中遇到问题或计算错误。我知道代码有点乱,我正在学习如何清理它,但就目前而言,我只想让代码根据用户输入计算复利。我添加了注释来解释每一段代码的作用,以帮助解决我的困惑问题。

我不认为这是一个语法错误,但我要么在算法中使用了一些逗号,要么我的括号有些错误。

我使用的公式是,

A = P (1 + r/n)^(nt)

Amount = principle (1 + interest rate/times compounded)^(rate, times compounded)

#include <iostream>
#include <cmath>


using namespace std;

int main()
{

    double rate;
    double time;
    double principle;
    double amount;
    double amount2;

//Asking for the amount of money that the user would like to invest

    cout << "What is the amount of money you would like to invest? ";

    cin >> principle;
    cout << principle << endl;

//Asking for the interest rate that will be compounded annually

    cout << "What is the interest rate you would like to calculate? "; 
    cin >> rate;

//interest rate divided by 100 so it can be multiplied by the principle and the amount of time the money will be invested later in code.

    rate /= 100;
    cout << rate << endl;

//Asking for the amount of years that the user will invest. 
cout << "How many times would you like to compound your money? ";

    cin >> time;
    cout << time << endl;

//calculation for amount of money that will be made after all user input is input

    amount2 = pow(rate, time);
    cout << amount2 << endl;

    amount = (principle * (1 + rate/time), amount2); 


//Output data after all user data is input and calculated. 

    cout << "Your will have "; cout << amount; cout << " dollars in 
    interest! "<< endl;  

    return 0;

}

最佳答案

这一行:

amount = (principle * (1 + rate/time), amount2); 

完全不使用 principle * (1 + rate/time)。对该表达式求值,然后丢弃结果,将 amount 分配给 amount2 中的值。我假设您正在尝试使用这两个表达式作为参数来调用函数。

http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator

In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before evaluation of the expression E2 begins.

现在我们有了您的公式:

A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate * times compounded)

我不确定您是在计算上面列出的公式,请注意对第二行的更正,我将其中的逗号替换为乘法。

amount2 = pow(rate, time);

计算rate^time,你应该有:

amount2 = 1 * time; // Where one is the times compounded per period (year)

这样你的另一行可以读到:

amount = principle * pow((1 + rate, amount2);

因为您每年复利一次,所以您将利率除以 1,而不是年数。同样的道理,amount2现在严格等于时间;

关于c++ - 复利算法产生奇怪的答案? (已经进行了 5-7 小时的故障排除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42014988/

相关文章:

c++ - cpp新手问题: error: conversion to ‘std::array<int, 5>::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result

c++ - 在共享内存 C++ 中实例化对象

c++ - 编程基本的应用程序内内存分析 : how to count pointers that are created?

c++ - 不兼容的函数声明

c++ - 为什么在返回对象 Mat 时堆损坏?

c++ gmock使用相同的args调用其他函数

c++ - 寻求 Linux 的 C++ 指标工具

c++ - 我不明白这个 : terminate called after throwing an instance of 'std::length_error'

c++ - C++中结构/类的历史是什么?

c++ - 如何防止 opengl 绘图拉伸(stretch)到窗口大小?