用c语言编写一个银行账户管理系统,允许用户执行以下操作: [1]存款:用户输入存款金额,更新账户余额。 [2]取款:用户输入取款金额,如果余额不足,则提示“余额不足”,否则扣减账户余额。 [3]查询余额:输出当前账户余额。
时间: 2024-10-23 19:20:18 浏览: 30
C语言实现银行管理系统
在C语言中创建一个简单的银行账户管理系统,你可以使用结构体来表示账户信息,并通过函数来处理用户的操作。首先,我们定义一个`Account`结构体:
```c
typedef struct {
float balance;
} Account;
// 定义函数来处理用户操作
void deposit(Account *account, float amount) {
account->balance += amount;
printf("Deposit successful. New balance is %.2f.\n", account->balance);
}
void withdraw(Account *account, float amount) {
if (amount <= account->balance) {
account->balance -= amount;
printf("Withdrawal successful. New balance is %.2f.\n", account->balance);
} else {
printf("Insufficient balance. Withdrawal denied.\n");
}
}
void check_balance(Account *account) {
printf("Current balance is %.2f.\n", account->balance);
}
```
接下来,你可以设计一个主程序,用于接收用户的命令并调用相应的函数:
```c
int main() {
Account account = {0}; // 初始余额为0
char command[50];
while (1) {
printf("\nChoose an operation:\n1. Deposit\n2. Withdraw\n3. Check Balance\n4. Exit\n");
scanf("%s", command);
if (strcmp(command, "Exit") == 0)
break;
switch (command[0]) {
case '1':
float deposit_amount;
printf("Enter deposit amount: ");
scanf("%f", &deposit_amount);
deposit(&account, deposit_amount);
break;
case '2':
float withdraw_amount;
printf("Enter withdrawal amount: ");
scanf("%f", &withdraw_amount);
withdraw(&account, withdraw_amount);
break;
case '3':
check_balance(&account);
break;
default:
printf("Invalid command. Please try again.\n");
break;
}
}
return 0;
}
```
阅读全文