用 C 语言计算复利

CCBeginner
立即练习

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

简介


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/FunctionsGroup -.-> c/math_functions("Math Functions") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-435342{{"用 C 语言计算复利"}} c/math_functions -.-> lab-435342{{"用 C 语言计算复利"}} c/user_input -.-> lab-435342{{"用 C 语言计算复利"}} c/output -.-> lab-435342{{"用 C 语言计算复利"}} end

读取本金、利率和时间

在这一步中,你将学习如何在C程序中读取用于计算复利的本金金额、利率和时间段。我们将创建一个简单的程序,该程序接受用户输入这些财务参数。

首先,让我们在项目目录中创建一个新的C文件:

cd ~/project
nano compound_interest.c

现在,添加以下代码以读取输入参数:

#include <stdio.h>

int main() {
    // 声明本金、利率和时间的变量
    float principal, rate, time;

    // 提示并读取本金金额
    printf("输入本金金额:");
    scanf("%f", &principal);

    // 提示并读取利率
    printf("输入年利率(以百分比表示):");
    scanf("%f", &rate);

    // 提示并读取时间段
    printf("输入时间段(以年为单位):");
    scanf("%f", &time);

    // 打印输入值以进行验证
    printf("\n输入参数:\n");
    printf("本金金额:$%.2f\n", principal);
    printf("年利率:%.2f%%\n", rate);
    printf("时间段:%.2f年\n", time);

    return 0;
}

编译并运行该程序:

gcc compound_interest.c -o compound_interest
./compound_interest

示例输出:

输入本金金额:1000
输入年利率(以百分比表示):5
输入时间段(以年为单位):3

输入参数:
本金金额:$1000.00
年利率:5.00%
时间段:3.00年
解释
  • 我们使用float变量来存储用于财务计算的小数值
  • printf()用于向用户显示提示和说明
  • scanf()读取用户输入的本金、利率和时间
  • %.2f格式说明符以两位小数显示数字

计算金额 = P * (1 + R) ^ T

在这一步中,你将学习如何使用数学公式A = P * (1 + R) ^ T来计算复利,其中:

  • A = 最终金额
  • P = 本金金额
  • R = 年利率(以小数形式表示)
  • T = 时间段(以年为单位)

让我们修改之前的程序以包含复利计算:

cd ~/project
nano compound_interest.c

用复利计算更新程序:

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

int main() {
    // 声明本金、利率、时间和金额的变量
    float principal, rate, time, amount;

    // 提示并读取本金金额
    printf("输入本金金额:");
    scanf("%f", &principal);

    // 提示并读取年利率
    printf("输入年利率(以百分比表示):");
    scanf("%f", &rate);

    // 提示并读取时间段
    printf("输入时间段(以年为单位):");
    scanf("%f", &time);

    // 将利率转换为小数
    rate = rate / 100;

    // 计算复利
    amount = principal * pow(1 + rate, time);

    // 打印结果
    printf("\n输入参数:\n");
    printf("本金金额:$%.2f\n", principal);
    printf("年利率:%.2f%%\n", rate * 100);
    printf("时间段:%.2f年\n", time);
    printf("\n复利计算:\n");
    printf("最终金额:$%.2f\n", amount);
    printf("赚取的总利息:$%.2f\n", amount - principal);

    return 0;
}

使用数学库编译程序:

gcc compound_interest.c -o compound_interest -lm

运行程序:

./compound_interest

示例输出:

输入本金金额:1000
输入年利率(以百分比表示):5
输入时间段(以年为单位):3

输入参数:
本金金额:$1000.00
年利率:5.00%
时间段:3.00年

复利计算:
最终金额:$1157.63
赚取的总利息:$157.63
解释
  • #include <math.h> 用于使用 pow() 函数进行幂运算
  • rate = rate / 100 将百分比转换为小数
  • amount = principal * pow(1 + rate, time) 计算最终金额
  • 编译时使用 -lm 标志来链接数学库

打印最终金额

在这最后一步中,你将通过添加格式化输出并改进财务结果的呈现方式来增强复利计算程序。

让我们修改之前的程序以改进结果格式化:

cd ~/project
nano compound_interest.c

用增强的输出格式化更新程序:

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

int main() {
    // 声明本金、利率、时间、金额和利息的变量
    float principal, rate, time, amount, interest;

    // 提示并读取本金金额
    printf("复利计算器\n");
    printf("----------------------------\n");
    printf("输入本金金额:");
    scanf("%f", &principal);

    // 提示并读取年利率
    printf("输入年利率(以百分比表示):");
    scanf("%f", &rate);

    // 提示并读取时间段
    printf("输入时间段(以年为单位):");
    scanf("%f", &time);

    // 将利率转换为小数
    rate = rate / 100;

    // 计算复利
    amount = principal * pow(1 + rate, time);
    interest = amount - principal;

    // 打印格式化的财务结果
    printf("\n===== 财务计算结果 =====\n");
    printf("初始本金:     $%10.2f\n", principal);
    printf("年利率:       %10.2f%%\n", rate * 100);
    printf("投资期限:     %10.2f年\n", time);
    printf("-------------------------------------------\n");
    printf("最终金额:     $%10.2f\n", amount);
    printf("赚取的总利息: $%10.2f\n", interest);
    printf("==========================================\n");

    return 0;
}

编译程序:

gcc compound_interest.c -o compound_interest -lm

运行程序:

./compound_interest

示例输出:

复利计算器
----------------------------
输入本金金额:1000
输入年利率(以百分比表示):5
输入时间段(以年为单位):3

===== 财务计算结果 =====
初始本金:     $   1000.00
年利率:       5.00%
投资期限:     3.00年
-------------------------------------------
最终金额:     $   1157.63
赚取的总利息: $    157.63
==========================================
解释
  • 添加了更具描述性的标题和分隔符
  • 使用 %10.2f 格式说明符进行对齐的、固定宽度的小数输出
  • 单独计算利息以使其呈现更清晰
  • 包含一个标题并以结构化方式显示财务结果

总结

在这个实验中,你学习了如何在C程序中读取用于计算复利的本金金额、利率和时间段。你创建了一个简单的程序,该程序提示用户输入这些财务参数,并打印输入值以进行验证。接下来,你将学习如何使用数学公式A = P * (1 + R)^T来计算复利,其中A是最终金额,P是本金金额,R是利率,T是时间段。