- 想起以前学C时写了个简易的银行管理系统,几乎不涉及任何数据结构,是对C初学者来说是比较容易入手的小项目。
- 一、程序功能
能够实现用户对自己银行账户的基本操作与管理,其功能包括:
(1) 存款;
(2) 取款;
(3) 查询;
(4) 转账;
(5) 挂失;
(6) 解挂;
(7) 销户;
(8) 改密。
二、总体设计
程序有主函数main()与若干功能函数体构成。
在主函数main()中,主要分三部分实现:读入——核心操作函数——写入文件
(1) 先调用读入函数refile(),将文件中的数据读入到user[]数组中来进行处理。
(2) 再通过关键的核心函数MainMenu(),来对账户进行相关管理操作。
(3) 最后调用写入函数writefile(),将修改后的数据写入文件进行保存。
- 三、详细设计
(1)准备工作:需先在C文件下创建文本account.txt。
(2)定义用户结构体struct person{},其中用户账号数目可在宏定义#define Account_num 1000中按需修改。程序规定第一个账号名为20190001,可在#define First_account_name 20190001按需修改。
(3)进入主函数,读入用户信息readfile();
(4)调用核心操作函数MainMenu(),根据用户需求,选择登陆(m=1)或者注册账号(m=2)。
若用户输入选择m为非1、2,则采用goto loop重新选择。
登陆:
①用户需先输入账号和密码,其中密码为六位数字,用户在输入六位密码错误时,有三次机会。若用户误输入非数字,则有五次机会。
②用户正确登陆后,可根据界面菜单选择提示进行存款、取款、查询、转账、挂失、解挂、销户、改密操作。此处采用switch选择结构语句,各个功能的实现采用函数调用、循环结构、选择分支结构。
注册:
①用户账号名由程序提供,其中密码为六位数字,用户在输入六位密码错误时,有三次机会。若用户误输入非数字,则有五次机会。其中注册密码需要输入两遍。
②正确输入密码后即成功注册账号。
(5) 将信息写入文件writefile()。
(6) 程序执行结束。
(7)具体代码实现可见代码注释。
- 四、执行结果分析
(1)总体上,程序基本可以实现基础功能。
(2)在某些细节存在缺陷:
用户在销户时,采用的时将用户结构体中的账号状态置为某一个标记,而不是将文件中对应的用户信息进行彻底删除。因此销毁的用户账号仍存在与文件中。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#define Account_num 1000
#define First_account_name 20190001
long long int N = 0;
int n = 0;
struct person
{
long long int num;
char name[20];
int password;
double money;
int flag;
} user[Account_num];
void readfile();
void writefile();
void savemoney();
void withdraw();
void searchmoney();
void transferaccount();
void reportloss();
void cancelloss();
void destroyaccount();
void changepassword();
int search(int num);
void MainMenu();
int inputpassword_1();
int inputpassword_2();
double inputmoney();
int main()
{
int i = 0;
system("title 银行储蓄管理系统");
system("color F1");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t\t欢迎进入 银行储蓄管理系统!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("正在登录,请稍后...\n");
printf("===============================================================================\r");
for (i = 0; i<80; i++)
{
Sleep(30);
printf(">");
}
system("cls");
readfile();
MainMenu();
writefile();
return 0;
}
void MainMenu()
{
int m = 0;
printf("\t\t *******************************\n");
printf("\t\t * 1.登录 *\n");
printf("\t\t * 2.注册 *\n");
printf("\t\t *******************************\n");
loop: printf("请输入操作:");
scanf("%d", &m);
if (m == 1)
{
int num = 0, pass = 0, t = 3, f = 0;
do
{
printf("请输入您的账号:");
scanf("%d", &num);
n = search(num);
if (n<0||(user[n].flag==2))
{
printf("该账号不存在!\n");
}
else
{
do
{
printf("请输入密码:");
pass = inputpassword_1();
if (pass != user[n].password)
{
t--;
if (t == 0)
{
printf("您已输错了三次密码!");
break;
}
printf("密码输入错误,您还有%d次机会", t);
}
else
{
f = 1;
break;
}
} while (t);
break;
}
} while (1);
if (f == 1)
{
int choice = 0;
do
{
printf("\t\t *******************\n");
printf("\t\t * 1.存款 2.取款 * \n\n");
printf("\t\t * 3.查询 4.转账 * \n\n");
printf("\t\t * 5.挂失 6.解挂 * \n\n");
printf("\t\t * 7.销户 8.改密 * \n\n");
printf("\t\t * 9.退出 *\n\n");
printf("\t\t *******************\n");
printf("请输入要办理的业务:");
scanf("%d", &choice);
if (choice>0 && choice <= 9)
{
switch (choice)
{
case 1:
readfile();
savemoney();
writefile();
break;
case 2:
readfile();
withdraw();
writefile();
break;
case 3:
readfile();
searchmoney();
writefile();
break;
case 4:
readfile();
transferaccount();
writefile();
break;
case 5:
readfile();
reportloss();
writefile();
break;
case 6:
readfile();
cancelloss();
writefile();
break;
case 7:
readfile();
destroyaccount();
writefile();
break;
case 8:
readfile();
changepassword();
writefile();
break;
case 9:
printf("欢迎您再来!\n");
}
}
if (choice == 7)
{
break;
}
} while (choice != 9);
}
}
int f = 0;
if (m == 2)
{
int num = 0, i = 0, choice = 0;
int pass1 = 0, pass2 = 0;
char name[20] = "";
double money = 0;
user[N].num = N + First_account_name;
printf("您的开户账号为:%d\n", user[N].num);
printf("请记住您的账号\n请输入您的姓名:");
scanf("%s", name);
printf("请输入6位数字开户密码:");
pass1 = inputpassword_1();
printf("请再次输入密码:");
pass2 = inputpassword_2();
if (pass1 == pass2)
{
strcpy(user[N].name, name);
user[N].password = pass1;
user[N].money = 0;
user[N].flag = 0;
N++;
printf("注册成功!\n");
f = 1;
}
else
{
printf("\n两次输入密码不同,开户失败!\n");
}
}
if (m<1 || m>2)
{
printf("输入有误!\n");
goto loop;
}
}
void readfile()
{
FILE *fp = NULL;
int i = 0;
if ((fp = fopen("account.txt", "r")) == NULL)
{
printf("Can't open!");
exit(0);
}
while (fscanf(fp, "%d %s %d %lf %d", &user[i].num, user[i].name, &user[i].password, &user[i].money, &user[i].flag) != EOF)
{
i++;
}
N = i;
fclose(fp);
}
void writefile()
{
FILE *fp = NULL;
int i = 0;
if ((fp = fopen("account.txt", "w")) == NULL)
{
printf("Can't open!");
exit(0);
}
for (i = 0; i<N; i++)
fprintf(fp, "%d %s %d %f %d\n", user[i].num, user[i].name, user[i].password, user[i].money, user[i].flag);
fclose(fp);
}
int inputpassword_1()
{
char a[10] = "";
int i = 0, count = 6, pass = 0;
while (1)
{
for (i = 0; i<6; i++)
{
a[i] = getch();
putchar('*');
if (a[i] >= '0'&&a[i] <= '9')
pass = pass * 10 + a[i] - '0';
else if (a[i] == '\b')
{
printf("\b \b");
i--;
}
else
{
pass = 0;
printf("\n密码要求全为数字,且不能全为0!\n");
break;
}
}
printf("\n");
if (pass == 0)
{
count--;
if (count == 0)
{
printf("错误次数过多,请稍后登陆系统!");
exit(0);
}
printf("你还有%d次机会,请重新输入密码: ", count);
}
else
break;
}
return pass;
}
int inputpassword_2()
{
char a[10] = "";
int i = 0, count = 3, pass = 0;
while (1)
{
for (i = 0; i<6; i++)
{
a[i] = getch();
putchar('*');
if (a[i] >= '0'&&a[i] <= '9')
pass = pass * 10 + a[i] - '0';
else if (a[i] == '\b')
{
printf("\b \b");
i--;
}
else
{
pass = 0;
break;
}
}
printf("\n");
if (pass == 0)
{
count--;
if (count == 0)
{
printf("开户失败,请稍后再登陆!\n");
exit(0);
}
printf("密码要求全为数字,且不能全为0!\n");
printf("\n您还有%d次确认机会,请再次输入密码以确定: ", count);
}
else
break;
}
return pass;
}
double inputmoney()
{
char a[100] = "";
double money = 0;
int i = 0, f = 0, t = 0;
while (1)
{
f = 1;
for (i = 0; i<100; i++)
{
scanf("%c", &a[i]);
if (a[i] >= '0'&&a[i] <= '9')
money = money * 10 + a[i] - '0';
else if (a[0] == '\n')
{
f = 0;
break;
}
else if (a[i] == '\n')
{
t = 0;
break;
}
else
{
f = 0;
break;
}
}
if (f == 0)
{
printf("金额要求全为数字!\n");
printf("请重新输入金额:");
}
else if (t == 0)
break;
}
return money;
}
void savemoney()
{
double money = 0;
if (user[n].flag == 0)
{
printf("您的姓名为:%s\n", user[n].name);
printf("请输入要存入的金额,存款下限为100,上限为100000000000:");
do
{
money = inputmoney();
if (money>100000000000)
{
printf("存款金额超过上限!\n");
printf("请重新输入金额:");
}
else if (money<100)
{
printf("存款金额低于下限\n");
printf("请重新输入金额:");
}
else
{
user[n].money += money;
printf("存款成功!\n");
break;
}
} while (1);
}
else printf("您的账户已注销!\n");
}
void searchmoney()
{
printf("您的姓名为:%s\n", user[n].name);
if (user[n].flag == 0)
printf("您的账户余额为:%.2f,并且您的账户状态正常!\n", user[n].money);
else if (user[n].flag == 1)
printf("您的账户余额为:%.2f,并且您的账户处于挂失状态!\n", user[n].money);
else if (user[n].flag == 2)
printf("您的账户已注销!\n");
}
void withdraw()
{
double money = 0;
printf("您的姓名为:%s\n", user[n].name);
if (user[n].flag == 0)
{
do
{
printf("请输入要取款的金额:");
money = inputmoney();
if (money>user[n].money)
{
printf("余额不足,无法取款!\n");
}
else
{
user[n].money -= money;
printf("取款成功!您的账户余额为:%.2f\n", user[n].money);
break;
}
} while (1);
}
else if (user[n].flag == 1)
{
printf("您的账户已挂失,无法取款!\n");
}
else if (user[n].flag == 2)
{
printf("您的账户已注销!\n");
}
}
void transferaccount()
{
int num2 = 0, n2 = 0;
double money = 0;
printf("请输入转出账户的账号:");
scanf("%d", &num2);
n2 = search(num2);
if (n2<0)
printf("账号不存在,无法转账!\n");
else
{
if (user[n].flag == 0)
{
do
{
printf("请输入要转账的金额:");
money = inputmoney();
if (money>user[n].money)
printf("账户余额不足!\n");
else
{
user[n].money -= money;
user[n2].money += money;
printf("转账成功!您的账户余额为:%.2f\n", user[n].money);
break;
}
} while (1);
}
else if (user[n].flag == 1)
{
printf("该账户已挂失,无法转账!\n");
}
else if (user[n].flag == 2)
{
printf("该账户已注销!\n");
}
}
}
void reportloss()
{
if (n<0)
printf("账号不存在,无法挂失!\n");
else
{
printf("您的姓名为:%s\n", user[n].name);
if (user[n].flag == 0)
{
user[n].flag = 1;
printf("挂失成功!\n");
}
else if (user[n].flag == 1)
{
printf("您的账户已处于挂失状态,无须挂失!\n");
}
else if (user[n].flag == 2)
{
printf("您的账户已注销!\n");
}
}
}
void cancelloss()
{
if (n<0)
printf("账号不存在,无法取消挂失!\n");
else
{
printf("您的姓名为:%s\n", user[n].name);
if (user[n].flag == 1)
{
user[n].flag = 0;
printf("取消挂失成功!\n");
}
else if (user[n].flag == 0)
{
printf("您的账户处于正常状态!\n");
}
else if (user[n].flag == 2)
{
printf("您的账户已注销!\n");
}
}
}
void changepassword()
{
int pass1 = 0, pass2 = 0, pass0 = 0;
if (n<0)
printf("账号不存在,无法修改密码!\n");
else
{
printf("您的姓名为:%s\n", user[n].name);
if (user[n].flag == 0)
{
while (1)
{
printf("请输入原来的密码:");
pass0 = inputpassword_2();
if (pass0 == user[n].password)
{
printf("请输入新密码:");
pass1 = inputpassword_1();
printf("请再次输入密码:");
pass2 = inputpassword_2();
if (pass1 == pass2)
{
user[n].password = pass1;
printf("改密成功!");
break;
}
else printf("密码错误!\n");
}
else printf("密码错误!\n");
}
}
else if (user[n].flag == 1)
{
printf("您的账户处于挂失状态,无法改密!\n");
}
else if (user[n].flag == 2)
{
printf("您的账户已注销!\n");
}
}
}
void destroyaccount()
{
int a = 0;
if (n<0)
printf("账号不存在,无法销户!\n");
else
{
if (user[n].flag == 0)
{
printf("您确定要销户?确定请输入1,取消操作请输入0:");
scanf("%d", &a);
if (a == 1)
{
user[n].flag = 2;
printf("销户成功!\n");
}
else
printf("操作已取消!\n");
}
else if (user[n].flag == 1)
{
printf("您的账户已挂失,无法注销!\n");
}
else if (user[n].flag == 2)
{
printf("您的账户已注销!\n");
}
}
}
int search(int num)
{
int n = -1, i = 0;
for (i = 0; i<N; i++)
{
if (user[i].num == num)
{
n = i;
break;
}
}
return n;
}