mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-04-13 17:49:46 +08:00
4-4
This commit is contained in:
35
code/practise/08 数组/01 计算利息/main.c
Normal file
35
code/practise/08 数组/01 计算利息/main.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include<stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
18
code/practise/08 数组/01 计算利息/readme.md
Normal file
18
code/practise/08 数组/01 计算利息/readme.md
Normal file
@@ -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 语句,一个控制年份,一个控制不同的利率。
|
||||
Reference in New Issue
Block a user