在这一步中,你将学习如何在 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格式说明符以两位小数显示数字在这一步中,你将学习如何使用数学公式 A = P * (1 + 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 是时间段。