#include<stdio.h>
#include<process.h>
#include<windows.h>
struct user
{
int id;
char name[50];
int key;
float m;
} a[10];
typedef struct user USER;
FILE *fp;
int d=0,i=0,c=0;
int z;
USER wenjian(char h[])//将字符串以分号形式输入
{
USER j;
strtok(h,";");
sscanf(h,"%d",&j.id);
h=strtok(NULL,";");
sscanf(h,"%s",j.name);
h=strtok(NULL,";");
sscanf(h,"%d",&j.key);
h=strtok(NULL,";");
sscanf(h,"%f",&j.m);
return j;
}
void start()//开始加载信息
{
char ch1[20];
fp=fopen("account_.txt","r+");
if(fp==NULL)
{
printf("无法打开");
exit(0);
}
for(i=0;i<=9;i++)
{
fgets(ch1,30,fp);
a[i]=wenjian(ch1);
}
}
int loginview()//登录界面
{
int b,g=0;
int t=0;
while(t==0)
{
printf("请输入账号:");
scanf("%d",&c);
printf("请输入账户密码:");
scanf("%d",&b);
for(i=0;i<=9;i++)
{
if(c==a[i].id)
{
g=1;
break;
}
}
z=i;
if(g==1)
{
if(a[z].key==b)
{
t=1;
system("cls");
printf("用户登录成功\n");
}
else
{
d++;
printf("密码错误重新开始");
Sleep(1000);
system("cls");
}
}
else
printf("账号错误重新开始");
Sleep(1000);
system("cls");
}
}
int show()//功能界面
{
int s;
printf("|欢迎:%s | \n",a[z].name);Sleep(50);
printf("| 【1】修改密码 | \n");Sleep(50);
printf("| 【2】余额查询 | \n");Sleep(50);
printf("| 【3】取款 | \n");Sleep(50);
printf("| 【4】转账 | \n");Sleep(50);
printf("| 【5】退卡 | \n");Sleep(50);
printf(" 请选择要执行的操作:");Sleep(50);
scanf("%d",&s);
return s;
}
void change()//修改密码
{
int m=0,n,l;
while(1)
{
printf("请输入你的密码:");
scanf("%d",&m);
printf("请输入你的新密码:");
scanf("%d",&n);
printf("请再次确认你的新密码");
scanf("%d",&l);
if(m==a[z].key )
{
if(l==n)
break;
else
printf("两次密码不一致\n");
}
if(m!=a[z].key)
printf("原密码不正确\n");
}
a[z].key=n;
rewind(fp);
for(i=0;i<=9;i++)
fprintf(fp,"%d;%s;%d;%.2f\n",a[i].id,a[i].name,a[i].key,a[i].m);
printf("密码修改成功\n");
}
void qk()//取款
{
float q;
printf("请输入你的取款金额");
scanf("%f",&q);
if(q<0||q>=2000||q>a[z].m)
printf("账号余额不够或者单次取款超过2000\n");
else
{
a[z].m=a[z].m-q;
printf("取款成功\n");
printf("用户剩余%.2f\n",a[z].m);
}
}
void zz()// 转账
{
int q;
float m;
printf("请输入目标账户:");
scanf("%d",&q);
printf("请输入转账金额:");
scanf("%f",&m);
for(i=0;i<=9;i++)
{
if(q==a[i].id)
{
printf("目标存在\n");
if(m<a[z].m)
{
a[z].m=a[z].m-m;
a[i].m=a[i].m+m;
printf("用户转账成功");
printf("用户剩余:%.2f\n",a[z].m);
break;
}
else
{
printf("账户余额不够\n");
break;
}
}
}
if(i==10)
printf("用户不存在\n");
}
void ye()//余额查询
{
printf("余额为:%.2f\n",a[z].m);
}
void tuika()
{
rewind(fp);
for(i=0;i<=9;i++)
fprintf(fp,"%d;%s;%d;%.2f\n",a[i].id,a[i].name,a[i].key,a[i].m);
}
void main()
{
int s,v;
start();
loginview();
while(d!=3&&s!=5)
{
s=show();
if(s==5)
break;
switch(s)
{
case 1: change(); break;
case 2: ye(); break;
case 3: qk(); break;
case 4: zz(); break;
}
printf("输入1返回");
scanf("%d",&v);
system("cls");
}
tuika();
printf("退卡成功");
}