(9)改密:用新密码替代旧密码。新密码要求输入两次,一致后才确认改密成功。
#include
#include
#include
int i=0;
int n=-1;
int k;
int id1;
FILE *p;
struct score{
int num;
char name[20];
int secret;
int money;
int state;
}rec[20];
void open()//开户
{
n=n+1;
printf(" please input your name: \n");
scanf("%s",rec[n].name);
rec[n].num=n;
printf("please input your secret: \n");
scanf("%d",&rec[n].secret);
printf("how much do you want to saving?\n");
scanf("%d",&rec[n].money);
rec[n].state=0;
printf("Open account successful,your account is %d!",n);
system("pause");
}
void savings()//存款
{
int id;
int dl;
printf("input num \n");
scanf("%d",&id);
printf("input your money\n");
scanf("%d",&dl);
if(rec[id].state==0)
{
rec[id].money+=dl;
printf("Saving money successful!\n");
//用于测试
// printf("%d %s %d %d %d\n",rec[id].num,rec[id].name,rec[id].secret,rec[id].money,rec[id].state);
}
else
{
printf("your account is report you can't saving money\n");
}
system("pause");
}
void draw()//取款
{
int id1;
int dl;
int k;
int i=0;
printf("input num \n");
scanf("%d",&id1);
//验证密码,有三次机会
do
{
printf("input your key\n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
printf("How much do you want to draw\n");
scanf("%d",&dl);
if(rec[id1].state==0)
{
//如果总钱数超过了余额会提示重新输入
if(dl>rec[id1].money)
{
do
{
printf("your money is only %d,you can't tansfer,please resume load: \n",rec[id1].money);
scanf("%d",&dl);
} while(dl>rec[id1].money);
rec[id1].money=rec[id1].money-dl;
printf("Draw money successful!\n");
}
else
{
rec[id1].money=rec[id1].money-dl;
printf("Draw money successful!\n");
}
}
else
{
printf("your account is report\n");
}
}
else{printf("your key is wrong you can't do account\n");}
system("pause");
}
void refer()//查询
{
int id;
int k;
int i=0;
printf("Input your num:\n");
scanf("%d",&id);
//验证密码,有三次机会
do
{
printf("input your key\n");
scanf("%d",&k);
i++;
}while((k!=rec[id].secret)&&(i<=2));
if(k==rec[id].secret)
{
printf("Your account information:\n");
if(rec[id].state==0)
{
printf("name:%5s\n money:%5d\n your account is normal\n",rec[id].name,rec[id].money);
}
else if(rec[id].state==0){
printf("name:%5s\n money:%5d\n your account is report\n",rec[id].name,rec[id].money);
}
else
{
printf("name:%5s\n money:%5d\n your account is closed\n",rec[id].name,rec[id].money);
}
}
else{printf("your key is wrong you can't do account\n");}
system("pause");
}
void transfer()//转账
{
int id1;
int id2;
int s;
int k;
int i=0;
printf("what is your account?\n");
scanf("%d",&id1);
printf("which account do you want to transfer?\n");
scanf("%d",&id2);
do
{
printf("Please input your key: \n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
if(rec[id1].state==1||rec[id1].state==1)
{
printf("your can't transfer\n");
}
else
{
printf("how much do you want to tansfer?\n");
scanf("%d",&s);
if(s>rec[id1].money)
{
do
{
printf("your money is only %d,you can't tansfer,please resume load\n",rec[id1].money);
scanf("%d",&s);
}while(s>rec[id1].money);
rec[id1].money=rec[id1].money-s;
rec[id2].money=rec[id2].money+s;
printf("transfer success!\n");
}
else
{
rec[id1].money=rec[id1].money-s;
rec[id2].money=rec[id2].money+s;
printf("transfer success!\n");
}
}
}
else{printf("your key is wrong you can't do account\n");}
system("pause");
}
void report()//挂失
{
int i=0;
int k;
int id1;
printf("what is your account?\n");
scanf("%d",&id1);
do
{
printf("Please input your key: \n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
if(rec[id1].state==0)
{
rec[id1].state=1;
printf("Report successful!\n");
}
else
{
printf("The account already reported,you needn't repetition report\n");
}
}
else{
printf("your key is wrong you can't do account");
}
system("pause");
}
void cancel_report()//解挂
{
int id1;
int i=0;
int k;
printf("what is your account?\n");
scanf("%d",&id1);
do
{
printf("Please input your key: \n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
if(rec[id1].state==1)
{
rec[id1].state=0;
printf("Cancel report successful!\n");
}
else
{
printf("The account is not report,you can't cancel report\n");
}
}
else{
printf("your key is wrong you can't do account");
}
system("pause");
}
void cancel_account()//销户
{
int id1;
int i=0;
int k;
printf("what is your account?\n");
scanf("%d",&id1);
do
{
printf("Please input your key: \n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
rec[id1].state=2;
printf("We will draw all the money in your account\n");
printf("Give you your money %d yuan\n",rec[id1].money);
rec[id1].money=0;
printf("Close account successful!\n");
}
else{
printf("your key is wrong you can't do account");
}
system("pause");
}
void change_security()//改密
{
int s;
int i=0;
int k;
int id1;
int k1,k2;
printf("what is your account?\n");
scanf("%d",&id1);
do
{
printf("Please input your key: \n");
scanf("%d",&k);
i++;
}while((k!=rec[id1].secret)&&(i<=2));
if(k==rec[id1].secret)
{
printf("what key do you want to change to?");
scanf("%d",&k1);
printf("Input your key one more time");
scanf("%d",&k2);
if(k1==k2)
{
rec[id1].secret=k1;
printf("Change security successful!\n");
}
else {printf("Change security FAIL,the two key isn't same!\n");}
}
else{
printf("your key is wrong you can't do account");
}
system("pause");
}
void readDate()
{
int i=0;
FILE *fp;
if((fp=fopen("date.txt","r+"))==NULL)
{
printf("Can't open the date\n");
exit(0);
}
while(fscanf(fp,"%d %s %d %d %d\n",&rec[i].num,rec[i].name,&rec[i].secret,&rec[i].money,&rec[i].state ) != EOF)
{
i++;
n++;
}
fclose(fp);
}
void writeDate()
{
int i;
FILE *fp;
if((fp=fopen("date.txt","r+"))==NULL)
{
printf("Can't open the date\n");
exit(0);
}
for(i=0;i<=n;i++)
{
fprintf(fp,"\n");
fprintf(fp,"%d %s %d %d %d\n",rec[i].num,rec[i].name,rec[i].secret,rec[i].money,rec[i].state);
}
fclose(fp);
printf("saving date successful!\n");
system("pause");
}
int main()
{
int s;
readDate();
while(1)
{
system("cls");
printf("******************************MENU*********************************************\n\n");
printf(" 0:Open a new account\n");
printf(" 1:Savings money\n");
printf(" 2:Draw money \n");
printf(" 3:Refer account\n");
printf(" 4:Transfer account\n");
printf(" 5:Report account\n");
printf(" 6:Cancel report\n");
printf(" 7:cancel_account\n");
printf(" 8:change_security\n");
printf(" 9:Save date\n");
printf(" 10:Quit\n");
printf("*******************************************************************************\n\n");
do{
printf("\n Input your choice(0 ~ 10):");
scanf("%d",&s);
}while(s<0||s>10);
system("cls");
switch(s)
{
case 0:open();break;
case 1:savings();break;
case 2:draw();break;
case 3:refer();break;
case 4:transfer();break;
case 5:report();break;
case 6:cancel_report();break;
case 7:cancel_account();break;
case 8:change_security();break;
case 9:writeDate();break;
case 10:exit(0);
}
}
return 0;
}
此银行管理系统设计的比较简单,一些功能如隐藏密码等功能也没有实现,还有对于密码的输入没有对其进行长度的限制当超出长度是程序就出错了,大家可以试着完善一些,我日后也会做出改进的。
在“data.txt"中存储了银行的数据,程序运行时就会读到内存中进行处理,处理完成之后,就会再写入文件中。程序运行之前,文件中的数据如下:
0 king 1 1 0
1 ding 1 1 0
2 vkj 1 1 0
3 k 1 1 0
4 j 1 2 0
第一列到第五列分别对应这用户的:卡号、姓名、密码、余额、状态。 测试的时候可以用这个文件来测试。