用 C 语言计算净现值(NPV)

CCBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何用 C 语言计算净现值(Net Present Value,NPV)。本实验将指导你完成读取一系列现金流和折现率、对每个现金流进行折现,然后将它们相加来计算净现值的过程。你将创建一个 C 程序,允许用户输入必要的数据,然后显示计算出的净现值。本实验涵盖了金融数学的基本概念以及使用 C 编程进行利息计算的内容。

读取现金流序列和利率

在这一步中,你将学习如何在 C 语言中读取一系列现金流和利率,用于计算净现值(Net Present Value,NPV)。我们将创建一个程序,允许用户输入多个现金流和折现率。

首先,让我们为净现值计算创建一个新的 C 文件:

cd ~/project
nano npv_calculator.c

现在,将以下代码添加到文件中:

#include <stdio.h>

#define MAX_CASH_FLOWS 10

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    double discount_rate;
    int num_cash_flows;

    // 输入现金流的数量
    printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    // 验证现金流的数量
    if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
        printf("Invalid number of cash flows.\n");
        return 1;
    }

    // 输入现金流
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Enter cash flow %d: ", i);
        scanf("%lf", &cash_flows[i]);
    }

    // 输入折现率
    printf("Enter the discount rate (as a decimal): ");
    scanf("%lf", &discount_rate);

    // 打印输入内容以进行验证
    printf("\nInput Summary:\n");
    printf("Number of Cash Flows: %d\n", num_cash_flows);
    printf("Discount Rate: %.2f%%\n", discount_rate * 100);

    printf("\nCash Flows:\n");
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Cash Flow %d: $%.2f\n", i, cash_flows[i]);
    }

    return 0;
}

编译程序:

gcc -o npv_calculator npv_calculator.c

运行程序以测试输入:

./npv_calculator

示例输出:

Enter the number of cash flows (max 10): 3
Enter cash flow 0: -1000
Enter cash flow 1: 500
Enter cash flow 2: 600
Enter the discount rate (as a decimal): 0.1

Input Summary:
Number of Cash Flows: 3
Discount Rate: 10.00%

Cash Flows:
Cash Flow 0: $-1000.00
Cash Flow 1: $500.00
Cash Flow 2: $600.00

这段代码演示了如何:

  1. 定义最大现金流数量
  2. 从用户输入中读取现金流的数量
  3. 输入各个现金流值
  4. 输入折现率
  5. 验证并显示输入内容以进行验证

该程序通过捕获基本输入参数,为净现值计算奠定了基础。

对每个现金流进行折现并求和

在这一步中,你将学习如何对每个现金流进行折现,并通过将折现后的现金流相加来计算净现值(Net Present Value,NPV)。

让我们修改之前的 npv_calculator.c 文件以添加净现值计算:

cd ~/project
nano npv_calculator.c

用以下实现替换之前的代码:

#include <stdio.h>
#include <math.h>

#define MAX_CASH_FLOWS 10

double calculate_npv(double cash_flows[], int num_cash_flows, double discount_rate) {
    double npv = 0.0;

    for (int i = 0; i < num_cash_flows; i++) {
        // 对每个现金流进行折现:CF / (1 + r)^t
        double discounted_cash_flow = cash_flows[i] / pow(1 + discount_rate, i);
        npv += discounted_cash_flow;
    }

    return npv;
}

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    double discount_rate;
    int num_cash_flows;

    // 输入现金流的数量
    printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    // 验证现金流的数量
    if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
        printf("Invalid number of cash flows.\n");
        return 1;
    }

    // 输入现金流
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Enter cash flow %d: ", i);
        scanf("%lf", &cash_flows[i]);
    }

    // 输入折现率
    printf("Enter the discount rate (as a decimal): ");
    scanf("%lf", &discount_rate);

    // 计算净现值
    double npv = calculate_npv(cash_flows, num_cash_flows, discount_rate);

    // 打印结果
    printf("\nInput Summary:\n");
    printf("Number of Cash Flows: %d\n", num_cash_flows);
    printf("Discount Rate: %.2f%%\n", discount_rate * 100);

    printf("\nCash Flows:\n");
    for (int i = 0; i < num_cash_flows; i++) {
        double discounted_cf = cash_flows[i] / pow(1 + discount_rate, i);
        printf("Cash Flow %d: $%.2f (Discounted: $%.2f)\n",
               i, cash_flows[i], discounted_cf);
    }

    printf("\nNet Present Value (NPV): $%.2f\n", npv);

    return 0;
}

使用数学库编译程序:

gcc -o npv_calculator npv_calculator.c -lm

运行程序以测试净现值计算:

./npv_calculator

示例输出:

Enter the number of cash flows (max 10): 3
Enter cash flow 0: -1000
Enter cash flow 1: 500
Enter cash flow 2: 600
Enter the discount rate (as a decimal): 0.1

Input Summary:
Number of Cash Flows: 3
Discount Rate: 10.00%

Cash Flows:
Cash Flow 0: $-1000.00 (Discounted: $-1000.00)
Cash Flow 1: $500.00 (Discounted: $454.55)
Cash Flow 2: $600.00 (Discounted: $495.87)

Net Present Value (NPV): $-49.58

此实现中的关键点:

  1. 添加了 calculate_npv() 函数来计算净现值
  2. 使用 pow() 函数对折现现金流
  3. 显示原始现金流和折现后的现金流
  4. 计算并显示最终的净现值

注意:-lm 标志用于链接数学库以使用 pow() 函数。

打印净现值

在这最后一步中,你将学习如何以用户友好的格式解释和展示净现值(Net Present Value,NPV)的计算结果。

让我们修改 npv_calculator.c 以增强输出展示:

cd ~/project
nano npv_calculator.c

用改进后的输出格式更新代码:

#include <stdio.h>
#include <math.h>

#define MAX_CASH_FLOWS 10

double calculate_npv(double cash_flows[], int num_cash_flows, double discount_rate) {
    double npv = 0.0;

    for (int i = 0; i < num_cash_flows; i++) {
        double discounted_cash_flow = cash_flows[i] / pow(1 + discount_rate, i);
        npv += discounted_cash_flow;
    }

    return npv;
}

void print_npv_analysis(double cash_flows[], int num_cash_flows, double discount_rate, double npv) {
    printf("\n--- NPV 分析报告 ---\n");
    printf("折现率:%.2f%%\n", discount_rate * 100);

    printf("\n现金流明细:\n");
    printf("--------------------\n");
    for (int i = 0; i < num_cash_flows; i++) {
        double discounted_cf = cash_flows[i] / pow(1 + discount_rate, i);
        printf("时期 %d:$%.2f(折现后:$%.2f)\n",
               i, cash_flows[i], discounted_cf);
    }

    printf("\n净现值(NPV):$%.2f\n", npv);

    // 解释 NPV 结果
    if (npv > 0) {
        printf("投资建议:接受\n");
        printf("该项目预计会创造价值。\n");
    } else if (npv < 0) {
        printf("投资建议:拒绝\n");
        printf("该项目预计会破坏价值。\n");
    } else {
        printf("投资建议:中性\n");
        printf("该项目在给定折现率下收支平衡。\n");
    }
}

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    double discount_rate;
    int num_cash_flows;

    printf("净现值(NPV)计算器\n");
    printf("==================================\n");

    // 输入现金流的数量
    printf("输入现金流的数量(最大 %d):", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    // 验证现金流的数量
    if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
        printf("现金流数量无效。\n");
        return 1;
    }

    // 输入现金流
    for (int i = 0; i < num_cash_flows; i++) {
        printf("输入时期 %d 的现金流:", i);
        scanf("%lf", &cash_flows[i]);
    }

    // 输入折现率
    printf("输入折现率(以小数形式):");
    scanf("%lf", &discount_rate);

    // 计算 NPV
    double npv = calculate_npv(cash_flows, num_cash_flows, discount_rate);

    // 打印 NPV 分析
    print_npv_analysis(cash_flows, num_cash_flows, discount_rate, npv);

    return 0;
}

编译程序:

gcc -o npv_calculator npv_calculator.c -lm

运行程序以测试增强后的 NPV 输出:

./npv_calculator

示例输出:

净现值(NPV)计算器
==================================
输入现金流的数量(最大10):3
输入时期0的现金流:-1000
输入时期1的现金流:500
输入时期2的现金流:600
输入折现率(以小数形式):0.1

--- NPV分析报告 ---
折现率:10.00%

现金流明细:
--------------------
时期0:$-1000.00(折现后:$-1000.00)
时期1:$500.00(折现后:$454.55)
时期2:$600.00(折现后:$495.87)

净现值(NPV):$-49.58
投资建议:拒绝
该项目预计会破坏价值。

此版本的主要改进:

  1. 添加了一个专用的 print_npv_analysis() 函数
  2. 增强了输出格式
  3. 根据 NPV 添加了投资建议
  4. 通过清晰的标题改进了用户界面

总结

在本实验中,你学习了如何在 C 语言中读取一系列现金流和利率,用于计算净现值(Net Present Value,NPV)。你创建了一个程序,允许用户输入多个现金流和折现率,然后程序会打印输入内容以供验证。

接下来,你将学习如何对每个现金流进行折现并将它们相加来计算净现值,然后打印最终结果。