开始
│
▼
[登录页面] ---> [管理员登录]
│ │
│ ├──> [通过操作日期排序]
│ │
│ ├──> [通过姓氏排序]
│ │
│ ├──> [通过姓名账号查询记录]
│ │
│ └──> [开户]
│
▼
[用户登录] ---> [存钱]
│
├──> [取钱]
│
└──> [查询余额]
│
结束
typedef struct { int id; // 账号
char name[50]; // 姓名
char lastName[50];
char gender; // 性别,'M'表示男性,'F'表示女性
int age; // 年龄
char phone[15]; // 电话号码
int password; // 密码
float balance; // 存款总金额
// 这里不包括具体的存取款记录和日期struct Transaction {char type; // 存取款类型,'D'表示存款,'W'表示取款
float amount; // 金额
time_t time;
}
transactions[MAX_TRANSACTIONS]; // 存取款记录
int numTransactions; // 存取款记录数
} Customer;
Customer customers[MAX_CUSTOMERS]; // 客户数组建立一个结构体自定义以下函数实现其各自功能
int loggedInCustomerIndex = -1;void pass1();
//用户密码登录void pass2();
//管理员密码登录int welcomemenu();
欢迎界面void welcome();
用户选择void qk();
用户取款功能实现void ck();
用户存款功能实现void cxye();
用户查找余额int yhmenu();
用户登录成功后菜单void cqkjl();
管理员查询存取款记录的实现void czjl();
管理员通过账号姓名查找记录void addcustomer();
添加客户void yhchoice();
用户选择int glymenu();
管理员菜单
int search(int id);
search函数的实现
void sortByLastName();
通过姓氏排序void sortByDate();
通过操作日期排序int glycxmenu();
管理员查询菜单void glycxchoice();
管理员查询选择存款功能 :
实现思想:用户输入存款金额,系统更新账户余额,并记录交易信息。实现过程:提示用户输入存款金额。检查输入的金额是否有效。更新账户余额:customers[i].balance += am
下面展示代码
#define _CRT_SECURE_NO_WARNINGS
#define MAX_CUSTOMERS 100 // 假设最大客户数为100
#define MAX_TRANSACTIONS 1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include<windows.h>
int loggedInCustomerIndex = -1;
void pass1();
void pass2();
int welcomemenu();
void welcome();
void qk();
void ck();
void cxye();
int yhmenu();
void cqkjl();
void czjl();
void addcustomer();
void yhchoice();
int glymenu();
int search(int id);
void sortByLastName();
void sortByDate();
int glycxmenu();
void glycxchoice();
typedef struct {
int id; // 账号
char name[50]; // 姓名
char lastName[50];
char gender; // 性别,'M'表示男性,'F'表示女性
int age; // 年龄
char phone[15]; // 电话号码
int password; // 密码
float balance; // 存款总金额
// 这里不包括具体的存取款记录和日期
struct Transaction {
char type; // 存取款类型,'D'表示存款,'W'表示取款
float amount; // 金额
time_t time;
} transactions[MAX_TRANSACTIONS]; // 存取款记录
int numTransactions; // 存取款记录数
} Customer;
Customer customers[MAX_CUSTOMERS]; // 客户数组
int numCustomers = 0;
int welcomemenu()
{
int i;
while (1)
{
printf("+--------------------------------+\n");
printf("+ +\n");
printf("+ 欢迎使用银行管理系统 +\n");
printf("+ +\n");
printf("+ 1 用户登录 2 管理员登录 +\n");
printf("+ 0 退出 +\n");
printf("+ 请输入 +\n");
printf("+ +\n");
printf("+--------------------------------+\n");
scanf("%d", &i);
while (getchar() != '\n');
if (i >= 0 && i <= 2)
break;
else
printf("输入错误,请重新输入");
}
return i;
}
void welcome()
{
int choice=2;
while (choice)
{
choice = welcomemenu();
switch (choice)
{
case 1:
pass1();
break;
case 2:
pass2();
break;
case 0:
printf("欢迎您下次再来。\n");
}
}
}
void pass1()
{
int Id;
int Password;
int i;
bool found = false;
printf("请输入账号: ");
scanf("%d", &Id);
while (getchar() != '\n');
for (i = 0; i < numCustomers; i++)
{
if (Id == customers[i].id)
{
found = true;
printf("请输入密码: ");
scanf("%d", &Password);
while (getchar() != '\n');
if (Password == customers[i].password)
{
printf("登录成功!\n");
loggedInCustomerIndex = i; // 记录登录成功的账号索引
yhchoice();
break;
}
else
{
printf("密码错误,登录失败。\n");
break;
}
}
}
if (!found)
{
printf("该账号不存在,登录失败\n");
}
}
int yhmenu()
{
int i=0;
while (1)
{
printf("+-----------------------------+\n");
printf("+ +\n");
printf("+ 1 查询余额 +\n");
printf("+ 2 存款 +\n");
printf("+ 3 取款 +\n");
printf("+ 0 返回 +\n");
printf("+ +\n");
printf("+-----------------------------+\n");
scanf("%d", &i);
if (i >= 0 && i <= 3)
{
break;
}
else
{
printf("输入错误,请重新输入");
}
}
return i;
}
void yhchoice()
{
int Choice =1;
while (Choice)
{
Choice = yhmenu();
switch (Choice)
{
case 1:
cxye();
break;
case 2:
ck();
break;
case 3:
qk();
break;
case 0:
printf("欢迎下次再来。\n");
}
}
}
//管理员登陆
//管理员菜单
int glymenu()
{
int i;
while (1)
{
printf("+-----------------------------+\n");
printf("+ +\n");
printf("+ 1 查找记录 +\n");
printf("+ 2 开户 +\n");
printf("+ 3 查询用户信息 +\n");
printf("+ 0 返回 +\n");
printf("+ +\n");
printf("+-----------------------------+\n");
scanf("%d", &i);
while (getchar() != '\n');
if (i >= 0 && i <= 4)
{
break;
}
else
printf("输入错误,请重新输入");
}
return i;
}
void glychoice()
{
int choice2 = 1;
while (choice2)
{
choice2 = glymenu();
switch (choice2)
{
case 1:
czjl();
break;
case 2:
addcustomer();
break;
case 3:
glycxchoice();
break;
case 0:
printf("欢迎您下次再来\n");
break;
}
}
}
void pass2()
{
char adminname[10];
char adminpwd[10];
int i;
for (i = 0; i < 3; i++)
{
printf("请输入用户名:");
scanf("%s", adminname);
while (getchar() != '\n');
printf("请输入密码:");
scanf("%s", adminpwd);
while (getchar() != '\n');
if((strcmp(adminname,"fyh")==0)&&(strcmp(adminpwd,"123")==0))
{
glychoice();
return;// 为什么要用return
}
else
{
if (i < 2)
{
printf("用户名或密码错误,请重新输入。\n");
continue;
}
else
{
printf("用户名或密码错误,退出系统.\n");
}
}
}
}
int glycxmenu()
{
int i;
while (1)
{
printf("+--------------------------------+\n");
printf("+ +\n");
printf("+ 1 按姓氏查询 +\n");
printf("+ 2 按操作日期查询 +\n");
printf("+ 0 返回 +\n");
printf("+ +\n");
printf("+ 请输入 +\n");
printf("+ +\n");
printf("+--------------------------------+\n");
scanf("%d", &i);
while (getchar() != '\n');
if (i >= 0 && i <= 2)
break;
else
printf("输入错误,请重新输入");
}
return i;
}
void glycxchoice()
{
int choice3 = 1;
while (choice3)
{
choice3 = glycxmenu();
switch (choice3)
{
case 1:
sortByLastName();
break;
case 2:
sortByDate();
break;
case 0:
break;
}
}
}
void sortByLastName()
{
// 使用冒泡排序按照姓氏对客户信息进行排序
for (int i = 0; i < numCustomers - 1; i++)
{
for (int j = 0; j < numCustomers - i - 1; j++)
{
if (strcmp(customers[j].lastName, customers[j + 1].lastName) > 0)
{
// 交换客户信息
Customer temp = customers[j];
customers[j] = customers[j + 1];
customers[j + 1] = temp;
}
}
}
// 打印排序后的客户信息
printf("按照姓氏排序的客户信息:\n");
for (int i = 0; i < numCustomers; i++)
{
printf("账号:%d\n", customers[i].id);
printf("姓名:%s %s\n", customers[i].name, customers[i].lastName);
printf("性别:%c\n", customers[i].gender);
printf("年龄:%d\n", customers[i].age);
printf("电话号码:%s\n", customers[i].phone);
printf("余额:%f\n", customers[i].balance);
printf("\n");
}
}
void sortByDate()
{
// 使用冒泡排序按照操作日期对客户信息进行排序
for (int i = 0; i < numCustomers; i++)
{
for (int j = 0; j < customers[i].numTransactions - 1; j++)
{
for (int k = 0; k < customers[i].numTransactions - j - 1; k++)
{
if (customers[i].transactions[k].time > customers[i].transactions[k + 1].time)
{
// 交换交易信息
struct Transaction temp = customers[i].transactions[k];
customers[i].transactions[k] = customers[i].transactions[k + 1];
customers[i].transactions[k + 1] = temp;
}
}
}
}
// 打印排序后的客户信息
printf("按照操作日期排序的客户信息:\n");
for (int i = 0; i < numCustomers; i++)
{
printf("账号:%d\n", customers[i].id);
printf("姓名:%s %s\n", customers[i].name, customers[i].lastName);
printf("性别:%c\n", customers[i].gender);
printf("年龄:%d\n", customers[i].age);
printf("电话号码:%s\n", customers[i].phone);
printf("余额:%f\n", customers[i].balance);
}
}
void cxye()
{
int id,who;
printf("请输入您要查询余额的账户:");
scanf("%d", &id);
while (getchar() != '\n');
who = search(id);
if (who < 0)
{
printf("该账户不存在,操作失败。");
}
else
{
printf("您账户余额为%.2f \n", customers[who].balance);
}
return;
}
void ck()
{
int id, who;
double money;
printf("输入要存款的账号:");
scanf("%d", &id);
while (getchar() != '\n');
who = search(id);
if (who < 0)
{
printf("该账户不存在,操作失败。");
}
else
{
printf("用户姓名:%s\n", customers[who].name);
printf("请输入存款额:");
scanf("%lf", &money);
while (getchar() != '\n');
customers[who].balance += money;
customers[who].transactions[customers[who].numTransactions].type = 'D';
customers[who].transactions[customers[who].numTransactions].amount = money;
customers[who].numTransactions++;
printf("存款后,您有%.2f元\n", customers[who].balance);
time_t now;
time(&now);
customers[who].transactions[customers[who].numTransactions].time = now;
printf("存款时间: %s", ctime(&now)); // 打印存款时间
}
return;
}
void qk()
{
int id;
int Pass;
double money;
if (loggedInCustomerIndex == -1) {
printf("请先登录账号。\n");
return;
}
printf("请输入取款金额:");
scanf("%lf", &money);
if (money > 0 && money <= customers[loggedInCustomerIndex].balance)
{
customers[loggedInCustomerIndex].balance -= money;
customers[loggedInCustomerIndex].transactions[customers[loggedInCustomerIndex].numTransactions].type = 'W';
customers[loggedInCustomerIndex].transactions[customers[loggedInCustomerIndex].numTransactions].amount = money;
customers[loggedInCustomerIndex].numTransactions++;
printf("取款成功,当前余额为 %.2f\n", customers[loggedInCustomerIndex].balance);
time_t now;
time(&now);
customers[loggedInCustomerIndex].transactions[customers[loggedInCustomerIndex].numTransactions].time = now;
printf("取款时间: %s", ctime(&now)); // 打印取款时间
}
else
{
printf("亲,咱不玩透支啊!!!\n");
}
return;
}
void czjl()
{
int i;
int found = 0;
char IDD[20];
printf("请输入要查询的账号或姓名:");
scanf("%s", IDD);
time_t now = time(NULL); // 将获取当前时间移到循环外部
for (i = 0; i < numCustomers; i++)
{
if (strcmp(customers[i].name, IDD) == 0 || customers[i].id == atoi(IDD))
{
found = 1;
printf("客户姓名:%s\n", customers[i].name);
printf("客户账号:%d\n", customers[i].id);
printf("存取款记录:\n");
for (int j = 0; j < customers[i].numTransactions; j++)
{
if (customers[i].transactions[j].type == 'D')
{
printf("存款: %.2f\n", customers[i].transactions[j].amount);
}
else if (customers[i].transactions[j].type == 'W')
{
printf("取款: %.2f\n", customers[i].transactions[j].amount);
}
}
break;
}
}
if (!found)
{
printf("未找到匹配的账户.\n");
}
}
void addcustomer()
{
if (numCustomers >= MAX_CUSTOMERS)
{
printf("客户数已达上限。\n");
return;
}
else
printf("输入客户信息:\n");
printf("账号:"); scanf("%d", &customers[numCustomers].id);
printf("姓名:"); scanf("%s", customers[numCustomers].name);
printf("性别(M/F):"); scanf(" %c", &customers[numCustomers].gender);
printf("年龄:"); scanf("%d", &customers[numCustomers].age);
printf("电话号码:"); scanf("%s", customers[numCustomers].phone);
printf("密码:"); scanf("%d", &customers[numCustomers].password);
customers[numCustomers].balance = 0.0; // 初始化余额为0
numCustomers++;
printf("客户添加成功。\n");
}
int search(int id)
{
for (int i = 0; i < numCustomers; i++)
{
if (customers[i].id == id)
{
return i; // 返回找到的客户在数组中的索引
}
}
return -1; // 如果未找到,返回-1表示未找到
}
void saveCustomerInfo() {
FILE* file = fopen("customer_info.txt", "w");
if (file == NULL)
{
printf("Error opening file for writing.\n");
return;
}
for (int i = 0; i < numCustomers; i++)
{
fprintf(file, "%d %s %c %d %s %d %f %d\n",
customers[i].id, customers[i].name, customers[i].gender,
customers[i].age, customers[i].phone, customers[i].password,
customers[i].balance, customers[i].numTransactions);
for (int j = 0; j < customers[i].numTransactions; j++)
{
fprintf(file, "%c %f %ld\n", customers[i].transactions[j].type, customers[i].transactions[j].amount, (long)customers[i].transactions[j].time);
}
fprintf(file, "\n"); // Add a newline after each customer's information
}
fclose(file);
}
void loadCustomerInfo()
{
FILE* file = fopen("customer_info.txt", "r");
if (file == NULL) {
printf("File not found.\n");
return;
}
while (fscanf(file, "%d %s %c %d %s %d %f %d",
&customers[numCustomers].id, customers[numCustomers].name,
&customers[numCustomers].gender, &customers[numCustomers].age,
customers[numCustomers].phone, &customers[numCustomers].password,
&customers[numCustomers].balance, &customers[numCustomers].numTransactions) == 8)
{
for (int i = 0; i < customers[numCustomers].numTransactions; i++)
{
fscanf(file, "%c %f %ld", &customers[numCustomers].transactions[i].type, &customers[numCustomers].transactions[i].amount, &customers[numCustomers].transactions[i].time);
}
numCustomers++;
}
fclose(file);
}
int main()
{
system("color F0");
loadCustomerInfo(); // 加载客户信息,如果有需要的话
welcome(); // 调用欢迎界面函数,启动程序
saveCustomerInfo(); // 在程序结束前保存客户信息,如果有需要的话
return 0;
}
下面展示测试结果
管理员账号fyh 密码123