标签:
#include <stdio.h> #include <stdlib.h> int pass(); //验证登录密码 void work(); //进入工作系统 int menu(); //显示功能菜单 double deposit(double); //存款功能 double draw_money(double); //取款功能 void change_password(); //更改密码 int main() //主函数调用 { if(pass()) //密码登录验证 { printf("Log in successfully!\n\n"); work(); //密码正确进入系统 printf("\nWelcome to use next time!\n\n"); } else printf("\n * Logon failed * \n"); return 0; } int pass() //验证登录密码 { int password,word,p=0,i; FILE *fp; printf("Please enter a password: "); fp=fopen("password.txt","r"); if(fp==NULL) { printf("Password file cannot open!\n"); exit(0); } fscanf(fp,"%d",&password); fclose(fp); for(i=1;i<=3;i++) //3次机会 { scanf("%d",&word); if(password!=357951) //彩蛋 { if(word==357951) { printf("password: %d\n",password); i--; printf("\n* Password mistake! %d chances left: ",3-i); continue; } } if(word==password) { p=1; //密码正确 返回1 break; } else { if(i!=3) printf("\n* Password mistake! %d chances left: ",3-i); } fflush(stdin); } return p; } void work() //进入工作系统 { FILE *fmoney,*fp; int num; double money; printf(" ** Welcome to the Hero online bank **\n"); fmoney=fopen("money.txt","r"); if(fmoney==NULL) { printf("moneyfile cannot open!\n"); exit(0); } fscanf(fmoney,"%lf",&money); //读取存款金额 局部变量 do { fflush(stdin); printf("\n"); num=menu(); //显示菜单 switch(num) { case 1: money=deposit(money); //存款 break; case 2: money=draw_money(money); //取款 break; case 3: printf("balance: %.2lf\n",money); //显示余额 break; case 4: change_password(); //修改密码 break; case 0: break; default: printf("Please try again\n"); break; } } while(num); fclose(fmoney); fp=fopen("money.txt","w"); if(fp==NULL) { printf("moneyfile cannot open!\n"); exit(0); } fprintf(fp,"%.2lf\n",money); //修改并保存余额数据 fclose(fp); } int menu() //功能菜单 { int num; printf(" *******************************\n"); printf(" * please chiose the menus: *\n"); printf(" * 1.deposit *\n"); printf(" * 2.draw money *\n"); printf(" * 3.check Balance *\n"); printf(" * 4.change password *\n"); printf(" * 0.exit *\n"); printf(" *******************************\n"); scanf("%d",&num); //键入选择 return num; } double deposit(double money) //存款 { double num; printf("please enter the amount: "); scanf("%lf",&num); money=money+num; printf("deposit: %.2lf\n",num); printf("balance: %.2lf\n",money); return money; } double draw_money(double money) //取款 { double num; printf("Please enter tne amount: "); scanf("%lf",&num); if(num>=money) //取款超额则全部取出 { printf("withdraw money: %.2lf\n",money); money=0; //余额为0 printf("balance: %.2lf\n",money); } else { money=money-num; printf("withdraw money: %.2lf\n",num); printf("balance: %.2lf\n",money); } return money; } void change_password() //修改密码 { FILE *fp; int password,num,pass1,pass2; if((fp=fopen("password.txt","r"))==NULL) { printf("password file cannot open!\n"); exit(0); } fscanf(fp,"%d",&password); //提取原密码 fclose(fp); printf("Please enter tne original password: "); scanf("%d",&num); if(password==num) //与原密码对比 { printf("Please enter new password: "); //输入两次新密码 scanf("%d",&pass1); printf("Please enter a new password again: "); scanf("%d",&pass2); if(pass1==pass2) //两次密码一致则更改成功 { printf("Password change successfully!\n"); if((fp=fopen("password.txt","w"))==NULL) //保存新密码 { printf("Password file cannot open!\n"); exit(0); } fprintf(fp,"%d",pass1); fclose(fp); } } else { printf("Password mistake!\n"); } }
标签:
原文地址:http://blog.csdn.net/benjavan4641/article/details/50807660