用c++语言实现基于atm的银行账户管理系统
时间: 2024-01-15 10:04:22 浏览: 169
基于QT框架,采用CS架构实现的银行管理系统(含客户端、服务端),编程语言C++.zip
好的,下面是一个简单的基于ATM的银行账户管理系统的C语言实现。该程序包括以下功能:创建账户、存款、取款、查询余额、更改密码和退出系统。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100 // 最大账户数
#define MAX_PASSWORD_LENGTH 16 // 最大密码长度
// 账户结构体
struct account {
int account_number; // 账号
char password[MAX_PASSWORD_LENGTH]; // 密码
float balance; // 余额
};
// 全局变量
struct account accounts[MAX_ACCOUNTS]; // 账户数组
int num_accounts = 0; // 账户数量
// 函数声明
void create_account();
void deposit();
void withdraw();
void balance_inquiry();
void change_password();
void print_menu();
int get_account_index(int account_number, char *password);
int main() {
int choice;
char password[MAX_PASSWORD_LENGTH];
int account_number;
int index;
while (1) {
print_menu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
create_account();
break;
case 2:
printf("请输入账号:");
scanf("%d", &account_number);
printf("请输入密码:");
scanf("%s", password);
index = get_account_index(account_number, password);
if (index >= 0) {
deposit(index);
} else {
printf("账号或密码错误!\n");
}
break;
case 3:
printf("请输入账号:");
scanf("%d", &account_number);
printf("请输入密码:");
scanf("%s", password);
index = get_account_index(account_number, password);
if (index >= 0) {
withdraw(index);
} else {
printf("账号或密码错误!\n");
}
break;
case 4:
printf("请输入账号:");
scanf("%d", &account_number);
printf("请输入密码:");
scanf("%s", password);
index = get_account_index(account_number, password);
if (index >= 0) {
balance_inquiry(index);
} else {
printf("账号或密码错误!\n");
}
break;
case 5:
printf("请输入账号:");
scanf("%d", &account_number);
printf("请输入原密码:");
scanf("%s", password);
index = get_account_index(account_number, password);
if (index >= 0) {
change_password(index);
} else {
printf("账号或密码错误!\n");
}
break;
case 6:
printf("感谢使用本系统,再见!\n");
exit(0);
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
// 创建账户
void create_account() {
int account_number;
char password[MAX_PASSWORD_LENGTH];
float balance;
if (num_accounts >= MAX_ACCOUNTS) {
printf("账户数量已达上限!\n");
return;
}
printf("请输入账号:");
scanf("%d", &account_number);
if (get_account_index(account_number, "") >= 0) {
printf("该账号已存在!\n");
return;
}
printf("请输入密码:");
scanf("%s", password);
printf("请输入余额:");
scanf("%f", &balance);
accounts[num_accounts].account_number = account_number;
strcpy(accounts[num_accounts].password, password);
accounts[num_accounts].balance = balance;
num_accounts++;
printf("账户创建成功!\n");
}
// 存款
void deposit(int index) {
float amount;
printf("请输入存款金额:");
scanf("%f", &amount);
accounts[index].balance += amount;
printf("存款成功!\n");
printf("当前余额为:%.2f\n", accounts[index].balance);
}
// 取款
void withdraw(int index) {
float amount;
printf("请输入取款金额:");
scanf("%f", &amount);
if (amount > accounts[index].balance) {
printf("余额不足!\n");
return;
}
accounts[index].balance -= amount;
printf("取款成功!\n");
printf("当前余额为:%.2f\n", accounts[index].balance);
}
// 查询余额
void balance_inquiry(int index) {
printf("当前余额为:%.2f\n", accounts[index].balance);
}
// 更改密码
void change_password(int index) {
char password[MAX_PASSWORD_LENGTH];
printf("请输入新密码:");
scanf("%s", password);
strcpy(accounts[index].password, password);
printf("密码修改成功!\n");
}
// 打印菜单
void print_menu() {
printf("********** ATM **********\n");
printf("* 1. 创建账户 *\n");
printf("* 2. 存款 *\n");
printf("* 3. 取款 *\n");
printf("* 4. 查询余额 *\n");
printf("* 5. 更改密码 *\n");
printf("* 6. 退出系统 *\n");
printf("****************************\n");
}
// 根据账号和密码获取账户索引
int get_account_index(int account_number, char *password) {
int i;
for (i = 0; i < num_accounts; i++) {
if (accounts[i].account_number == account_number && strcmp(accounts[i].password, password) == 0) {
return i;
}
}
return -1;
}
```
需要注意的是,上述程序仅是一个简单的实现,没有考虑并发和安全性等问题,实际应用中需要进行进一步的完善。
阅读全文