发布网友 发布时间:2022-05-17 01:41
共1个回答
热心网友 时间:2023-09-19 10:12
呵呵,代码帮你改编如下(调试通过),其他改进功能就靠你自己了: #include<stdio.h> /*输入密码*/ secret(char insecret[6]) { int i=0; printf("\n-----------\n| |"); printf("\n-----------\n"); insecret[i]=getch(); printf("*"); while(insecret[i]!=13&&i<6) { i++; insecret[i]=getch(); printf("*"); } } /*更改输入密码*/ altersecret() { FILE *fp ; char secretch1[6]={0},secretch2[6]={0}; int i=10; if((fp=fopen("secret.txt","r"))==NULL); r: printf("请输入6位初始密码") ; secret(secretch1); fread(secretch2,6,1,fp); i=0; while(i<6) { if(secretch1[i]==secretch2[i]) i++; else break; } if(i==6) { if((fp=fopen("secret.txt","wb"))==NULL) { printf("cant open the file"); exit(0); } else{ system("cls"); alts(fp); fclose(fp); } } else goto r; } /*输入密码比较与记录*/ alts(FILE *fp){ int i; char secretch1[6]={0},secretch2[6]={0}; reinput: printf("请输入新密码:") ; secret(secretch1); system("cls"); printf("密码确认:"); secret(secretch2); i=0; while(i<6) { if(secretch1[i]==secretch2[i]) i++; else break; } if(i==6) { fwrite(secretch1,6,1,fp); rewind(fp); } else { system("cls"); goto reinput; } } void printchoice() { printf("************************\n"); printf("* input you choice *\n"); printf("* 1 enter the system *\n"); printf("* 2 alter the scret *\n"); printf("* 3 exit *\n"); printf("************************\n"); } /*主程序*/ void main() { char secretch1[6]={0},secretch2[6]={0}; int i=10; int choice; FILE *fp; if((fp=fopen("secret.txt","r"))==NULL) { if((fp=fopen("secret.txt","wb"))==NULL) { printf("cant open the file"); exit(0); } else { printf("初次进入系统,") ; alts(fp); fclose(fp); while(1) { system("cls"); printchoice(); scanf("%d",&choice); switch (choice) { case 1: printf("entry the system sucess");getch();break; case 2: altersecret();break; case 3: printf("exit the system!(Y/N)?\n"); if(getch()=='Y'||getch()=='y');exit(0);break; } } } } else{ reinputsecret: printf("请输入密码") ; secret(secretch1); fread(secretch2,6,1,fp); rewind(fp); i=0; while(i<6) {if(secretch1[i]==secretch2[i]) i++; else break;} if(i==6) { fclose(fp); while(1) { system("cls"); printchoice(); scanf("%d",&choice); switch (choice) { case 1: printf("entry the system sucess");getch();break; case 2: altersecret();break; case 3: printf("exit the system!(Y/N)?\n"); if(getch()=='Y'||getch()=='y');exit(0);break; } } } else { system("cls"); goto reinputsecret; } } getch(); }