From f5c53f86bef5bd264055cd8e1e9808e6220208a7 Mon Sep 17 00:00:00 2001 From: hairrrrr <781728963@qq.com> Date: Sat, 4 Apr 2020 16:43:59 +0800 Subject: [PATCH] 4-4 --- code/practise/08 数组/01 计算利息/main.c | 35 +++++++++++++++++++++ code/practise/08 数组/01 计算利息/readme.md | 18 +++++++++++ 2 files changed, 53 insertions(+) create mode 100644 code/practise/08 数组/01 计算利息/main.c create mode 100644 code/practise/08 数组/01 计算利息/readme.md diff --git a/code/practise/08 数组/01 计算利息/main.c b/code/practise/08 数组/01 计算利息/main.c new file mode 100644 index 0000000..c4b53b0 --- /dev/null +++ b/code/practise/08 数组/01 计算利息/main.c @@ -0,0 +1,35 @@ +#include + +#define NUM_RATES (int)sizeof(value) / sizeof(value[0]) +#define INITIAL_BALANCE 100.00 + + +int main(void) { + + int rate; + int year; + double value[5]; + + printf("Enter intrest rate: "); + scanf("%d", &rate); + printf("Enter number of years: "); + scanf("%d", &year); + + printf("\nYears"); + for (int i = 0; i < NUM_RATES; i++) { + printf("%7d%%", rate + i); + value[i] = INITIAL_BALANCE; // ʼ + } + printf("\n"); + + for (int i = 0; i < year; i++) { + printf("%3d ", i); // ոõһкж + for (int j = 0; j < NUM_RATES; j++) { + value[j] += value[j] * (rate + j) / 100; // עﲻҪд + printf("%8.2f", value[j]); + } + printf("\n"); + } + + return 0; +} diff --git a/code/practise/08 数组/01 计算利息/readme.md b/code/practise/08 数组/01 计算利息/readme.md new file mode 100644 index 0000000..2d404b6 --- /dev/null +++ b/code/practise/08 数组/01 计算利息/readme.md @@ -0,0 +1,18 @@ +#### 程序:计算利息 + +编写一个程序显示一个表格。这个表格显示了几年时间内 100 美元投资在不同利率下的价值。用户输入利率和要投资的年数。投资总价值一年算一次,表格将显示输入的利率和紧随其后的 4 个更高的利率下投资的总价值。程序会话如下: + +```c +Enter intrest rate: 6 +Enter number of years: 2 + +Years 6% 7% 8% 9% 10% + 1 106.00 107.00 108.00 109.00 110.00 + 2 112.36 114.49 116.64 118.81 121.00 +``` + +第一行用一个 for 语句来显示。 + +我们在计算第一年的价值的时候将结果存放到数组中,然后使用数组中的结果继续计算下一年的价值。 + +在这一过程中我们将需要两个 for 语句,一个控制年份,一个控制不同的利率。 \ No newline at end of file