|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- <P> </P>
- <P> </P>
- <P> </P>
- <P> </P>
- <P>#include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- char cfile[] = "date.txt";//创建一个文件
- struct bank
- {
- char id[10+1];
- char psw[6+1];
- float money; //定义账号,密码,金额
- };
- menu1()
- {
- printf("*************欢迎使用虚拟银行服务!*****************\n");
- printf("***************************************************\n");
- printf(" || 请输入序号 ||\n");
- printf(" || 1.新用户开户。 ||\n");
- printf(" || 2.老用户登陆。 ||\n");
- printf(" || 3.退出系统。 ||\n");
- printf("***************************************************\n");
- printf("请选择功能:\n");
- }//一级菜单,实现新用户注册和老用户
- menu2()
- {
- printf("************** 欢迎进入虚拟银行系统 ***************\n");
- printf("***************************************************\n");
- printf("**************1.取款; ***************\n");
- printf("**************2.存款; ***************\n");
- printf("**************3.查询余额; ***************\n");
- printf("**************4.返回主页面; ***************\n");
- printf("**************任意键退出. ***************\n");
- printf("***************************************************\n");
- }//二级菜单实现老用户登陆后的各种功能
- int search(char* user, char* pwd, char* real_pwd)//将注册的账号与已注册的账号对比是否重复
- {
- FILE* file;
- char user_no[256], pwd_no[256];
- file = fopen(cfile, "r");//以只读方式打开文本文件。
- if (!file)
- return 0;
- while (!feof(file))//feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为非零值,否则为0。
- {
- if (fscanf(file, "%s%s\n", user_no, pwd_no) == EOF)
- return 0;
- if (!strcmp(user_no, user))//字符串的比较,(前1,后2)若str1==str2,则返回零;若str1>str2,则返回正数;若str1<str2,则返回负数。
- {
- if (real_pwd) strcpy(real_pwd, pwd_no);//将字符串2复制到字符串1中,应确保字符数组1足够大以便于存储字符串2。
- return 1;
- }
- }
- fclose(file);//把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区。
- return 0;
- }
- int add(char* user, char* pwd)//如果新注册的账号不重复,则将其添加至文件data.txt中
- {
- FILE* file;
- file = fopen(cfile, "a");//以只写的方式打开文本文件,位置指针指向文件末尾,原文件数据保留。
- if (!file)
- return 0;
- fprintf(file, "%s %s \n", user, pwd );
- fclose(file);
- return 0;
- }
- int wenjian(char* user, char* pwd )
- {
- if (search(user, pwd, NULL))
- return 0;
- else add(user, pwd );
- return 1;
- }
- zhuce()//新用户注册
- {
- struct bank *p;
- int i=0;
- char name[30];
- float money;
- char sh[18],n;
- p=(struct bank*)malloc(sizeof(struct bank));//p创造动态存储空间
- printf("请输入您的名字:");
- gets(name);
- fflush(stdin); //清空输入缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符,此时应该先执行fflush(stdin);)
- printf("请输入您的身份证号码:");
- gets(sh);
- fflush(stdin);
- printf("请输入您的帐号10位:\n");
- fflush(stdin);
- scanf("%s",p->id);
- fflush(stdin);
- printf("请输入您的密码(6位数字):\n");
- fflush(stdin);
- scanf("%s",p->psw);
- fflush(stdin);
- printf("请输入您的存款:\n");
- fflush(stdin);
- scanf("%f",&money);
- fflush(stdin);
- if (wenjian(p->id,p->psw))
- {
- system("cls");
- printf("注册成功!");
- printf("您的注册信息如下:\n");
- printf("名字:");
- puts(name);
- printf("身份证号码:");
- puts(sh);
- printf("帐号:");
- puts(p->id);
- printf("密码:");
- puts(p->psw);
- printf("存款");
- printf("%f",money);
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);//当输入1时返回上一级菜单,否则程序终止
- }
- else
- {
- system("cls");
- printf("您输入的帐号已经存在注册失败!按1返回,按任意键退出\n");
- scanf("%c",&n);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- }
- gongneng(char Account[10],char Password[6],float Money)//实现老用户登陆后的各种功能
- {
- FILE *ps;
- struct bank xin[100];
- int k=0,j=0,n;
- char Chiose;
- float inmoney,outmoney;
- menu2();
- fflush(stdin);
- scanf("%c",&Chiose);
- fflush(stdin);
- ps=fopen(cfile,"r");
- if (!ps)
- {
- exit(0);
- }
- if(Chiose=='1')
- {
- printf("输入您的取款金额:");
- fflush(stdin);
- scanf("%lf",&outmoney);
- fflush(stdin);
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"wb");
- if (!ps)
- {
- exit(0);
- }
- for (j=0;;j++)
- {if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- { xin[j].money=xin[j].money-outmoney;
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;}
- }//实现菜单2中的取款功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- if(Chiose=='2')
- {
- printf("输入您的存款款金额:");
- fflush(stdin);
- scanf("%lf",&inmoney);
- fflush(stdin);
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"wb");
- if (!ps)
- {
- exit(0);
- }
- for (j=0;;j++)
- {
- { if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- xin[j].money=xin[j].money+inmoney;
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;
- } }//实现菜单2中的存款功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- if(Chiose=='3')
- {
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"r");
- if (!ps)
- {
- exit(0);
- }
- for(j=0;;j++)
- {
- {if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;
- } }
- }//实现菜单2中的查询余额的功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- if(Chiose=='4')
- return 0;
- }
- denglu()
- {
- FILE *fp;
- char account[10],password[6],h;
- int m=0;
- char real_account[10];
- char real_password[6];
- float real_money=0.0;
- fp = fopen(cfile, "r");
- if (!fp)
- {
- exit(0);
- }
- while (m<=2)
- {
- printf("请输入您的帐号(10位):");
- gets(account);
- fflush(stdin);
- printf("请输入您的密码(6位):");
- gets(password);
- fflush(stdin);
- while(fscanf(fp, "%s %s %lf", &real_account, &real_password,&real_money) != EOF)
- {
- if ((strcmp(real_account, account)==0)&&(strcmp(real_password, password)==0))
- {
- system("cls");
- printf("登陆成功!");
- gongneng(real_account,real_password,real_money);
- fclose(fp);
- return 0;
- }//输入曾经注册过的账号进行登陆,账号或密码输入错误则有三次重新输入的机会,否则退出程序
- else
- rewind (fp);
- printf("您输入的帐号不不正确!请重新输入:\n");
- m++;
- printf("请输入您的帐号(10位):");
- gets(account);
- fflush(stdin);
- printf("请输入您的密码(6位):");
- gets(password);
- fflush(stdin);
- } }
- fclose(fp);
- printf("您输入帐号密码不正确已经3次,被强制退出(按任意键退出)!");
- fflush(stdin);
- scanf("%c",&h);
- fflush(stdin);
- exit(0);
- }
- int main()
- {
- char chiose;
- char flag;
- while (flag!='N')
- {menu1();
- scanf("%c",&chiose);
- getchar();
- if(chiose <='0' ||chiose>='4')
- {
- while(chiose <='0' || chiose>='4')
- {
- printf("您的输入有误,请重新输入:");
- scanf("%c",&chiose);
- getchar(); //在一级菜单中所输入的账号必须在1~5之间,否则无法进行下一步操作
- }
- }
- if(chiose=='1')
- {
- system("cls");
- zhuce(); printf ("继续请按任意键,退出请按N"); scanf ("%c",&flag);
- }
- if(chiose=='2')
- {
- system("cls");
- denglu(); printf ("继续请按任意键,退出请按N"); scanf ("%c",&flag);
- }
- if(chiose=='3')
- {
- printf("谢谢您的使用!");
- exit(0);
- }
- }
- }//一级菜单中按3直接退出程序
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- char cfile[] = "date.txt";//创建一个文件
- struct bank
- {
- char id[10+1];
- char psw[6+1];
- float money; //定义账号,密码,金额
- };
- menu1()
- {
- printf("*************欢迎使用虚拟银行服务!*****************\n");
- printf("***************************************************\n");
- printf(" || 请输入序号 ||\n");
- printf(" || 1.新用户开户。 ||\n");
- printf(" || 2.老用户登陆。 ||\n");
- printf(" || 3.退出系统。 ||\n");
- printf("***************************************************\n");
- printf("请选择功能:\n");
- }//一级菜单,实现新用户注册和老用户
- menu2()
- {
- printf("************** 欢迎进入虚拟银行系统 ***************\n");
- printf("***************************************************\n");
- printf("**************1.取款; ***************\n");
- printf("**************2.存款; ***************\n");
- printf("**************3.查询余额; ***************\n");
- printf("**************4.返回主页面; ***************\n");
- printf("**************任意键退出. ***************\n");
- printf("***************************************************\n");
- }//二级菜单实现老用户登陆后的各种功能
- int search(char* user, char* pwd, char* real_pwd)//将注册的账号与已注册的账号对比是否重复
- {
- FILE* file;
- char user_no[256], pwd_no[256];
- file = fopen(cfile, "r");//以只读方式打开文本文件。
- if (!file)
- return 0;
- while (!feof(file))//feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为非零值,否则为0。
- {
- if (fscanf(file, "%s%s\n", user_no, pwd_no) == EOF)
- return 0;
- if (!strcmp(user_no, user))//字符串的比较,(前1,后2)若str1==str2,则返回零;若str1>str2,则返回正数;若str1<str2,则返回负数。
- {
- if (real_pwd) strcpy(real_pwd, pwd_no);//将字符串2复制到字符串1中,应确保字符数组1足够大以便于存储字符串2。
- return 1;
- }
- }
- fclose(file);//把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区。
- return 0;
- }
- int add(char* user, char* pwd)//如果新注册的账号不重复,则将其添加至文件data.txt中
- {
- FILE* file;
- file = fopen(cfile, "a");//以只写的方式打开文本文件,位置指针指向文件末尾,原文件数据保留。
- if (!file)
- return 0;
- fprintf(file, "%s %s \n", user, pwd );
- fclose(file);
- return 0;
- }
- int wenjian(char* user, char* pwd )
- {
- if (search(user, pwd, NULL))
- return 0;
- else add(user, pwd );
- return 1;
- }
- zhuce()//新用户注册
- {
- struct bank *p;
- int i=0;
- char name[30];
- float money;
- char sh[18],n;
- p=(struct bank*)malloc(sizeof(struct bank));//p创造动态存储空间
- printf("请输入您的名字:");
- gets(name);
- fflush(stdin); //清空输入缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符,此时应该先执行fflush(stdin);)
- printf("请输入您的身份证号码:");
- gets(sh);
- fflush(stdin);
- printf("请输入您的帐号10位:\n");
- fflush(stdin);
- scanf("%s",p->id);
- fflush(stdin);
- printf("请输入您的密码(6位数字):\n");
- fflush(stdin);
- scanf("%s",p->psw);
- fflush(stdin);
- printf("请输入您的存款:\n");
- fflush(stdin);
- scanf("%f",&money);
- fflush(stdin);
- if (wenjian(p->id,p->psw))
- {
- system("cls");
- printf("注册成功!");
- printf("您的注册信息如下:\n");
- printf("名字:");
- puts(name);
- printf("身份证号码:");
- puts(sh);
- printf("帐号:");
- puts(p->id);
- printf("密码:");
- puts(p->psw);
- printf("存款");
- printf("%f",money);
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);//当输入1时返回上一级菜单,否则程序终止
- }
- else
- {
- system("cls");
- printf("您输入的帐号已经存在注册失败!按1返回,按任意键退出\n");
- scanf("%c",&n);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- }
- gongneng(char Account[10],char Password[6],float Money)//实现老用户登陆后的各种功能
- {
- FILE *ps;
- struct bank xin[100];
- int k=0,j=0,n;
- char Chiose;
- float inmoney,outmoney;
- menu2();
- fflush(stdin);
- scanf("%c",&Chiose);
- fflush(stdin);
- ps=fopen(cfile,"r");
- if (!ps)
- {
- exit(0);
- }
- if(Chiose=='1')
- {
- printf("输入您的取款金额:");
- fflush(stdin);
- scanf("%lf",&outmoney);
- fflush(stdin);
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"wb");
- if (!ps)
- {
- exit(0);
- }
- for (j=0;;j++)
- {if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- { xin[j].money=xin[j].money-outmoney;
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;}
- }//实现菜单2中的取款功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- if(Chiose=='2')
- {
- printf("输入您的存款款金额:");
- fflush(stdin);
- scanf("%lf",&inmoney);
- fflush(stdin);
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"wb");
- if (!ps)
- {
- exit(0);
- }
- for (j=0;;j++)
- {
- { if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- xin[j].money=xin[j].money+inmoney;
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;
- } }//实现菜单2中的存款功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- }
- if(Chiose=='3')
- {
- while(!feof(ps))
- {
- fscanf(ps, "%s %s %lf",&xin[k].id,&xin[k].psw,&xin[k].money);
- k++;
- }
- fclose(ps);
- ps=fopen(cfile,"r");
- if (!ps)
- {
- exit(0);
- }
- for(j=0;;j++)
- {
- {if ((strcmp(Account, xin[j].id)==0)&&(strcmp(Password, xin[j].psw)==0))
- printf("%.2lf\n",xin[j].money);
- fprintf(ps, "%s %s %.2lf\n", xin[j].id, xin[j].psw, xin[j].money);
- j++; break;
- } }
- }//实现菜单2中的查询余额的功能
- printf("按1返回主页面. 按任意键退出:");
- fflush(stdin);
- scanf("%c",&n);
- fflush(stdin);
- if(n=='1')
- {
- system("cls");
- return 0;
- }
- else
- exit(0);
- if(Chiose=='4')
- return 0;
- }
- denglu()
- {
- FILE *fp;
- char account[10],password[6],h;
- int m=0;
- char real_account[10];
- char real_password[6];
- float real_money=0.0;
- fp = fopen(cfile, "r");
- if (!fp)
- {
- exit(0);
- }
- while (m<=2)
- {
- printf("请输入您的帐号(10位):");
- gets(account);
- fflush(stdin);
- printf("请输入您的密码(6位):");
- gets(password);
- fflush(stdin);
- while(fscanf(fp, "%s %s %lf", &real_account, &real_password,&real_money) != EOF)
- {
- if ((strcmp(real_account, account)==0)&&(strcmp(real_password, password)==0))
- {
- system("cls");
- printf("登陆成功!");
- gongneng(real_account,real_password,real_money);
- fclose(fp);
- return 0;
- }//输入曾经注册过的账号进行登陆,账号或密码输入错误则有三次重新输入的机会,否则退出程序
- else
- rewind (fp);
- printf("您输入的帐号不不正确!请重新输入:\n");
- m++;
- printf("请输入您的帐号(10位):");
- gets(account);
- fflush(stdin);
- printf("请输入您的密码(6位):");
- gets(password);
- fflush(stdin);
- } }
- fclose(fp);
- printf("您输入帐号密码不正确已经3次,被强制退出(按任意键退出)!");
- fflush(stdin);
- scanf("%c",&h);
- fflush(stdin);
- exit(0);
- }
- int main()
- {
- char chiose;
- char flag;
- while (flag!='N')
- {menu1();
- scanf("%c",&chiose);
- getchar();
- if(chiose <='0' ||chiose>='4')
- {
- while(chiose <='0' || chiose>='4')
- {
- printf("您的输入有误,请重新输入:");
- scanf("%c",&chiose);
- getchar(); //在一级菜单中所输入的账号必须在1~5之间,否则无法进行下一步操作
- }
- }
- if(chiose=='1')
- {
- system("cls");
- zhuce(); printf ("继续请按任意键,退出请按N"); scanf ("%c",&flag);
- }
- if(chiose=='2')
- {
- system("cls");
- denglu(); printf ("继续请按任意键,退出请按N"); scanf ("%c",&flag);
- }
- if(chiose=='3')
- {
- printf("谢谢您的使用!");
- exit(0);
- }
- }
- }//一级菜单中按3直接退出程序
- </P>
复制代码
|
|