mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-02-12 06:55:01 +08:00
4-3
This commit is contained in:
70
code/practise/06 循环/01 账簿计算/main.c
Normal file
70
code/practise/06 循环/01 账簿计算/main.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include<stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
double balance = 0; // 余额
|
||||
double credit, debit;
|
||||
|
||||
// 菜单,形式可以自己设计,尽量美观一点嘛,不过不用纠结这种界面,不要舍本逐末。
|
||||
|
||||
printf("**** ACME checkbook-balancing program ****\n");
|
||||
printf(" Comands: \n");
|
||||
printf(" 0 = clear \n");
|
||||
printf(" 1 = credit \n");
|
||||
printf(" 2 = debit \n");
|
||||
printf(" 3 = balance \n");
|
||||
printf(" 4 = exit \n");
|
||||
|
||||
// 题目中已经规定了这些功能用 0,1,2,3,4 代替,其实是想让我们用 switch
|
||||
// 如果你想用 if else 也完全 ok
|
||||
|
||||
// 死循环让用户可以重复选择
|
||||
while (1) {
|
||||
int choice;
|
||||
printf("Enter command: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
|
||||
// 清除账户是一种很“危险”的举动,可以让用户确认一次
|
||||
|
||||
case 0: printf("Are you sure to clear your balance?\n");
|
||||
printf("1 = yes, 0 = no\n");
|
||||
int isClear;
|
||||
scanf("%d", &isClear);
|
||||
if (isClear == 1) {
|
||||
balance = 0;
|
||||
printf("clear successfully!\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: printf("Enter amount of credit: ");
|
||||
scanf("%lf", &credit);
|
||||
balance += credit;
|
||||
break;
|
||||
|
||||
case 2: printf("Enter amount of debit : ");
|
||||
scanf("%lf", &debit);
|
||||
balance -= debit;
|
||||
break;
|
||||
|
||||
case 3: printf("Current balance: %.2f\n", balance);
|
||||
break;
|
||||
|
||||
case 4: printf("Are you sure to quit?\n");
|
||||
printf("1 = yes, 0 = no\n");
|
||||
int isQuit;
|
||||
scanf("%d", &isQuit);
|
||||
if (isQuit == 1) {
|
||||
printf("Goodbye~\n");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
default: printf("Illeagl option!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
code/practise/06 循环/01 账簿计算/readme.md
Normal file
22
code/practise/06 循环/01 账簿计算/readme.md
Normal file
@@ -0,0 +1,22 @@
|
||||
#### 程序:账薄结算
|
||||
|
||||
这个程序帮你理解一种简单的交互式程序设计,我们可以通过这种方式设计菜单。
|
||||
|
||||
题目:
|
||||
|
||||
开发一个程序用来维护账簿的余额。程序将为用户提供选择菜单:清空余额账户,向账户存钱,从账户取钱,显示当前余额,退出程序。选项用 0,1,2,3,4表示。程序的会话类似这样:
|
||||
|
||||
```c
|
||||
**** ACME checkbook-balancing program ****
|
||||
Comands: 0 = clear, 1 = credit, 2 = debit, 3 = balance, 4 = exit
|
||||
|
||||
Enter command: 1
|
||||
Enter amount of credit: 1042.56
|
||||
Enter command: 2
|
||||
Enter amount of debit : 133.56
|
||||
Enter command: 3
|
||||
Current balance: 909
|
||||
Ener command: 4
|
||||
Goodbye~
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user