c语言编写银行业务模拟程序
时间: 2024-09-25 09:15:11 浏览: 33
C语言编写银行业务模拟程序可以涉及账户管理、交易操作、存款、取款、转账等功能。以下是一个简单的例子,展示如何创建一个银行账户类和处理一些基本操作:
```c
#include <stdio.h>
// 定义银行账户结构体
typedef struct {
char account_number[10];
char customer_name[50];
float balance;
} BankAccount;
// 初始化账户
BankAccount* create_account(char name[], float initial_deposit) {
BankAccount *account = (BankAccount*)malloc(sizeof(BankAccount));
strcpy(account->account_number, "XXXXXX");
strcpy(account->customer_name, name);
account->balance = initial_deposit;
return account;
}
// 存款函数
void deposit(BankAccount* account, float amount) {
if(amount > 0) {
account->balance += amount;
printf("存入了%.2f元,当前余额为%.2f\n", amount, account->balance);
} else {
printf("无法存款,金额必须为正。\n");
}
}
// 取款函数
void withdraw(BankAccount* account, float amount) {
if(amount <= account->balance) {
account->balance -= amount;
printf("取出了%.2f元,当前余额为%.2f\n", amount, account->balance);
} else {
printf("取款失败,余额不足。\n");
}
}
// 转账函数
void transfer(BankAccount* from_account, BankAccount* to_account, float amount) {
if(amount <= from_account->balance) {
withdraw(from_account, amount);
deposit(to_account, amount);
printf("转账%.2f元成功,从%.2f元变为%.2f元,转出到%.2f元变%.2f元\n",
amount, from_account->balance, from_account->balance,
to_account->balance, to_account->balance);
} else {
printf("转账失败,余额不足。\n");
}
}
int main() {
BankAccount* account1 = create_account("张三", 1000.00);
BankAccount* account2 = create_account("李四", 500.00);
// 模拟业务操作
deposit(account1, 500);
withdraw(account2, 200);
transfer(account1, account2, 300);
return 0;
}
阅读全文