净现值计算
#include <iostream>
#include <iomanip>
#include <cmath>
const int PROJECTS = 6;
const int YEARS = 4;
int main() {
// 创建二维数组储存每个项目每年利润
int money[PROJECTS][YEARS] = {
{-100000, -1000000, -100000, -120000},
{10000, 200000, 30000, 30000},
{10000, 200000, 30000, 30000},
{10000, 200000, 30000, 30000},
{20000, 200000, 30000, 30000},
{100000, 300000, 30000, 75000}
};
// 贴现率数组 贴现因子
double discountRates[] = {0.08, 0.12};
double discountFactors[2][PROJECTS];
// 贴现因子计算
for (int i = 0; i < 2; i++) {
for (int j = 0; j < PROJECTS; j++) {
// 计算贴现因子
discountFactors[i][j] = 1 / pow(1 + discountRates[i], j);
// 将因子四舍五入
discountFactors[i][j] = round(discountFactors[i][j] * 10000) / 10000.0;
// 输出
std::cout << (discountRates[i] == 0.08 ? "0.08时" : "0.12时")
<< ",第" << j << "年的贴现因子为 : " << discountFactors[i][j] << std::endl;
}
}
// 净现值计算
for (int i = 0; i < 2; i++) {
for (int project = 0; project < YEARS; project++) {
double netPresentValue = 0;
for (int year = 0; year < PROJECTS; year++) {
// 计算每年净现值
netPresentValue += money[year][project] * discountFactors[i][year];
}
// 输出
std::cout << (discountRates[i] == 0.08 ? "0.08时" : "0.12时")
<< ",第" << (project + 1) << "个的净现值为 : " << netPresentValue << std::endl;
}
}
return 0;
}
标签:总结,30000,int,3.11,每日,++,discountFactors,year,PROJECTS From: https://www.cnblogs.com/2351920019xin/p/18067037