如果没有数学库,我如何在C++中创建复利公式
How can I create the compound interest formula in C++ without a math library?
我一直试图完成一项需要运行复利公式的作业,但我只能使用iomainip和iostream库。
do (cout << BaseMembershipFee * pow((ONE+(GoldRate/CompoundMonthly),(CompoundMonthly*years)) << years++) << endl;
while (years < 11);
这适用于数学库(我想(,但我不知道如何在没有pow的情况下制作它。
变量:
const float BaseMembershipFee = 1200, GoldRate = 0.01, CompoundMonthly = 12, ONE = 1;
float years = 1;
在您的问题中,它指出年份=1。根据do while((循环,我怀疑您的意思是显示年份=10。
下面的代码有一个函数可以计算每个期间的复利,在您的情况下是每个月。
然后,因为看起来你被要求在每年年底计算中期金额,我们有第二个循环。
#include<iostream>
float CompountInterestMultiplier(float ratePerPeriod, float periods) {
float multiplier = 1.0;
for (int i = 0; i < periods; i++) {
multiplier *= (1.0 + ratePerPeriod);
}
return multiplier;
}
int main() {
const float BaseMembershipFee = 1200;
const float GoldRate = 0.01; // represents annual rate at 1.0%
const float periodsPerYear = 12.0; // number of compounds per year. Changed the name of this from compoundsMonthly
const float ONE = 1.0; // Did not use this
const float years = 10.0; // I think you have a typo
// and wanted this to be 10
float ratePerPeriod = GoldRate / periodsPerYear;
// If you want to print the value at the end of each year,
// then you would do this:
float interimValue = BaseMembershipFee;
for (int year = 0; year < 11; year++) {
interimValue = BaseMembershipFee *
CompountInterestMultiplier(ratePerPeriod, periodsPerYear * year);
std::cout << "Value at end of year " << year << ": $" << interimValue << std::endl;
}
}
Here is the output:
Value at end of year 0: $1200
Value at end of year 1: $1212.06
Value at end of year 2: $1224.23
Value at end of year 3: $1236.53
Value at end of year 4: $1248.95
Value at end of year 5: $1261.5
Value at end of year 6: $1274.17
Value at end of year 7: $1286.97
Value at end of year 8: $1299.9
Value at end of year 9: $1312.96
Value at end of year 10: $1326.15
Process finished with exit code 0
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息