帮我写一个C语言的银行管理系统
时间: 2023-08-28 08:11:26 浏览: 69
好的,下面是一个简单的银行管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100 // 最大账户数量
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_PASSWORD_LEN 20 // 密码最大长度
// 账户结构体
typedef struct Account {
char name[MAX_NAME_LEN];
char password[MAX_PASSWORD_LEN];
float balance;
} Account;
// 全局变量
Account accounts[MAX_ACCOUNTS]; // 所有账户
int num_accounts = 0; // 账户数量
// 显示菜单
void show_menu() {
printf("1. 创建账户\n");
printf("2. 存款\n");
printf("3. 取款\n");
printf("4. 查询余额\n");
printf("5. 修改密码\n");
printf("6. 删除账户\n");
printf("7. 退出\n");
}
// 创建账户
void create_account() {
if (num_accounts >= MAX_ACCOUNTS) {
printf("无法创建更多账户!\n");
return;
}
printf("请输入姓名:");
scanf("%s", accounts[num_accounts].name);
printf("请输入密码:");
scanf("%s", accounts[num_accounts].password);
accounts[num_accounts].balance = 0;
num_accounts++;
printf("账户创建成功!\n");
}
// 存款
void deposit() {
char name[MAX_NAME_LEN];
char password[MAX_PASSWORD_LEN];
float amount;
printf("请输入账户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
// 查找账户
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(name, accounts[i].name) == 0 && strcmp(password, accounts[i].password) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("账户名或密码错误!\n");
return;
}
printf("请输入存款金额:");
scanf("%f", &amount);
// 更新余额
accounts[index].balance += amount;
printf("存款成功!\n");
}
// 取款
void withdraw() {
char name[MAX_NAME_LEN];
char password[MAX_PASSWORD_LEN];
float amount;
printf("请输入账户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
// 查找账户
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(name, accounts[i].name) == 0 && strcmp(password, accounts[i].password) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("账户名或密码错误!\n");
return;
}
printf("请输入取款金额:");
scanf("%f", &amount);
// 检查余额是否足够
if (accounts[index].balance < amount) {
printf("余额不足!\n");
return;
}
// 更新余额
accounts[index].balance -= amount;
printf("取款成功!\n");
}
// 查询余额
void check_balance() {
char name[MAX_NAME_LEN];
char password[MAX_PASSWORD_LEN];
printf("请输入账户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
// 查找账户
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(name, accounts[i].name) == 0 && strcmp(password, accounts[i].password) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("账户名或密码错误!\n");
return;
}
printf("余额为:%.2f\n", accounts[index].balance);
}
// 修改密码
void change_password() {
char name[MAX_NAME_LEN];
char old_password[MAX_PASSWORD_LEN];
char new_password[MAX_PASSWORD_LEN];
printf("请输入账户名:");
scanf("%s", name);
printf("请输入旧密码:");
scanf("%s", old_password);
// 查找账户
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(name, accounts[i].name) == 0 && strcmp(old_password, accounts[i].password) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("账户名或密码错误!\n");
return;
}
printf("请输入新密码:");
scanf("%s", new_password);
// 更新密码
strcpy(accounts[index].password, new_password);
printf("密码修改成功!\n");
}
// 删除账户
void delete_account() {
char name[MAX_NAME_LEN];
char password[MAX_PASSWORD_LEN];
printf("请输入账户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
// 查找账户
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(name, accounts[i].name) == 0 && strcmp(password, accounts[i].password) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("账户名或密码错误!\n");
return;
}
// 删除账户
for (int i = index; i < num_accounts - 1; i++) {
accounts[i] = accounts[i + 1];
}
num_accounts--;
printf("账户删除成功!\n");
}
int main() {
int choice;
while (1) {
show_menu();
printf("请选择操作(1-7):");
scanf("%d", &choice);
switch (choice) {
case 1:
create_account();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
check_balance();
break;
case 5:
change_password();
break;
case 6:
delete_account();
break;
case 7:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
该银行管理系统支持以下操作:
1. 创建账户
2. 存款
3. 取款
4. 查询余额
5. 修改密码
6. 删除账户
7. 退出
阅读全文