c语言编写银行业务模拟程序,c语言银行业务模拟
时间: 2024-06-08 11:05:52 浏览: 205
c编写模拟银行业务
好的,下面给您提供一个简单的银行业务模拟程序的C语言代码示例。
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_CUSTOMERS 5 // 最大顾客数
#define MINUTES_PER_HOUR 60 // 每小时分钟数
// 枚举所有可能的银行业务类型
enum TransactionType { DEPOSIT, WITHDRAWAL, CHECK_BALANCE };
// 银行账户结构体
typedef struct {
int accountNumber;
float balance;
} Account;
// 银行业务结构体
typedef struct {
int customerNumber;
enum TransactionType type;
float amount;
} Transaction;
// 顾客结构体
typedef struct {
int customerNumber;
Account account;
Transaction transactions[10]; // 最多存储10个银行业务
} Customer;
// 银行结构体
typedef struct {
Customer customers[MAX_CUSTOMERS]; // 最多存储5个顾客
int customerCount;
} Bank;
// 初始化银行账户
void initializeAccounts(Bank *bank) {
for (int i = 0; i < bank->customerCount; i++) {
bank->customers[i].account.accountNumber = 1000 + i;
bank->customers[i].account.balance = rand() % 100000; // 随机生成一个0~99999之间的随机数
}
}
// 在银行中查找一个账户
Account *findAccount(Bank *bank, int accountNumber) {
for (int i = 0; i < bank->customerCount; i++) {
if (bank->customers[i].account.accountNumber == accountNumber) {
return &bank->customers[i].account;
}
}
return NULL;
}
// 执行一个存款业务
void doDeposit(Bank *bank, int customerNumber, float amount) {
Account *account = &bank->customers[customerNumber].account;
account->balance += amount;
Transaction transaction = { customerNumber, DEPOSIT, amount };
bank->customers[customerNumber].transactions[0] = transaction;
}
// 执行一个取款业务
void doWithdrawal(Bank *bank, int customerNumber, float amount) {
Account *account = &bank->customers[customerNumber].account;
if (account->balance >= amount) {
account->balance -= amount;
Transaction transaction = { customerNumber, WITHDRAWAL, amount };
bank->customers[customerNumber].transactions[0] = transaction;
}
}
// 执行一个余额查询业务
void doCheckBalance(Bank *bank, int customerNumber) {
Account *account = &bank->customers[customerNumber].account;
Transaction transaction = { customerNumber, CHECK_BALANCE, account->balance };
bank->customers[customerNumber].transactions[0] = transaction;
}
// 随机执行一个顾客的银行业务
void doRandomTransaction(Bank *bank) {
int customerIndex = rand() % bank->customerCount;
int transactionType = rand() % 3;
float amount = rand() % 10000;
switch (transactionType) {
case 0:
doDeposit(bank, customerIndex, amount);
printf("Customer %d deposited $%.2f.\n", customerIndex, amount);
break;
case 1:
doWithdrawal(bank, customerIndex, amount);
printf("Customer %d withdrew $%.2f.\n", customerIndex, amount);
break;
case 2:
doCheckBalance(bank, customerIndex);
printf("Customer %d checked their balance.\n", customerIndex);
break;
}
}
// 显示一个顾客的银行业务历史记录
void displayTransactions(Bank *bank, int customerNumber) {
printf("Transaction history for customer %d:\n", customerNumber);
for (int i = 0; i < 10 && bank->customers[customerNumber].transactions[i].type != 0; i++) {
Transaction transaction = bank->customers[customerNumber].transactions[i];
switch (transaction.type) {
case DEPOSIT:
printf("Deposit of $%.2f\n", transaction.amount);
break;
case WITHDRAWAL:
printf("Withdrawal of $%.2f\n", transaction.amount);
break;
case CHECK_BALANCE:
printf("Balance check - current balance is $%.2f\n", transaction.amount);
break;
}
}
}
int main() {
srand(time(NULL)); // 初始化随机数生成器
Bank bank = { 0 };
bank.customerCount = 3;
initializeAccounts(&bank);
for (int i = 0; i < MINUTES_PER_HOUR * 8; i++) { // 模拟一整个工作日,即8小时
doRandomTransaction(&bank);
}
displayTransactions(&bank, 0);
return 0;
}
```
这个程序模拟了一个简单的银行业务系统,包括顾客账户、银行业务类型、银行账户、顾客结构体和银行结构体等。程序随机执行银行业务,并且允许您查看一个顾客的银行业务历史记录。请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。
阅读全文