diff --git a/bank.c b/bank.c index 4e9a264..0d76aaf 100644 --- a/bank.c +++ b/bank.c @@ -2,7 +2,7 @@ #include #include #include "bank.h" - +LogQueue g_logQueue; // <-- 新增:定义全局日志队列 STACCOUNT g_astAccounts[MAX_ACCOUNTS]; int g_iAccCount = 1; @@ -19,6 +19,7 @@ int main(void) printf("=== 简易银行账户管理系统 ===\n"); printf("欢迎使用! 系统已加载%d个账户\n", g_iAccCount); InitAdminAccount();//初始化管理员账号 + InitLogQueue(); int iChoice=-1; //选择标志 int pChoice=-1; while (1) { @@ -58,7 +59,7 @@ int main(void) } break; case 1: - printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n0. 退出账户"); + printf("1. 存款\n2. 取款\n3. 查询\n4. 显示所有账户\n5. 删除账户\n6. 创建管理员账户\n7.查看交易\n日志0. 退出账户"); pChoice=-1; scanf("%d", &pChoice); @@ -84,6 +85,9 @@ int main(void) case 6: CreateAccount(1); break; + case 7: + ShowTransactionLogs(); + break; case 0://EXIT printf("谢谢使用!\n按任意键退出"); getchar(); diff --git a/bank.h b/bank.h index 6b0ab52..c38051c 100644 --- a/bank.h +++ b/bank.h @@ -1,6 +1,6 @@ #ifndef BANK_H #define BANK_H - +#define MAX_LOGS 100 // 日志队列最大容量,防止无限增长 #define MAX_ACCOUNTS 50 //系统规格为50个账户 #define NAME_LEN 31 //用户名字最长30字符,因此需要多1个字符串结束符 @@ -51,4 +51,30 @@ 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(); // 显示所有日志 #endif diff --git a/log.c b/log.c new file mode 100644 index 0000000..7550a7b --- /dev/null +++ b/log.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#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; + } +} \ No newline at end of file diff --git a/transaction.c b/transaction.c index afa0597..d3836e3 100644 --- a/transaction.c +++ b/transaction.c @@ -31,6 +31,7 @@ void Deposit(int id) g_astAccounts[FindAccount(id)].balance+=amount; printf("success"); QueryBalance(id); + EnqueueLog(id, "Deposit", amount, g_astAccounts[FindAccount(id)].balance); SaveData(); break; } @@ -68,6 +69,7 @@ void Withdraw(int id) 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();