3 Commits
Author SHA1 Message Date
Idiot-awa 74e467e111 增加日志 2026-05-08 12:39:58 +08:00
Idiot-awa 7d623c1993 增加日志 2026-05-08 11:46:09 +08:00
idiot ae99d48eda 更改编码为 utf8 2026-04-22 17:32:22 +08:00
8 changed files with 306 additions and 140 deletions
+5
View File
@@ -0,0 +1,5 @@
*.o
*.exe
bank.dat
.vscode/launch.json
.vscode/tasks.json
+57 -57
View File
@@ -6,15 +6,15 @@
extern STACCOUNT g_astAccounts[]; extern STACCOUNT g_astAccounts[];
extern int g_iAccCount; extern int g_iAccCount;
/* 函数功能:处理用户的创建账户操作 /* 函数功能:处理用户的创建账户操作
思考处理步骤,写到函数体内注释 思考处理步骤,写到函数体内注释
* 参数:无 * 参数:无
* 存款信息要写入全局变量g_astAccounts中 * 存款信息要写入全局变量g_astAccounts中
* 返回值:无 * 返回值:无
* 存款失败怎么处理? * 存款失败怎么处理?
*/ */
void InitAdminAccount() { void InitAdminAccount() {
printf("初始化默认管理员账户"); printf("初始化默认管理员账户");
g_astAccounts[0].id = 1000; g_astAccounts[0].id = 1000;
strcpy(g_astAccounts[0].name, "admin"); strcpy(g_astAccounts[0].name, "admin");
g_astAccounts[0].balance = 0.0; g_astAccounts[0].balance = 0.0;
@@ -25,73 +25,73 @@ void InitAdminAccount() {
void CreateAccount(int quanxian) void CreateAccount(int quanxian)
{ {
// (1)合法性检查 // (1)合法性检查
if(MAX_ACCOUNTS < g_iAccCount) if(MAX_ACCOUNTS < g_iAccCount)
{ {
printf("账户数量已达上限!\n"); printf("账户数量已达上限!\n");
return; return;
} }
// (2)定义局部变量(函数内使用的变量) // (2)定义局部变量(函数内使用的变量)
STACCOUNT stNewAcc; //账户变量,结构体变量定义 STACCOUNT stNewAcc; //账户变量,结构体变量定义
//char cInput[NAME_LEN]; //接输入的名字字符串 //char cInput[NAME_LEN]; //接输入的名字字符串
// (3)功能实现 // (3)功能实现
printf("请输入姓名(最多30个字符): "); printf("请输入姓名(最多30个字符): ");
//这里有风险,怎么改进? //这里有风险,怎么改进?
scanf("%30s", stNewAcc.name); scanf("%30s", stNewAcc.name);
//stNewAcc.name[NAME_LEN - 1] = '\0'; // 确保null终止 //stNewAcc.name[NAME_LEN - 1] = '\0'; // 确保null终止
//输密码 //输密码
int temp_password; int temp_password;
while (1) { while (1) {
printf("请输入密码\n"); printf("请输入密码\n");
scanf("%d",&temp_password); scanf("%d",&temp_password);
if (99999<temp_password&&temp_password<999999) { if (99999<temp_password&&temp_password<999999) {
printf("设置成功\n"); printf("设置成功\n");
stNewAcc.password=temp_password; stNewAcc.password=temp_password;
SaveData(); SaveData();
break; break;
} }
printf("密码不符合规范!请重新输入\n"); printf("密码不符合规范!请重新输入\n");
} }
//需求中要求账户ID从1000开始,初始化账户余额 //需求中要求账户ID从1000开始,初始化账户余额
stNewAcc.id = 1000 + g_iAccCount; stNewAcc.id = 1000 + g_iAccCount;
stNewAcc.balance = 0.0; stNewAcc.balance = 0.0;
stNewAcc.quanxian = quanxian; stNewAcc.quanxian = quanxian;
g_astAccounts[g_iAccCount] = stNewAcc; g_astAccounts[g_iAccCount] = stNewAcc;
//把记录账户数量的全局变量自增。加过之后自增,所以下标0占用,数量是1个 //把记录账户数量的全局变量自增。加过之后自增,所以下标0占用,数量是1个
g_iAccCount++; g_iAccCount++;
printf("开户成功! 账户ID: %d\n", stNewAcc.id); printf("开户成功! 账户ID: %d\n", stNewAcc.id);
SaveData(); SaveData();
} }
/* 函数功能:处理用户的显示所有用户信息的操作 /* 函数功能:处理用户的显示所有用户信息的操作
思考处理步骤,写到函数体内注释 思考处理步骤,写到函数体内注释
* 参数:无 * 参数:无
* 从全局变量g_astAccounts中取信息,并显示 * 从全局变量g_astAccounts中取信息,并显示
* 返回值:无 * 返回值:无
* 存款失败怎么处理? * 存款失败怎么处理?
*/ */
void ShowAccounts() void ShowAccounts()
{ {
//(1)输出用户内容提示 //(1)输出用户内容提示
printf("\n所有账户信息:\n"); printf("\n所有账户信息:\n");
//(2)检查是否有账户了?异常情况无账户的处理 //(2)检查是否有账户了?异常情况无账户的处理
if (0 == g_iAccCount) if (0 == g_iAccCount)
{ {
printf("暂无账户信息\n"); printf("暂无账户信息\n");
return; return;
} }
//(3)打印表头 //(3)打印表头
printf("%-8s %-30s %-10s %-3s\n", "ID", "姓名", "余额","权限"); printf("%-8s %-30s %-10s %-3s\n", "ID", "姓名", "余额","权限");
printf("------------------------------------------------------\n"); printf("------------------------------------------------------\n");
//(4)循环打印每一行 //(4)循环打印每一行
for(int i = 0; i < g_iAccCount; i++) for(int i = 0; i < g_iAccCount; i++)
{ {
printf("%-8d %-30s %-10.2f %-10d\n", printf("%-8d %-30s %-10.2f %-10d\n",
@@ -99,37 +99,37 @@ void ShowAccounts()
} }
} }
//登录账户,返回权限 //登录账户,返回权限
int LoginAccount(int temp_id) { int LoginAccount(int temp_id) {
int temp_password; int temp_password;
if (FindAccount(temp_id)==-1) { if (FindAccount(temp_id)==-1) {
printf("没有找到账户\n"); printf("没有找到账户\n");
return 0; return 0;
} }
else { else {
while (1) { while (1) {
printf("请输入密码"); printf("请输入密码");
scanf("%d", &temp_password); scanf("%d", &temp_password);
if (g_astAccounts[FindAccount(temp_id)].password==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) { if (g_astAccounts[FindAccount(temp_id)].quanxian==1) {
printf("您目前以管理员身份登录"); printf("您目前以管理员身份登录");
return 1; return 1;
} }
else {return 2;} else {return 2;}
} }
else { else {
printf("密码错误,请重新输入\n"); printf("密码错误,请重新输入\n");
} }
} }
} }
} }
/* 函数功能:根据输入的用户ID找到 /* 函数功能:根据输入的用户ID找到
思考处理步骤,写到函数体内注释 思考处理步骤,写到函数体内注释
* 参数:无 * 参数:无
* 用户ID跟数组ID * 用户ID跟数组ID
* 返回值:无 * 返回值:无
* 存款失败怎么处理? * 存款失败怎么处理?
*/ */
int FindAccount(int id) int FindAccount(int id)
{ {
@@ -140,15 +140,15 @@ int FindAccount(int id)
return -1; return -1;
} }
//删除账户 //删除账户
void DeleteAccount(int id) { void DeleteAccount(int id) {
printf("正在删除\n"); printf("正在删除\n");
int i=FindAccount(id); int i=FindAccount(id);
if (i==-1) { if (i==-1) {
printf("有这个账户吗你就删"); printf("有这个账户吗你就删");
} }
else { else {
printf("正在删除一下账户..."); printf("正在删除一下账户...");
printf("%-8d %-30s %-10.2f %-3d\n", printf("%-8d %-30s %-10.2f %-3d\n",
g_astAccounts[i].id, g_astAccounts[i].name, g_astAccounts[i].balance,g_astAccounts[i].quanxian); g_astAccounts[i].id, g_astAccounts[i].name, g_astAccounts[i].balance,g_astAccounts[i].quanxian);
for (; i < g_iAccCount-i; i++) for (; i < g_iAccCount-i; i++)
@@ -162,24 +162,24 @@ void DeleteAccount(int id) {
void ChangePassword(int id) { void ChangePassword(int id) {
int temp_password; int temp_password;
while (1) { while (1) {
printf("请输入原密码"); printf("请输入原密码");
scanf("%d",&temp_password); scanf("%d",&temp_password);
if (g_astAccounts[FindAccount(id)].password==temp_password) { if (g_astAccounts[FindAccount(id)].password==temp_password) {
while (1) { while (1) {
printf("请输入新密码\n"); printf("请输入新密码\n");
scanf("%d",&temp_password); scanf("%d",&temp_password);
if (99999<temp_password&&temp_password<999999) { if (99999<temp_password&&temp_password<999999) {
printf("修改成功\n"); printf("修改成功\n");
g_astAccounts[FindAccount(id)].password=temp_password; g_astAccounts[FindAccount(id)].password=temp_password;
SaveData(); SaveData();
break; break;
} }
printf("密码不符合规范!请重新输入\n"); printf("密码不符合规范!请重新输入\n");
} }
} }
else { else {
printf("密码错误,请重新输入"); printf("密码错误,请重新输入");
} }
} }
} }
+41 -37
View File
@@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "bank.h" #include "bank.h"
LogQueue g_logQueue; // <-- 新增:定义全局日志队列
STACCOUNT g_astAccounts[MAX_ACCOUNTS]; STACCOUNT g_astAccounts[MAX_ACCOUNTS];
int g_iAccCount = 1; int g_iAccCount = 1;
@@ -12,53 +12,54 @@ int d_id;
int main(void) int main(void)
{ {
LoadData(); LoadData();
//(1)导入数据 //(1)导入数据
// which fuction? // which fuction?
//应该先导入数据再欢迎?还是欢迎了再导入数据? //应该先导入数据再欢迎?还是欢迎了再导入数据?
printf("=== 简易银行账户管理系统 ===\n"); printf("=== 简易银行账户管理系统 ===\n");
printf("欢迎使用! 系统已加载%d个账户\n", g_iAccCount); printf("欢迎使用! 系统已加载%d个账户\n", g_iAccCount);
InitAdminAccount();//初始化管理员账号 InitAdminAccount();//初始化管理员账号
int iChoice=-1; //选择标志 InitLogQueue();
int iChoice=-1; //选择标志
int pChoice=-1; int pChoice=-1;
while (1) { while (1) {
//持续显示菜单的秘诀 //持续显示菜单的秘诀
//(2)显示主菜单,并且处理用户的选择 //(2)显示主菜单,并且处理用户的选择
if (quanxian==-2){break;} if (quanxian==-2){break;}
printf("\n=== 简易银行账户管理系统 ===\n"); printf("\n=== 简易银行账户管理系统 ===\n");
switch(quanxian) { switch(quanxian) {
case 0: case 0:
printf("1. 开户\n2. 登录 \n0. 退出\n"); printf("1. 开户\n2. 登录 \n0. 退出\n");
//printf("1. 开户\n2. 存款\n3. 取款\n4. 查询\n5. 显示所有账户\n6. 退出\n"); //printf("1. 开户\n2. 存款\n3. 取款\n4. 查询\n5. 显示所有账户\n6. 退出\n");
printf("请选择: "); printf("请选择: ");
iChoice=-1; iChoice=-1;
scanf("%d", &iChoice); scanf("%d", &iChoice);
switch(iChoice) { switch(iChoice) {
case 1://CREATE case 1://CREATE
//调用创建账号的函数 //调用创建账号的函数
CreateAccount(2); CreateAccount(2);
quanxian=0; quanxian=0;
break; break;
case 2://LOGIN case 2://LOGIN
printf("请输入账户id"); printf("请输入账户id");
scanf("%d", &user_id); scanf("%d", &user_id);
quanxian=LoginAccount(user_id); quanxian=LoginAccount(user_id);
break; break;
case 0://EXIT case 0://EXIT
printf("谢谢使用!\n按任意键退出\n"); printf("谢谢使用!\n按任意键退出\n");
getchar(); getchar();
quanxian=-2; quanxian=-2;
exit(0); exit(0);
//还要实现哪些菜单吗? //还要实现哪些菜单吗?
//如果要做排序展示,要做什么? //如果要做排序展示,要做什么?
default: //好的编码习惯,对无效值做处理 default: //好的编码习惯,对无效值做处理
printf("无效选择!\n"); printf("无效选择!\n");
break; //好的编码加上break,万一default挪了个位置呢? break; //好的编码加上break,万一default挪了个位置呢?
} }
break; break;
case 1: case 1:
printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n0. 退出账户"); printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n7.查看交易\n日志0. 退出账户");
pChoice=-1; pChoice=-1;
scanf("%d", &pChoice); scanf("%d", &pChoice);
@@ -76,7 +77,7 @@ int main(void)
ShowAccounts(); ShowAccounts();
break; break;
case 5: case 5:
printf("请输入要删除的id"); printf("请输入要删除的id");
scanf("%d", &d_id); scanf("%d", &d_id);
DeleteAccount(d_id); DeleteAccount(d_id);
g_iAccCount--; g_iAccCount--;
@@ -84,20 +85,23 @@ int main(void)
case 6: case 6:
CreateAccount(1); CreateAccount(1);
break; break;
case 7:
ShowTransactionLogs();
break;
case 0://EXIT case 0://EXIT
printf("谢谢使用!\n按任意键退出"); printf("谢谢使用!\n按任意键退出");
getchar(); getchar();
quanxian=-2; quanxian=-2;
//还要实现哪些菜单吗? //还要实现哪些菜单吗?
//如果要做排序展示,要做什么? //如果要做排序展示,要做什么?
default: //好的编码习惯,对无效值做处理 default: //好的编码习惯,对无效值做处理
printf("无效选择!\n"); printf("无效选择!\n");
break; //好的编码加上break,万一default挪了个位置呢? break; //好的编码加上break,万一default挪了个位置呢?
} }
break; break;
case 2: case 2:
printf("1. 存款\n2. 取款\n3. 查询\n0.退出账户"); printf("1. 存款\n2. 取款\n3. 查询\n0.退出账户");
pChoice=-1; pChoice=-1;
scanf("%d", &pChoice); scanf("%d", &pChoice);
switch(pChoice) { switch(pChoice) {
@@ -111,22 +115,22 @@ int main(void)
QueryBalance(user_id); QueryBalance(user_id);
break; break;
case 0://EXIT case 0://EXIT
printf("谢谢使用!\n按任意键退出"); printf("谢谢使用!\n按任意键退出");
getchar(); getchar();
quanxian=-2; quanxian=-2;
break; break;
//还要实现哪些菜单吗? //还要实现哪些菜单吗?
//如果要做排序展示,要做什么? //如果要做排序展示,要做什么?
default: //好的编码习惯,对无效值做处理 default: //好的编码习惯,对无效值做处理
printf("无效选择!\n"); printf("无效选择!\n");
break; //好的编码加上break,万一default挪了个位置呢? break; //好的编码加上break,万一default挪了个位置呢?
} }
break; break;
} }
} }
//(3)调用保存数据的函数 //(3)调用保存数据的函数
// which fuction? // which fuction?
return 0; return 0;
+42 -16
View File
@@ -1,10 +1,10 @@
#ifndef BANK_H #ifndef BANK_H
#define 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个账户 // 账户信息,每个账户id对应了名字、账户余额等信息,是不是用数据结构定义比较合适?
#define NAME_LEN 31 //用户名字最长30字符,因此需要多1个字符串结束符
// 账户信息,每个账户id对应了名字、账户余额等信息,是不是用数据结构定义比较合适?
typedef struct strAccount typedef struct strAccount
{ {
int id; int id;
@@ -16,39 +16,65 @@ typedef struct strAccount
// strAccount STACCOUNT; // strAccount STACCOUNT;
// 函数声明,main函数中调用的各模块函数需要在程序公用的.h中声明 // 函数声明,main函数中调用的各模块函数需要在程序公用的.h中声明
// 公开出来被其他模块调用的函数,也应该在公用的.h中声明 // 公开出来被其他模块调用的函数,也应该在公用的.h中声明
//创建账户的函数 //创建账户的函数
void CreateAccount(int quanxian); void CreateAccount(int quanxian);
//实现存款的函数 //实现存款的函数
void Deposit(int id); void Deposit(int id);
//实现取款的函数 //实现取款的函数
void Withdraw(int id); void Withdraw(int id);
//查询余额的函数 //查询余额的函数
void QueryBalance(int id); void QueryBalance(int id);
//显示全部账户的函数 //显示全部账户的函数
void ShowAccounts(); void ShowAccounts();
//数据持久化函数 //数据持久化函数
void SaveData(); void SaveData();
//加载数据的函数 //加载数据的函数
void LoadData(); void LoadData();
//根据ID查找账户数组id的函数 //根据ID查找账户数组id的函数
int FindAccount(int iD); int FindAccount(int iD);
//登录函数,返回用户权限 //登录函数,返回用户权限
int LoginAccount(int temp_id); int LoginAccount(int temp_id);
//销户 //销户
void DeleteAccount(int id); void DeleteAccount(int id);
void InitAdminAccount(); 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(); // 显示所有日志
#endif #endif
+55
View File
@@ -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;
}
+74
View File
@@ -0,0 +1,74 @@
#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;
}
}
+30 -28
View File
@@ -5,96 +5,98 @@
extern STACCOUNT g_astAccounts[]; extern STACCOUNT g_astAccounts[];
extern int g_iAccCount; extern int g_iAccCount;
/* 函数功能:处理用户的存款操作 /* 函数功能:处理用户的存款操作
需要什么步骤? 需要什么步骤?
* 参数:无 * 参数:无
* 存款信息要写入全局变量g_astAccounts中 * 存款信息要写入全局变量g_astAccounts中
* 返回值:无 * 返回值:无
* 存款失败怎么处理? * 存款失败怎么处理?
*/ */
void Deposit(int id) void Deposit(int id)
{ {
int amount; int amount;
int temp_password; int temp_password;
while (1) { while (1) {
printf("请输入存款金额"); printf("请输入存款金额");
scanf("%d",&amount); scanf("%d",&amount);
if (amount>0) { if (amount>0) {
break; break;
} }
printf("?你存给我存个负的?重新说存多少"); printf("?你存给我存个负的?重新说存多少");
} }
printf("请输入密码"); printf("请输入密码");
while (1) { while (1) {
scanf("%d",&temp_password); scanf("%d",&temp_password);
if (g_astAccounts[FindAccount(id)].password==temp_password) { if (g_astAccounts[FindAccount(id)].password==temp_password) {
g_astAccounts[FindAccount(id)].balance+=amount; g_astAccounts[FindAccount(id)].balance+=amount;
printf("success"); printf("success");
QueryBalance(id); QueryBalance(id);
EnqueueLog(id, "Deposit", amount, g_astAccounts[FindAccount(id)].balance);
SaveData(); SaveData();
break; break;
} }
else else
{ {
{ {
printf("密码错误,请重新输入"); printf("密码错误,请重新输入");
} }
} }
} }
} }
/* 函数功能:处理用户的取款操作 /* 函数功能:处理用户的取款操作
需要什么步骤? 需要什么步骤?
* 参数:无 * 参数:无
* 取款后,账户信息要写入全局变量g_astAccounts中 * 取款后,账户信息要写入全局变量g_astAccounts中
* 返回值:无 * 返回值:无
* 取款失败怎么处理? * 取款失败怎么处理?
*/ */
void Withdraw(int id) void Withdraw(int id)
{ {
int amount; int amount;
int temp_password; int temp_password;
while (1) { while (1) {
printf("请输入取款金额"); printf("请输入取款金额");
scanf("%d",&amount); scanf("%d",&amount);
if (amount>0) { if (amount>0) {
break; break;
} }
printf("?取款怎么能取负的呢"); printf("?取款怎么能取负的呢");
} }
while (1) { while (1) {
printf("请输入密码"); printf("请输入密码");
scanf("%d",&temp_password); scanf("%d",&temp_password);
if (g_astAccounts[FindAccount(id)].password==temp_password) { if (g_astAccounts[FindAccount(id)].password==temp_password) {
if (g_astAccounts[FindAccount(id)].balance>amount) { if (g_astAccounts[FindAccount(id)].balance>amount) {
g_astAccounts[FindAccount(id)].balance-=amount; g_astAccounts[FindAccount(id)].balance-=amount;
EnqueueLog(id, "Withdraw", amount, g_astAccounts[FindAccount(id)].balance);
printf("success"); printf("success");
QueryBalance(id); QueryBalance(id);
SaveData(); SaveData();
return; return;
} }
else { else {
printf("有那么多钱吗你就取?\n"); printf("有那么多钱吗你就取?\n");
QueryBalance(id); QueryBalance(id);
return; return;
} }
} }
else { else {
printf("密码错误,请重新输入"); printf("密码错误,请重新输入");
} }
} }
} }
/* 函数功能:处理用户查询余额操作 /* 函数功能:处理用户查询余额操作
需要什么步骤? 需要什么步骤?
* 参数:无 * 参数:无
* 查询后全局变量是否需要变化? * 查询后全局变量是否需要变化?
* 返回值:无 * 返回值:无
* 查询失败怎么处理? * 查询失败怎么处理?
*/ */
void QueryBalance(int id) void QueryBalance(int id)
{ {
printf("你现在还有%f块\n",g_astAccounts[FindAccount(id)].balance); printf("你现在还有%f块\n",g_astAccounts[FindAccount(id)].balance);
return; return;
} }
+2 -2
View File
@@ -13,7 +13,7 @@ int GetInputInt(const char *prompt)
printf("%s", prompt); printf("%s", prompt);
while(scanf("%d", &value) != 1) { while(scanf("%d", &value) != 1) {
ClearInput(); ClearInput();
printf("输入错误,请重新输入: "); printf("输入错误,请重新输入: ");
} }
return value; return value;
} }
@@ -24,7 +24,7 @@ double GetInputDouble(const char *prompt)
printf("%s", prompt); printf("%s", prompt);
while(scanf("%lf", &value) != 1) { while(scanf("%lf", &value) != 1) {
ClearInput(); ClearInput();
printf("输入错误,请重新输入: "); printf("输入错误,请重新输入: ");
} }
return value; return value;
} }