c语言写出请输入功能序列一 查询余额二取款三存款是退卡
时间: 2024-10-23 10:13:35 浏览: 13
C语言输入一个整数序列并计算他们的值.rar_C语言输入一个整数序列并计算他们的值_输入序列C语言
5星 · 资源好评率100%
在C语言中编写这个简单的命令行交互程序,我们可以使用结构体、循环以及switch-case语句来实现输入选择的功能。下面是一个基本的示例:
```c
#include <stdio.h>
typedef enum {
ACTION_QUERY_BALANCE,
ACTION_WITHDRAW,
ACTION_DEPOSIT,
ACTION_EXIT
} TransactionAction;
struct Account {
float balance;
};
void query_balance(struct Account *account) {
printf("查询余额: %.2f\n", account->balance);
}
void withdraw(struct Account *account, float amount) {
if (amount <= account->balance) {
account->balance -= amount;
printf("取款成功, 新余额: %.2f\n", account->balance);
} else {
printf("余额不足, 取款失败\n");
}
}
void deposit(struct Account *account, float amount) {
account->balance += amount;
printf("存款成功, 新余额: %.2f\n", account->balance);
}
int main() {
struct Account acc = {0.0};
int choice;
while (1) {
printf("\n请选择操作:\n"
"1. 查询余额\n"
"2. 取款\n"
"3. 存款\n"
"4. 退出\n"
"> ");
scanf("%d", &choice);
switch (choice) {
case ACTION_QUERY_BALANCE:
query_balance(&acc);
break;
case ACTION_WITHDRAW:
float withdrawal_amount;
printf("请输入取款金额: ");
scanf("%f", &withdrawal_amount);
withdraw(&acc, withdrawal_amount);
break;
case ACTION_DEPOSIT:
float deposit_amount;
printf("请输入存款金额: ");
scanf("%f", &deposit_amount);
deposit(&acc, deposit_amount);
break;
case ACTION_EXIT:
printf("感谢使用,再见!\n");
return 0;
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}
```
阅读全文