Files

182 lines
5.6 KiB
C
Raw Permalink Normal View History

2026-04-06 21:36:26 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bank.h"
extern STACCOUNT g_astAccounts[];
extern int g_iAccCount;
2026-04-22 17:32:22 +08:00
/* 函数功能:处理用户的创建账户操作
思考处理步骤,写到函数体内注释
* 参数:无
* 存款信息要写入全局变量g_astAccounts中
* 返回值:无
* 存款失败怎么处理?
2026-04-06 21:36:26 +08:00
*/
void InitAdminAccount() {
2026-04-22 17:32:22 +08:00
printf("初始化默认管理员账户");
2026-04-06 21:36:26 +08:00
g_astAccounts[0].id = 1000;
strcpy(g_astAccounts[0].name, "admin");
g_astAccounts[0].balance = 0.0;
g_astAccounts[0].password = 123456;
g_astAccounts[0].quanxian = 1;
SaveData();
}
void CreateAccount(int quanxian)
{
2026-04-22 17:32:22 +08:00
// (1)合法性检查
2026-04-06 21:36:26 +08:00
if(MAX_ACCOUNTS < g_iAccCount)
{
2026-04-22 17:32:22 +08:00
printf("账户数量已达上限!\n");
2026-04-06 21:36:26 +08:00
return;
}
2026-04-22 17:32:22 +08:00
// (2)定义局部变量(函数内使用的变量)
STACCOUNT stNewAcc; //账户变量,结构体变量定义
//char cInput[NAME_LEN]; //接输入的名字字符串
2026-04-06 21:36:26 +08:00
2026-04-22 17:32:22 +08:00
// (3)功能实现
printf("请输入姓名(最多30个字符): ");
2026-04-06 21:36:26 +08:00
2026-04-22 17:32:22 +08:00
//这里有风险,怎么改进?
2026-04-06 21:36:26 +08:00
scanf("%30s", stNewAcc.name);
2026-04-22 17:32:22 +08:00
//stNewAcc.name[NAME_LEN - 1] = '\0'; // 确保null终止
//输密码
2026-04-06 21:36:26 +08:00
int temp_password;
while (1) {
2026-04-22 17:32:22 +08:00
printf("请输入密码\n");
2026-04-06 21:36:26 +08:00
scanf("%d",&temp_password);
if (99999<temp_password&&temp_password<999999) {
2026-04-22 17:32:22 +08:00
printf("设置成功\n");
2026-04-06 21:36:26 +08:00
stNewAcc.password=temp_password;
SaveData();
break;
}
2026-04-22 17:32:22 +08:00
printf("密码不符合规范!请重新输入\n");
2026-04-06 21:36:26 +08:00
}
2026-04-22 17:32:22 +08:00
//需求中要求账户ID从1000开始,初始化账户余额
2026-04-06 21:36:26 +08:00
stNewAcc.id = 1000 + g_iAccCount;
stNewAcc.balance = 0.0;
stNewAcc.quanxian = quanxian;
g_astAccounts[g_iAccCount] = stNewAcc;
2026-04-22 17:32:22 +08:00
//把记录账户数量的全局变量自增。加过之后自增,所以下标0占用,数量是1个
HashInsert(stNewAcc.id, g_iAccCount);
2026-04-06 21:36:26 +08:00
g_iAccCount++;
2026-04-22 17:32:22 +08:00
printf("开户成功! 账户ID: %d\n", stNewAcc.id);
2026-04-06 21:36:26 +08:00
SaveData();
}
2026-04-22 17:32:22 +08:00
/* 函数功能:处理用户的显示所有用户信息的操作
思考处理步骤,写到函数体内注释
* 参数:无
* 从全局变量g_astAccounts中取信息,并显示
* 返回值:无
* 存款失败怎么处理?
2026-04-06 21:36:26 +08:00
*/
void ShowAccounts()
{
2026-04-22 17:32:22 +08:00
//(1)输出用户内容提示
printf("\n所有账户信息:\n");
2026-04-06 21:36:26 +08:00
2026-04-22 17:32:22 +08:00
//(2)检查是否有账户了?异常情况无账户的处理
2026-04-06 21:36:26 +08:00
if (0 == g_iAccCount)
{
2026-04-22 17:32:22 +08:00
printf("暂无账户信息\n");
2026-04-06 21:36:26 +08:00
return;
}
2026-04-22 17:32:22 +08:00
//(3)打印表头
printf("%-8s %-30s %-10s %-3s\n", "ID", "姓名", "余额","权限");
2026-04-06 21:36:26 +08:00
printf("------------------------------------------------------\n");
2026-04-22 17:32:22 +08:00
//(4)循环打印每一行
2026-04-06 21:36:26 +08:00
for(int i = 0; i < g_iAccCount; i++)
{
printf("%-8d %-30s %-10.2f %-10d\n",
g_astAccounts[i].id, g_astAccounts[i].name, g_astAccounts[i].balance,g_astAccounts[i].quanxian);
}
}
2026-04-22 17:32:22 +08:00
//登录账户,返回权限
2026-04-06 21:36:26 +08:00
int LoginAccount(int temp_id) {
int temp_password;
if (FindAccount(temp_id)==-1) {
2026-04-22 17:32:22 +08:00
printf("没有找到账户\n");
2026-04-06 21:36:26 +08:00
return 0;
}
else {
while (1) {
2026-04-22 17:32:22 +08:00
printf("请输入密码");
2026-04-06 21:36:26 +08:00
scanf("%d", &temp_password);
if (g_astAccounts[FindAccount(temp_id)].password==temp_password) {
2026-04-22 17:32:22 +08:00
printf("欢迎用户%s \n",g_astAccounts[FindAccount(temp_id)].name);
2026-04-06 21:36:26 +08:00
if (g_astAccounts[FindAccount(temp_id)].quanxian==1) {
2026-04-22 17:32:22 +08:00
printf("您目前以管理员身份登录");
2026-04-06 21:36:26 +08:00
return 1;
}
else {return 2;}
}
else {
2026-04-22 17:32:22 +08:00
printf("密码错误,请重新输入\n");
2026-04-06 21:36:26 +08:00
}
}
}
}
2026-04-22 17:32:22 +08:00
/* 函数功能:根据输入的用户ID找到
思考处理步骤,写到函数体内注释
* 参数:无
* 用户ID跟数组ID
* 返回值:无
* 存款失败怎么处理?
2026-04-06 21:36:26 +08:00
*/
int FindAccount(int id)
{
return HashSearch(id);
2026-04-06 21:36:26 +08:00
}
2026-04-22 17:32:22 +08:00
//删除账户
2026-04-06 21:36:26 +08:00
void DeleteAccount(int id) {
2026-04-22 17:32:22 +08:00
printf("正在删除\n");
2026-04-06 21:36:26 +08:00
int i=FindAccount(id);
if (i==-1) {
2026-04-22 17:32:22 +08:00
printf("有这个账户吗你就删");
2026-04-06 21:36:26 +08:00
}
else {
2026-04-22 17:32:22 +08:00
printf("正在删除一下账户...");
2026-04-06 21:36:26 +08:00
printf("%-8d %-30s %-10.2f %-3d\n",
g_astAccounts[i].id, g_astAccounts[i].name, g_astAccounts[i].balance,g_astAccounts[i].quanxian);
HashDelete(id);
2026-04-06 21:36:26 +08:00
for (; i < g_iAccCount-i; i++)
{
g_astAccounts[i]=g_astAccounts[i+1];
}
printf("sucess");
SaveData();
}
}
void ChangePassword(int id) {
int temp_password;
while (1) {
2026-04-22 17:32:22 +08:00
printf("请输入原密码");
2026-04-06 21:36:26 +08:00
scanf("%d",&temp_password);
if (g_astAccounts[FindAccount(id)].password==temp_password) {
while (1) {
2026-04-22 17:32:22 +08:00
printf("请输入新密码\n");
2026-04-06 21:36:26 +08:00
scanf("%d",&temp_password);
if (99999<temp_password&&temp_password<999999) {
2026-04-22 17:32:22 +08:00
printf("修改成功\n");
2026-04-06 21:36:26 +08:00
g_astAccounts[FindAccount(id)].password=temp_password;
SaveData();
break;
}
2026-04-22 17:32:22 +08:00
printf("密码不符合规范!请重新输入\n");
2026-04-06 21:36:26 +08:00
}
}
else {
2026-04-22 17:32:22 +08:00
printf("密码错误,请重新输入");
2026-04-06 21:36:26 +08:00
}
}
}