用 C 语言计算单利

CCBeginner
立即练习

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

简介

在本实验中,你将学习如何在C编程中计算单利。该实验涵盖了从用户读取本金、利率和时间段的逐步过程,然后使用公式“利息 = 本金 × 利率 × 时间”计算单利。程序随后将显示计算出的利息金额。

该实验提供了一种清晰简洁的方法,用于理解金融数学和使用C编程进行利息计算的基本概念。在本实验结束时,你将对如何在C中实现单利计算以及如何将这些技能应用于更复杂的金融应用有扎实的理解。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) 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-435352{{"用 C 语言计算单利"}} c/math_functions -.-> lab-435352{{"用 C 语言计算单利"}} c/user_input -.-> lab-435352{{"用 C 语言计算单利"}} c/output -.-> lab-435352{{"用 C 语言计算单利"}} end

读取本金、利率、时间

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

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

cd ~/project
nano simple_interest.c

现在,输入以下代码来读取本金、利率和时间:

#include <stdio.h>

int main() {
    float principal, rate, time;

    // 提示用户输入本金金额
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // 提示用户输入年利率
    printf("Enter the annual interest rate (%): ");
    scanf("%f", &rate);

    // 提示用户输入时间段
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // 打印输入的值
    printf("\nInput Values:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Interest Rate: %.2f%%\n", rate);
    printf("Time Period: %.2f years\n", time);

    return 0;
}

编译并运行程序:

gcc simple_interest.c -o simple_interest
./simple_interest

示例输出:

Enter the principal amount: 1000
Enter the annual interest rate (%): 5
Enter the time period (in years): 2

Input Values:
Principal: $1000.00
Interest Rate: 5.00%
Time Period: 2.00 years
解释
  • 我们使用 float 数据类型来存储本金、利率和时间的十进制值
  • printf() 用于向用户显示提示和说明
  • scanf() 读取每个变量的用户输入
  • %.2f 格式说明符显示带有两位小数的浮点数

计算利息 = 本金×利率×时间

在这一步中,你将学习如何使用公式“利息 = 本金×利率×时间”来计算单利。我们将修改上一个程序以计算利息金额。

打开现有的C文件:

cd ~/project
nano simple_interest.c

用利息计算更新程序:

#include <stdio.h>

int main() {
    float principal, rate, time, interest;

    // 提示用户输入本金金额
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // 提示用户输入年利率
    printf("Enter the annual interest rate (%): ");
    scanf("%f", &rate);

    // 提示用户输入时间段
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // 计算单利
    interest = principal * (rate / 100) * time;

    // 打印输入值和计算出的利息
    printf("\nInput Values:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Interest Rate: %.2f%%\n", rate);
    printf("Time Period: %.2f years\n", time);
    printf("\nCalculated Simple Interest: $%.2f\n", interest);

    return 0;
}

编译并运行更新后的程序:

gcc simple_interest.c -o simple_interest
./simple_interest

示例输出:

Enter the principal amount: 1000
Enter the annual interest rate (%): 5
Enter the time period (in years): 2

Input Values:
Principal: $1000.00
Interest Rate: 5.00%
Time Period: 2.00 years

Calculated Simple Interest: $100.00
解释
  • 我们添加了一个新变量 interest 来存储计算出的单利
  • 单利公式为:利息 = 本金×(利率/100)×时间
  • 我们将利率除以100以将百分比转换为小数
  • 计算出的利息保留两位小数打印出来

打印利息

在这一步中,你将学习如何以清晰、专业的输出格式来格式化并打印计算出的单利。我们将对上一个程序进行增强,以提供更全面的财务计算显示。

打开现有的C文件:

cd ~/project
nano simple_interest.c

用改进后的利息打印功能更新程序:

#include <stdio.h>

int main() {
    float principal, rate, time, interest;

    // 提示用户输入本金金额
    printf("单利计算器\n");
    printf("-------------------------\n");
    printf("请输入本金金额:");
    scanf("%f", &principal);

    // 提示用户输入年利率
    printf("请输入年利率(%):");
    scanf("%f", &rate);

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

    // 计算单利
    interest = principal * (rate / 100) * time;

    // 打印详细的财务总结
    printf("\n--- 财务计算总结 ---\n");
    printf("本金金额:     $%10.2f\n", principal);
    printf("年利率:       %10.2f%%\n", rate);
    printf("时间段:         %10.2f 年\n", time);
    printf("总单利:       $%10.2f\n", interest);
    printf("总金额:       $%10.2f\n", principal + interest);

    return 0;
}

编译并运行更新后的程序:

gcc simple_interest.c -o simple_interest
./simple_interest

示例输出:

单利计算器
-------------------------
请输入本金金额:1000
请输入年利率(%):5
请输入时间段(以年为单位):2

--- 财务计算总结 ---
本金金额:     $    1000.00
年利率:       5.00%
时间段:         2.00 年
总单利:       $     100.00
总金额:       $    1100.00
解释
  • 添加了标题和分隔符以提升用户体验
  • 使用 %10.2f 格式说明符来对齐十进制数
  • 显示了如总金额等额外信息
  • 提高了财务计算输出的可读性

总结

在本实验中,你将学习如何从用户输入中读取本金、利率和时间,然后使用公式“利息 = 本金×利率×时间”计算单利。你还将学习如何打印计算出的利息值。关键步骤包括提示用户输入财务参数、进行利息计算以及显示最终结果。

该程序首先从用户那里读取本金金额、年利率和时间段。然后,它使用提供的公式计算单利并打印结果。本实验涵盖了C编程语言中输入/输出、算术运算和输出格式化的基本概念。