c语言银行存取款程序
时间: 2024-06-30 10:00:34 浏览: 5
在C语言中,创建一个简单的银行账户存取款程序,通常会包含以下步骤:
1. 定义结构体(Structure):创建一个表示账户的结构体,包含成员变量如账户号(account_number)、姓名(name)、初始余额(balance)等。
```c
typedef struct {
int account_number;
char name[50];
float balance;
} BankAccount;
```
2. 函数声明:定义函数来处理存取款操作,比如存款(deposit)、取款(withdraw)、打印余额(print_balance)等。
```c
void deposit(BankAccount*, float amount);
void withdraw(BankAccount*, float amount);
void print_balance(BankAccount*);
```
3. 功能实现:
- 存款函数会增加账户余额。
- 取款函数检查余额是否足够,并在足够时减少余额。
- 打印余额函数简单地显示当前的账户余额。
```c
void deposit(BankAccount* acc, float amount) {
acc->balance += amount;
}
void withdraw(BankAccount* acc, float amount) {
if (amount <= acc->balance) {
acc->balance -= amount;
} else {
printf("Insufficient balance.\n");
}
}
void print_balance(BankAccount* acc) {
printf("Account number: %d, Name: %s, Balance: %.2f\n",
acc->account_number, acc->name, acc->balance);
}
```
4. 主函数(main()):创建一个账户实例并执行用户交互,如循环接收输入直到用户选择退出。
```c
int main() {
BankAccount customer;
char choice;
// 初始化账户
customer.account_number = 123456;
strcpy(customer.name, "John Doe");
customer.balance = 0.0;
while (1) {
printf("1. Deposit\n2. Withdraw\n3. Check Balance\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1: deposit(&customer, ...); break;
case 2: withdraw(&customer, ...); break;
case 3: print_balance(&customer); break;
case 4: return 0; // 退出循环
default: printf("Invalid choice.\n");
}
}
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)