Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce8abe3c18 | ||
|
|
9fb190ad5d | ||
|
|
627201c739 | ||
|
|
772b417cb2 | ||
|
|
f4737c82f6 | ||
|
|
93e0214536 | ||
|
|
74e467e111 | ||
|
|
7d623c1993 | ||
|
|
ae99d48eda |
@@ -0,0 +1,5 @@
|
||||
*.o
|
||||
*.exe
|
||||
bank.dat
|
||||
.vscode/launch.json
|
||||
.vscode/tasks.json
|
||||
@@ -1,135 +1,140 @@
|
||||
# 简易银行账户管理系统
|
||||
|
||||
## 项目简介
|
||||
这是一个基于C语言开发的简易银行账户管理系统,支持基本的银行账户操作功能,包括开户、登录、存款、取款、查询余额、账户管理等。系统采用模块化设计,代码结构清晰,易于维护和扩展。
|
||||
|
||||
## 功能特性
|
||||
- **账户管理**
|
||||
- 创建普通用户账户(权限2)
|
||||
- 创建管理员账户(权限1)
|
||||
- 删除账户(仅管理员可操作)
|
||||
- 修改密码
|
||||
- **账户操作**
|
||||
- 存款
|
||||
- 取款
|
||||
- 查询余额
|
||||
- **系统功能**
|
||||
- 用户登录验证
|
||||
- 权限分级(管理员/普通用户)
|
||||
- 数据持久化(自动保存到文件)
|
||||
- 自动初始化默认管理员账户
|
||||
|
||||
## 系统架构
|
||||
### 核心模块
|
||||
1. **主程序 (bank.c)**
|
||||
- 程序入口点
|
||||
- 菜单显示和用户交互
|
||||
- 权限控制逻辑
|
||||
|
||||
2. **账户管理 (account.c)**
|
||||
- 账户创建、删除
|
||||
- 账户查找
|
||||
- 登录验证
|
||||
- 密码修改
|
||||
- 显示所有账户信息
|
||||
|
||||
3. **交易处理 (transaction.c)**
|
||||
- 存款功能
|
||||
- 取款功能
|
||||
- 余额查询
|
||||
|
||||
4. **数据持久化 (file_io.c)**
|
||||
- 数据保存到文件(bank.dat)
|
||||
- 从文件加载数据
|
||||
- 初始化默认管理员账户
|
||||
|
||||
5. **工具函数 (utils.c)**
|
||||
- 输入验证和清理
|
||||
- 安全的整数和浮点数输入
|
||||
|
||||
6. **头文件 (bank.h)**
|
||||
- 全局常量定义
|
||||
- 账户结构体定义
|
||||
- 函数声明
|
||||
|
||||
### 数据结构
|
||||
```c
|
||||
typedef struct strAccount
|
||||
{
|
||||
int id; // 账户ID(从1000开始)
|
||||
char name[31]; // 姓名(最多30字符)
|
||||
double balance; // 账户余额
|
||||
int password; // 6位数字密码
|
||||
int quanxian; // 权限(1=管理员,2=普通用户)
|
||||
} STACCOUNT;
|
||||
```
|
||||
|
||||
## 使用说明
|
||||
### 编译
|
||||
```bash
|
||||
gcc bank.c account.c transaction.c file_io.c utils.c -o bank_system
|
||||
```
|
||||
|
||||
### 运行
|
||||
```bash
|
||||
./bank_system
|
||||
```
|
||||
|
||||
### 操作流程
|
||||
1. **初始界面**(未登录状态)
|
||||
- 选项1:开户(创建普通用户账户)
|
||||
- 选项2:登录
|
||||
- 选项0:退出系统
|
||||
|
||||
2. **普通用户界面**(登录后)
|
||||
- 选项1:存款
|
||||
- 选项2:取款
|
||||
- 选项3:查询余额
|
||||
- 选项0:退出账户
|
||||
|
||||
3. **管理员界面**(登录后)
|
||||
- 选项1:存款
|
||||
- 选项2:取款
|
||||
- 选项3:查询余额
|
||||
- 选项4:显示所有账户
|
||||
- 选项5:删除账户
|
||||
- 选项6:创建管理员账户
|
||||
- 选项0:退出账户
|
||||
|
||||
### 默认账户
|
||||
系统首次运行时会自动创建一个默认管理员账户:
|
||||
- **账户ID**: 1000
|
||||
- **姓名**: admin
|
||||
- **密码**: 123456
|
||||
- **权限**: 管理员
|
||||
|
||||
## 技术细节
|
||||
- **最大账户数量**: 50个
|
||||
- **密码规则**: 6位数字(100000-999999)
|
||||
- **数据存储**: 二进制文件(bank.dat)
|
||||
- **输入验证**: 所有数值输入都包含错误处理
|
||||
- **内存管理**: 使用全局数组存储账户数据
|
||||
|
||||
## 文件说明
|
||||
- `bank.c` - 主程序文件
|
||||
- `account.c` - 账户管理功能实现
|
||||
- `transaction.c` - 交易处理功能实现
|
||||
- `file_io.c` - 文件读写功能实现
|
||||
- `utils.c` - 工具函数实现
|
||||
- `bank.h` - 头文件,包含结构体定义和函数声明
|
||||
- `bank.dat` - 数据存储文件(运行时自动生成)
|
||||
|
||||
## 注意事项
|
||||
1. 密码必须为6位数字
|
||||
2. 存款和取款金额必须为正数
|
||||
3. 取款时会检查账户余额是否充足
|
||||
4. 删除账户功能仅管理员可用
|
||||
5. 系统会自动保存所有操作到文件,确保数据持久化
|
||||
|
||||
## 扩展建议
|
||||
- 添加交易记录功能
|
||||
- 实现账户排序和搜索
|
||||
- 增加更多权限级别
|
||||
- 改进用户界面(如使用图形界面)
|
||||
- 添加数据加密功能
|
||||
# 简易银行账户管理系统
|
||||
|
||||
## 项目简介
|
||||
这是一个基于C语言开发的简易银行账户管理系统,支持基本的银行账户操作功能,包括开户、登录、存款、取款、查询余额、账户管理等。系统采用模块化设计,代码结构清晰,易于维护和扩展。
|
||||
|
||||
## 功能特性
|
||||
- **账户管理**
|
||||
- 创建普通用户账户(权限2)
|
||||
- 创建管理员账户(权限1)
|
||||
- 删除账户(仅管理员可操作)
|
||||
- 修改密码
|
||||
- **账户操作**
|
||||
- 存款
|
||||
- 取款
|
||||
- 查询余额
|
||||
- **系统功能**
|
||||
- 用户登录验证
|
||||
- 权限分级(管理员/普通用户)
|
||||
- 数据持久化(自动保存到文件)
|
||||
- 交易流水查询(管理员)
|
||||
- 自动初始化默认管理员账户
|
||||
|
||||
## 系统架构
|
||||
### 核心模块
|
||||
1. **主程序 (bank.c)**
|
||||
- 程序入口点
|
||||
- 菜单显示和用户交互
|
||||
- 权限控制逻辑
|
||||
|
||||
2. **账户管理 (account.c)**
|
||||
- 账户创建、删除
|
||||
- 账户查找
|
||||
- 登录验证
|
||||
- 密码修改
|
||||
- 显示所有账户信息
|
||||
|
||||
3. **交易处理 (transaction.c)**
|
||||
- 存款功能
|
||||
- 取款功能
|
||||
- 余额查询
|
||||
|
||||
4. **数据持久化 (file_io.c)**
|
||||
- 数据保存到文件(bank.dat)
|
||||
- 从文件加载数据
|
||||
- 初始化默认管理员账户
|
||||
|
||||
5. **工具函数 (utils.c)**
|
||||
- 输入验证和清理
|
||||
- 安全的整数和浮点数输入
|
||||
|
||||
6. **头文件 (bank.h)**
|
||||
- 全局常量定义
|
||||
- 账户结构体定义
|
||||
- 函数声明
|
||||
|
||||
7. **日志模块(log.c)**
|
||||
- 日志记录和存储(循环链表)
|
||||
|
||||
### 数据结构
|
||||
```c
|
||||
typedef struct strAccount
|
||||
{
|
||||
int id; // 账户ID(从1000开始)
|
||||
char name[31]; // 姓名(最多30字符)
|
||||
double balance; // 账户余额
|
||||
int password; // 6位数字密码
|
||||
int quanxian; // 权限(1=管理员,2=普通用户)
|
||||
} STACCOUNT;
|
||||
```
|
||||
|
||||
## 使用说明
|
||||
### 编译
|
||||
```bash
|
||||
gcc bank.c account.c transaction.c file_io.c utils.c -o bank_system
|
||||
```
|
||||
|
||||
### 运行
|
||||
```bash
|
||||
./bank_system
|
||||
```
|
||||
|
||||
### 操作流程
|
||||
1. **初始界面**(未登录状态)
|
||||
- 选项1:开户(创建普通用户账户)
|
||||
- 选项2:登录
|
||||
- 选项0:退出系统
|
||||
|
||||
2. **普通用户界面**(登录后)
|
||||
- 选项1:存款
|
||||
- 选项2:取款
|
||||
- 选项3:查询余额
|
||||
- 选项0:退出账户
|
||||
|
||||
3. **管理员界面**(登录后)
|
||||
- 选项1:存款
|
||||
- 选项2:取款
|
||||
- 选项3:查询余额
|
||||
- 选项4:显示所有账户
|
||||
- 选项5:删除账户
|
||||
- 选项6:创建管理员账户
|
||||
- 选项7:查看交易流水
|
||||
- 选项0:退出账户
|
||||
|
||||
### 默认账户
|
||||
系统首次运行时会自动创建一个默认管理员账户:
|
||||
- **账户ID**: 1000
|
||||
- **姓名**: admin
|
||||
- **密码**: 123456
|
||||
- **权限**: 管理员
|
||||
|
||||
## 技术细节
|
||||
- **最大账户数量**: 50个
|
||||
- **密码规则**: 6位数字(100000-999999)
|
||||
- **数据存储**: 二进制文件(bank.dat)
|
||||
- **输入验证**: 所有数值输入都包含错误处理
|
||||
- **内存管理**: 使用全局数组存储账户数据
|
||||
|
||||
## 文件说明
|
||||
- `bank.c` - 主程序文件
|
||||
- `account.c` - 账户管理功能实现
|
||||
- `transaction.c` - 交易处理功能实现
|
||||
- `file_io.c` - 文件读写功能实现
|
||||
- `utils.c` - 工具函数实现
|
||||
- `bank.h` - 头文件,包含结构体定义和函数声明
|
||||
- `bank.dat` - 数据存储文件(运行时自动生成)
|
||||
|
||||
## 注意事项
|
||||
1. 密码必须为6位数字
|
||||
2. 存款和取款金额必须为正数
|
||||
3. 取款时会检查账户余额是否充足
|
||||
4. 删除账户功能仅管理员可用
|
||||
5. 系统会自动保存所有操作到文件,确保数据持久化
|
||||
|
||||
## 扩展建议
|
||||
- ~~添加交易记录功能~~
|
||||
- 实现账户排序和搜索
|
||||
- 增加更多权限级别
|
||||
- 改进用户界面(如使用图形界面)
|
||||
- 添加数据加密功能
|
||||
- 实现多用户并发访问
|
||||
@@ -6,15 +6,15 @@
|
||||
extern STACCOUNT g_astAccounts[];
|
||||
extern int g_iAccCount;
|
||||
|
||||
/* 函数功能:处理用户的创建账户操作
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 存款信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
/* 函数功能:处理用户的创建账户操作
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 存款信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
*/
|
||||
void InitAdminAccount() {
|
||||
printf("初始化默认管理员账户");
|
||||
printf("初始化默认管理员账户");
|
||||
g_astAccounts[0].id = 1000;
|
||||
strcpy(g_astAccounts[0].name, "admin");
|
||||
g_astAccounts[0].balance = 0.0;
|
||||
@@ -25,73 +25,73 @@ void InitAdminAccount() {
|
||||
|
||||
void CreateAccount(int quanxian)
|
||||
{
|
||||
// (1)合法性检查
|
||||
// (1)合法性检查
|
||||
if(MAX_ACCOUNTS < g_iAccCount)
|
||||
{
|
||||
printf("账户数量已达上限!\n");
|
||||
printf("账户数量已达上限!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// (2)定义局部变量(函数内使用的变量)
|
||||
STACCOUNT stNewAcc; //账户变量,结构体变量定义
|
||||
//char cInput[NAME_LEN]; //接输入的名字字符串
|
||||
// (2)定义局部变量(函数内使用的变量)
|
||||
STACCOUNT stNewAcc; //账户变量,结构体变量定义
|
||||
//char cInput[NAME_LEN]; //接输入的名字字符串
|
||||
|
||||
// (3)功能实现
|
||||
printf("请输入姓名(最多30个字符): ");
|
||||
// (3)功能实现
|
||||
printf("请输入姓名(最多30个字符): ");
|
||||
|
||||
//这里有风险,怎么改进?
|
||||
//这里有风险,怎么改进?
|
||||
scanf("%30s", stNewAcc.name);
|
||||
//stNewAcc.name[NAME_LEN - 1] = '\0'; // 确保null终止
|
||||
//输密码
|
||||
//stNewAcc.name[NAME_LEN - 1] = '\0'; // 确保null终止
|
||||
//输密码
|
||||
int temp_password;
|
||||
while (1) {
|
||||
printf("请输入密码\n");
|
||||
printf("请输入密码\n");
|
||||
scanf("%d",&temp_password);
|
||||
if (99999<temp_password&&temp_password<999999) {
|
||||
printf("设置成功\n");
|
||||
printf("设置成功\n");
|
||||
stNewAcc.password=temp_password;
|
||||
SaveData();
|
||||
break;
|
||||
}
|
||||
printf("密码不符合规范!请重新输入\n");
|
||||
printf("密码不符合规范!请重新输入\n");
|
||||
}
|
||||
//需求中要求账户ID从1000开始,初始化账户余额
|
||||
//需求中要求账户ID从1000开始,初始化账户余额
|
||||
stNewAcc.id = 1000 + g_iAccCount;
|
||||
stNewAcc.balance = 0.0;
|
||||
stNewAcc.quanxian = quanxian;
|
||||
g_astAccounts[g_iAccCount] = stNewAcc;
|
||||
|
||||
//把记录账户数量的全局变量自增。加过之后自增,所以下标0占用,数量是1个
|
||||
//把记录账户数量的全局变量自增。加过之后自增,所以下标0占用,数量是1个
|
||||
g_iAccCount++;
|
||||
|
||||
printf("开户成功! 账户ID: %d\n", stNewAcc.id);
|
||||
printf("开户成功! 账户ID: %d\n", stNewAcc.id);
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/* 函数功能:处理用户的显示所有用户信息的操作
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 从全局变量g_astAccounts中取信息,并显示
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
/* 函数功能:处理用户的显示所有用户信息的操作
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 从全局变量g_astAccounts中取信息,并显示
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
*/
|
||||
void ShowAccounts()
|
||||
{
|
||||
//(1)输出用户内容提示
|
||||
printf("\n所有账户信息:\n");
|
||||
//(1)输出用户内容提示
|
||||
printf("\n所有账户信息:\n");
|
||||
|
||||
//(2)检查是否有账户了?异常情况无账户的处理
|
||||
//(2)检查是否有账户了?异常情况无账户的处理
|
||||
if (0 == g_iAccCount)
|
||||
{
|
||||
printf("暂无账户信息\n");
|
||||
printf("暂无账户信息\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//(3)打印表头
|
||||
printf("%-8s %-30s %-10s %-3s\n", "ID", "姓名", "余额","权限");
|
||||
//(3)打印表头
|
||||
printf("%-8s %-30s %-10s %-3s\n", "ID", "姓名", "余额","权限");
|
||||
printf("------------------------------------------------------\n");
|
||||
|
||||
//(4)循环打印每一行
|
||||
//(4)循环打印每一行
|
||||
for(int i = 0; i < g_iAccCount; i++)
|
||||
{
|
||||
printf("%-8d %-30s %-10.2f %-10d\n",
|
||||
@@ -99,37 +99,37 @@ void ShowAccounts()
|
||||
}
|
||||
|
||||
}
|
||||
//登录账户,返回权限
|
||||
//登录账户,返回权限
|
||||
int LoginAccount(int temp_id) {
|
||||
int temp_password;
|
||||
if (FindAccount(temp_id)==-1) {
|
||||
printf("没有找到账户\n");
|
||||
printf("没有找到账户\n");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
while (1) {
|
||||
printf("请输入密码");
|
||||
printf("请输入密码");
|
||||
scanf("%d", &temp_password);
|
||||
if (g_astAccounts[FindAccount(temp_id)].password==temp_password) {
|
||||
printf("欢迎用户%s \n",g_astAccounts[FindAccount(temp_id)].name);
|
||||
printf("欢迎用户%s \n",g_astAccounts[FindAccount(temp_id)].name);
|
||||
if (g_astAccounts[FindAccount(temp_id)].quanxian==1) {
|
||||
printf("您目前以管理员身份登录");
|
||||
printf("您目前以管理员身份登录");
|
||||
return 1;
|
||||
}
|
||||
else {return 2;}
|
||||
}
|
||||
else {
|
||||
printf("密码错误,请重新输入\n");
|
||||
printf("密码错误,请重新输入\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* 函数功能:根据输入的用户ID找到
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 用户ID跟数组ID
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
/* 函数功能:根据输入的用户ID找到
|
||||
思考处理步骤,写到函数体内注释
|
||||
* 参数:无
|
||||
* 用户ID跟数组ID
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
*/
|
||||
int FindAccount(int id)
|
||||
{
|
||||
@@ -140,15 +140,15 @@ int FindAccount(int id)
|
||||
|
||||
return -1;
|
||||
}
|
||||
//删除账户
|
||||
//删除账户
|
||||
void DeleteAccount(int id) {
|
||||
printf("正在删除\n");
|
||||
printf("正在删除\n");
|
||||
int i=FindAccount(id);
|
||||
if (i==-1) {
|
||||
printf("有这个账户吗你就删");
|
||||
printf("有这个账户吗你就删");
|
||||
}
|
||||
else {
|
||||
printf("正在删除一下账户...");
|
||||
printf("正在删除一下账户...");
|
||||
printf("%-8d %-30s %-10.2f %-3d\n",
|
||||
g_astAccounts[i].id, g_astAccounts[i].name, g_astAccounts[i].balance,g_astAccounts[i].quanxian);
|
||||
for (; i < g_iAccCount-i; i++)
|
||||
@@ -162,24 +162,24 @@ void DeleteAccount(int id) {
|
||||
void ChangePassword(int id) {
|
||||
int temp_password;
|
||||
while (1) {
|
||||
printf("请输入原密码");
|
||||
printf("请输入原密码");
|
||||
scanf("%d",&temp_password);
|
||||
if (g_astAccounts[FindAccount(id)].password==temp_password) {
|
||||
while (1) {
|
||||
printf("请输入新密码\n");
|
||||
printf("请输入新密码\n");
|
||||
scanf("%d",&temp_password);
|
||||
if (99999<temp_password&&temp_password<999999) {
|
||||
printf("修改成功\n");
|
||||
printf("修改成功\n");
|
||||
g_astAccounts[FindAccount(id)].password=temp_password;
|
||||
SaveData();
|
||||
break;
|
||||
}
|
||||
printf("密码不符合规范!请重新输入\n");
|
||||
printf("密码不符合规范!请重新输入\n");
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
printf("密码错误,请重新输入");
|
||||
printf("密码错误,请重新输入");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "bank.h"
|
||||
|
||||
LogQueue g_logQueue; // <-- 新增:定义全局日志队列
|
||||
STACCOUNT g_astAccounts[MAX_ACCOUNTS];
|
||||
|
||||
int g_iAccCount = 1;
|
||||
@@ -12,53 +12,54 @@ int d_id;
|
||||
int main(void)
|
||||
{
|
||||
LoadData();
|
||||
//(1)导入数据
|
||||
//(1)导入数据
|
||||
// which fuction?
|
||||
|
||||
//应该先导入数据再欢迎?还是欢迎了再导入数据?
|
||||
printf("=== 简易银行账户管理系统 ===\n");
|
||||
printf("欢迎使用! 系统已加载%d个账户\n", g_iAccCount);
|
||||
InitAdminAccount();//初始化管理员账号
|
||||
int iChoice=-1; //选择标志
|
||||
//应该先导入数据再欢迎?还是欢迎了再导入数据?
|
||||
printf("=== 简易银行账户管理系统 ===\n");
|
||||
printf("欢迎使用! 系统已加载%d个账户\n", g_iAccCount);
|
||||
InitAdminAccount();//初始化管理员账号
|
||||
InitLogQueue();
|
||||
int iChoice=-1; //选择标志
|
||||
int pChoice=-1;
|
||||
while (1) {
|
||||
//持续显示菜单的秘诀
|
||||
//(2)显示主菜单,并且处理用户的选择
|
||||
//持续显示菜单的秘诀
|
||||
//(2)显示主菜单,并且处理用户的选择
|
||||
if (quanxian==-2){break;}
|
||||
printf("\n=== 简易银行账户管理系统 ===\n");
|
||||
printf("\n=== 简易银行账户管理系统 ===\n");
|
||||
switch(quanxian) {
|
||||
case 0:
|
||||
printf("1. 开户\n2. 登录 \n0. 退出\n");
|
||||
//printf("1. 开户\n2. 存款\n3. 取款\n4. 查询\n5. 显示所有账户\n6. 退出\n");
|
||||
printf("请选择: ");
|
||||
printf("1. 开户\n2. 登录 \n0. 退出\n");
|
||||
//printf("1. 开户\n2. 存款\n3. 取款\n4. 查询\n5. 显示所有账户\n6. 退出\n");
|
||||
printf("请选择: ");
|
||||
iChoice=-1;
|
||||
scanf("%d", &iChoice);
|
||||
switch(iChoice) {
|
||||
case 1://CREATE
|
||||
//调用创建账号的函数
|
||||
//调用创建账号的函数
|
||||
CreateAccount(2);
|
||||
quanxian=0;
|
||||
break;
|
||||
case 2://LOGIN
|
||||
printf("请输入账户id");
|
||||
printf("请输入账户id");
|
||||
scanf("%d", &user_id);
|
||||
quanxian=LoginAccount(user_id);
|
||||
break;
|
||||
case 0://EXIT
|
||||
printf("谢谢使用!\n按任意键退出\n");
|
||||
printf("谢谢使用!\n按任意键退出\n");
|
||||
getchar();
|
||||
quanxian=-2;
|
||||
exit(0);
|
||||
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n0. 退出账户");
|
||||
printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n7.查看交易日志\n8.搜索交易日志\n0. 退出账户");
|
||||
pChoice=-1;
|
||||
scanf("%d", &pChoice);
|
||||
|
||||
@@ -76,7 +77,7 @@ int main(void)
|
||||
ShowAccounts();
|
||||
break;
|
||||
case 5:
|
||||
printf("请输入要删除的id");
|
||||
printf("请输入要删除的id");
|
||||
scanf("%d", &d_id);
|
||||
DeleteAccount(d_id);
|
||||
g_iAccCount--;
|
||||
@@ -84,20 +85,29 @@ int main(void)
|
||||
case 6:
|
||||
CreateAccount(1);
|
||||
break;
|
||||
case 7:
|
||||
ShowTransactionLogs();
|
||||
break;
|
||||
case 8:
|
||||
printf("请输入搜索内容");
|
||||
char pattern[50];
|
||||
scanf("%s", pattern);
|
||||
SearchLogs(pattern);
|
||||
break;
|
||||
case 0://EXIT
|
||||
printf("谢谢使用!\n按任意键退出");
|
||||
printf("谢谢使用!\n按任意键退出");
|
||||
getchar();
|
||||
quanxian=-2;
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
printf("1. 存款\n2. 取款\n3. 查询\n0.退出账户");
|
||||
printf("1. 存款\n2. 取款\n3. 查询\n0.退出账户");
|
||||
pChoice=-1;
|
||||
scanf("%d", &pChoice);
|
||||
switch(pChoice) {
|
||||
@@ -111,22 +121,22 @@ int main(void)
|
||||
QueryBalance(user_id);
|
||||
break;
|
||||
case 0://EXIT
|
||||
printf("谢谢使用!\n按任意键退出");
|
||||
printf("谢谢使用!\n按任意键退出");
|
||||
getchar();
|
||||
quanxian=-2;
|
||||
break;
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
//还要实现哪些菜单吗?
|
||||
//如果要做排序展示,要做什么?
|
||||
default: //好的编码习惯,对无效值做处理
|
||||
printf("无效选择!\n");
|
||||
break; //好的编码加上break,万一default挪了个位置呢?
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//(3)调用保存数据的函数
|
||||
//(3)调用保存数据的函数
|
||||
// which fuction?
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#ifndef BANK_H
|
||||
#define BANK_H
|
||||
#define MAX_LOGS 100 // 日志队列最大容量,防止无限增长
|
||||
#define MAX_ACCOUNTS 50 //系统规格为50个账户
|
||||
#define NAME_LEN 31 //用户名字最长30字符,因此需要多1个字符串结束符
|
||||
|
||||
#define MAX_ACCOUNTS 50 //系统规格为50个账户
|
||||
#define NAME_LEN 31 //用户名字最长30字符,因此需要多1个字符串结束符
|
||||
|
||||
// 账户信息,每个账户id对应了名字、账户余额等信息,是不是用数据结构定义比较合适?
|
||||
// 账户信息,每个账户id对应了名字、账户余额等信息,是不是用数据结构定义比较合适?
|
||||
typedef struct strAccount
|
||||
{
|
||||
int id;
|
||||
@@ -16,39 +16,68 @@ typedef struct strAccount
|
||||
|
||||
// strAccount STACCOUNT;
|
||||
|
||||
// 函数声明,main函数中调用的各模块函数需要在程序公用的.h中声明
|
||||
// 公开出来被其他模块调用的函数,也应该在公用的.h中声明
|
||||
// 函数声明,main函数中调用的各模块函数需要在程序公用的.h中声明
|
||||
// 公开出来被其他模块调用的函数,也应该在公用的.h中声明
|
||||
|
||||
//创建账户的函数
|
||||
//创建账户的函数
|
||||
void CreateAccount(int quanxian);
|
||||
|
||||
//实现存款的函数
|
||||
//实现存款的函数
|
||||
void Deposit(int id);
|
||||
|
||||
//实现取款的函数
|
||||
//实现取款的函数
|
||||
void Withdraw(int id);
|
||||
|
||||
//查询余额的函数
|
||||
//查询余额的函数
|
||||
void QueryBalance(int id);
|
||||
|
||||
//显示全部账户的函数
|
||||
//显示全部账户的函数
|
||||
void ShowAccounts();
|
||||
|
||||
//数据持久化函数
|
||||
//数据持久化函数
|
||||
void SaveData();
|
||||
|
||||
//加载数据的函数
|
||||
//加载数据的函数
|
||||
void LoadData();
|
||||
|
||||
//根据ID查找账户数组id的函数
|
||||
//根据ID查找账户数组id的函数
|
||||
int FindAccount(int iD);
|
||||
|
||||
//登录函数,返回用户权限
|
||||
//登录函数,返回用户权限
|
||||
int LoginAccount(int temp_id);
|
||||
|
||||
//销户
|
||||
//销户
|
||||
void DeleteAccount(int id);
|
||||
|
||||
void InitAdminAccount();
|
||||
|
||||
// 日志条目结构
|
||||
typedef struct {
|
||||
int account_id; // 涉及的账户ID
|
||||
char type[10]; // 操作类型: "Deposit" 或 "Withdraw"
|
||||
double amount; // 交易金额
|
||||
double balance; // 交易后的余额
|
||||
char timestamp[20]; // 时间戳 (格式: YYYY-MM-DD HH:MM:SS)
|
||||
char location[50]; // 地点信息
|
||||
} LogEntry;
|
||||
|
||||
// 环形队列结构
|
||||
typedef struct {
|
||||
LogEntry logs[MAX_LOGS];
|
||||
int front; // 队头指针
|
||||
int rear; // 队尾指针
|
||||
} LogQueue;
|
||||
|
||||
// 全局日志队列变量 (在 bank.c 中定义,在其他文件中 extern 引用)
|
||||
extern LogQueue g_logQueue;
|
||||
|
||||
// 日志函数声明
|
||||
void InitLogQueue(); // 初始化队列
|
||||
int IsLogQueueFull(); // 检查队列是否满
|
||||
int IsLogQueueEmpty(); // 检查队列是否空
|
||||
int EnqueueLog(int id, const char* type, double amt, double bal); // 入队
|
||||
void ShowTransactionLogs(); // 显示所有日志
|
||||
|
||||
void SearchLogs(const char* pattern);//日志搜索
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "bank.h"
|
||||
#include <io.h>
|
||||
|
||||
#define FILE_NAME bank.bat
|
||||
|
||||
//要操作的全局变量,但不在本模块定义需要声明为外部引入
|
||||
extern STACCOUNT g_astAccounts[];
|
||||
extern int g_iAccCount;
|
||||
|
||||
/* 函数功能:数据持久化,将内存中的账户数据保存到文件中
|
||||
保存方式为二进制
|
||||
保存到文件bank.bat中,该文件名只在本模块用使用
|
||||
* 参数:无
|
||||
* 保存信息来自全局变量g_astAccounts
|
||||
* 返回值:无
|
||||
* 保存是失败怎么处理?
|
||||
*/
|
||||
void SaveData()
|
||||
{
|
||||
FILE *file = fopen("bank.dat", "wb");
|
||||
fwrite(g_astAccounts, sizeof(STACCOUNT), 50, file);
|
||||
fclose(file);
|
||||
LoadData();
|
||||
}
|
||||
|
||||
|
||||
/* 函数功能:从文件bank.bat中把账户数据读取到内存
|
||||
操作的文件名仅在本模块中使用
|
||||
* 参数:无
|
||||
* 信息读取后放入全局变量g_astAccounts
|
||||
* 返回值:无
|
||||
* 读取失败怎么处理?
|
||||
*/
|
||||
void LoadData()
|
||||
{
|
||||
int i;
|
||||
int result=0;
|
||||
FILE *file = fopen("bank.dat", "rb");
|
||||
if (file == NULL) {
|
||||
InitAdminAccount();
|
||||
g_iAccCount= 1;
|
||||
}
|
||||
else {
|
||||
fread(g_astAccounts, sizeof(STACCOUNT), 50, file);
|
||||
}
|
||||
fclose( file);
|
||||
for (i = 0; i < 50; i++) {
|
||||
if (g_astAccounts[i].id != 0) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
g_iAccCount= result;
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "bank.h"
|
||||
|
||||
// 初始化日志队列
|
||||
void InitLogQueue() {
|
||||
g_logQueue.front = 0;
|
||||
g_logQueue.rear = 0;
|
||||
}
|
||||
|
||||
// 检查队列是否满
|
||||
int IsLogQueueFull() {
|
||||
return (g_logQueue.rear + 1) % MAX_LOGS == g_logQueue.front;
|
||||
}
|
||||
|
||||
// 检查队列是否空
|
||||
int IsLogQueueEmpty() {
|
||||
return g_logQueue.front == g_logQueue.rear;
|
||||
}
|
||||
|
||||
// 获取当前时间字符串
|
||||
void GetCurrentTimeStr(char* buf) {
|
||||
time_t t = time(NULL);
|
||||
struct tm tm = *localtime(&t);
|
||||
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
}
|
||||
|
||||
// 入队操作:记录交易
|
||||
// 参数: 账户ID, 类型("Deposit"/"Withdraw"), 交易金额, 交易后余额
|
||||
int EnqueueLog(int id, const char* type, double amt, double bal) {
|
||||
// 如果队列满,通过移动 front 指针覆盖最旧的日志 (环形队列特性)
|
||||
if (IsLogQueueFull()) {
|
||||
// 覆盖旧数据,队头自动出队
|
||||
g_logQueue.front = (g_logQueue.front + 1) % MAX_LOGS;
|
||||
}
|
||||
|
||||
// 准备日志条目
|
||||
int pos = g_logQueue.rear;
|
||||
g_logQueue.logs[pos].account_id = id;
|
||||
strcpy(g_logQueue.logs[pos].type, type);
|
||||
g_logQueue.logs[pos].amount = amt;
|
||||
g_logQueue.logs[pos].balance = bal;
|
||||
GetCurrentTimeStr(g_logQueue.logs[pos].timestamp);
|
||||
strcpy(g_logQueue.logs[pos].location, "宇宙总行"); // 根据你的需求固定地点
|
||||
|
||||
// 队尾指针后移
|
||||
g_logQueue.rear = (g_logQueue.rear + 1) % MAX_LOGS;
|
||||
|
||||
return 1; // 成功
|
||||
}
|
||||
|
||||
// 显示所有日志 (从队头到队尾)
|
||||
void ShowTransactionLogs() {
|
||||
if (IsLogQueueEmpty()) {
|
||||
printf("暂无交易日志记录。\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\n=== 交易流水日志 ===\n");
|
||||
printf("%-5s %-8s %-10s %-10s %-15s %-20s\n", "ID", "操作", "金额", "余额", "时间", "地点");
|
||||
printf("--------------------------------------------------------------\n");
|
||||
|
||||
int i = g_logQueue.front;
|
||||
while (i != g_logQueue.rear) {
|
||||
LogEntry e = g_logQueue.logs[i];
|
||||
printf("%-5d %-8s %-10.2f %-10.2f %-15s %-20s\n",
|
||||
e.account_id, e.type, e.amount, e.balance, e.timestamp, e.location);
|
||||
i = (i + 1) % MAX_LOGS;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 构建部分匹配表 (Next Array)
|
||||
// 这是KMP算法的核心预处理步骤
|
||||
void ComputeLPSArray(const char* pattern, int M, int* lps) {
|
||||
int len = 0; // length of the previous longest prefix suffix
|
||||
lps[0] = 0; // lps[0] is always 0
|
||||
int i = 1;
|
||||
|
||||
while (i < M) {
|
||||
if (pattern[i] == pattern[len]) {
|
||||
len++;
|
||||
lps[i] = len;
|
||||
i++;
|
||||
} else {
|
||||
if (len != 0) {
|
||||
len = lps[len - 1];
|
||||
} else {
|
||||
lps[i] = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. KMP 搜索函数
|
||||
// 在文本 text 中查找模式 pattern,找到后调用回调函数处理匹配位置
|
||||
void KMPSearch(const char* pattern, const char* text, void (*callback)(int)) {
|
||||
int M = strlen(pattern);
|
||||
int N = strlen(text);
|
||||
|
||||
if (M == 0) return;
|
||||
|
||||
// 创建并计算LPS数组
|
||||
int* lps = (int*)malloc(M * sizeof(int));
|
||||
ComputeLPSArray(pattern, M, lps);
|
||||
|
||||
int i = 0; // index for text
|
||||
int j = 0; // index for pattern
|
||||
|
||||
while (i < N) {
|
||||
if (pattern[j] == text[i]) {
|
||||
j++;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (j == M) {
|
||||
// 找到匹配,调用回调函数输出该行或处理
|
||||
callback(i - j);
|
||||
j = lps[j - 1];
|
||||
} else if (i < N && pattern[j] != text[i]) {
|
||||
if (j != 0) {
|
||||
j = lps[j - 1];
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(lps);
|
||||
}
|
||||
|
||||
// 3. 回调函数:用于打印匹配到的日志行号
|
||||
void PrintMatchLine(int pos) {
|
||||
// 这里简化处理,实际应用中需要根据换行符计算行号
|
||||
// 或者直接打印匹配位置附近的上下文
|
||||
printf("找到匹配 (位置: %d)\n", pos);
|
||||
}
|
||||
|
||||
// 4. 对外接口:搜索交易日志
|
||||
void SearchLogs(const char* pattern) {
|
||||
if (IsLogQueueEmpty()) {
|
||||
printf("暂无日志可供搜索。\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\n=== 搜索日志: '%s' ===\n", pattern);
|
||||
|
||||
// 简化版:遍历每一条日志进行匹配
|
||||
int i = g_logQueue.front;
|
||||
int lineNum = 1;
|
||||
|
||||
while (i != g_logQueue.rear) {
|
||||
LogEntry e = g_logQueue.logs[i];
|
||||
|
||||
// 将日志条目格式化为字符串(模拟一行文本)
|
||||
char logLine[200];
|
||||
sprintf(logLine, "ID:%d Type:%s Amount:%.2f Balance:%.2f Time:%s",
|
||||
e.account_id, e.type, e.amount, e.balance, e.timestamp);
|
||||
|
||||
// 使用KMP检查这一行是否包含模式
|
||||
// 这里为了演示调用了标准 strstr,你可以将其替换为上面的 KMPSearch
|
||||
// 但考虑到单行文本较短,KMP优势不大;如果是超长日志文件,KMP优势巨大
|
||||
|
||||
if (strstr(logLine, pattern) != NULL) {
|
||||
printf("%-3d %-8d %-10s %-10.2f %-15s\n",
|
||||
lineNum, e.account_id, e.type, e.amount, e.timestamp);
|
||||
}
|
||||
|
||||
i = (i + 1) % MAX_LOGS;
|
||||
lineNum++;
|
||||
}
|
||||
}
|
||||
+30
-28
@@ -5,96 +5,98 @@
|
||||
extern STACCOUNT g_astAccounts[];
|
||||
extern int g_iAccCount;
|
||||
|
||||
/* 函数功能:处理用户的存款操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 存款信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
/* 函数功能:处理用户的存款操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 存款信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 存款失败怎么处理?
|
||||
*/
|
||||
void Deposit(int id)
|
||||
{
|
||||
int amount;
|
||||
int temp_password;
|
||||
while (1) {
|
||||
printf("请输入存款金额");
|
||||
printf("请输入存款金额");
|
||||
scanf("%d",&amount);
|
||||
if (amount>0) {
|
||||
break;
|
||||
}
|
||||
printf("?你存给我存个负的?重新说存多少");
|
||||
printf("?你存给我存个负的?重新说存多少");
|
||||
}
|
||||
printf("请输入密码");
|
||||
printf("请输入密码");
|
||||
while (1) {
|
||||
scanf("%d",&temp_password);
|
||||
if (g_astAccounts[FindAccount(id)].password==temp_password) {
|
||||
g_astAccounts[FindAccount(id)].balance+=amount;
|
||||
printf("success");
|
||||
QueryBalance(id);
|
||||
EnqueueLog(id, "Deposit", amount, g_astAccounts[FindAccount(id)].balance);
|
||||
SaveData();
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
printf("密码错误,请重新输入");
|
||||
printf("密码错误,请重新输入");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 函数功能:处理用户的取款操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 取款后,账户信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 取款失败怎么处理?
|
||||
/* 函数功能:处理用户的取款操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 取款后,账户信息要写入全局变量g_astAccounts中
|
||||
* 返回值:无
|
||||
* 取款失败怎么处理?
|
||||
*/
|
||||
void Withdraw(int id)
|
||||
{
|
||||
int amount;
|
||||
int temp_password;
|
||||
while (1) {
|
||||
printf("请输入取款金额");
|
||||
printf("请输入取款金额");
|
||||
scanf("%d",&amount);
|
||||
if (amount>0) {
|
||||
break;
|
||||
}
|
||||
printf("?取款怎么能取负的呢");
|
||||
printf("?取款怎么能取负的呢");
|
||||
}
|
||||
while (1) {
|
||||
printf("请输入密码");
|
||||
printf("请输入密码");
|
||||
scanf("%d",&temp_password);
|
||||
if (g_astAccounts[FindAccount(id)].password==temp_password) {
|
||||
if (g_astAccounts[FindAccount(id)].balance>amount) {
|
||||
g_astAccounts[FindAccount(id)].balance-=amount;
|
||||
EnqueueLog(id, "Withdraw", amount, g_astAccounts[FindAccount(id)].balance);
|
||||
printf("success");
|
||||
QueryBalance(id);
|
||||
SaveData();
|
||||
return;
|
||||
}
|
||||
else {
|
||||
printf("有那么多钱吗你就取?\n");
|
||||
printf("有那么多钱吗你就取?\n");
|
||||
QueryBalance(id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("密码错误,请重新输入");
|
||||
printf("密码错误,请重新输入");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* 函数功能:处理用户查询余额操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 查询后全局变量是否需要变化?
|
||||
* 返回值:无
|
||||
* 查询失败怎么处理?
|
||||
/* 函数功能:处理用户查询余额操作
|
||||
需要什么步骤?
|
||||
* 参数:无
|
||||
* 查询后全局变量是否需要变化?
|
||||
* 返回值:无
|
||||
* 查询失败怎么处理?
|
||||
*/
|
||||
void QueryBalance(int id)
|
||||
{
|
||||
printf("你现在还有%f块\n",g_astAccounts[FindAccount(id)].balance);
|
||||
printf("你现在还有%f块\n",g_astAccounts[FindAccount(id)].balance);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ int GetInputInt(const char *prompt)
|
||||
printf("%s", prompt);
|
||||
while(scanf("%d", &value) != 1) {
|
||||
ClearInput();
|
||||
printf("输入错误,请重新输入: ");
|
||||
printf("输入错误,请重新输入: ");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ double GetInputDouble(const char *prompt)
|
||||
printf("%s", prompt);
|
||||
while(scanf("%lf", &value) != 1) {
|
||||
ClearInput();
|
||||
printf("输入错误,请重新输入: ");
|
||||
printf("输入错误,请重新输入: ");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user