diff --git a/通讯录.c b/通讯录.c index 5502e72..37a853c 100644 --- a/通讯录.c +++ b/通讯录.c @@ -1,19 +1,48 @@ #include -#include #include #include +#include //¶¨Ò庯Êý FILE *fp; //ÎļþÖ¸Õë void menu(); void add(); void search(); void modify(); +void dele(); void look(); void quit(); +void enroll(); +void login(); +void reenroll(); +void jiemian(); +void ycmm(); +void relogin(); + +int n; +int m; +int t; +int offset; +char mima_[10]; +char MIMA[4]="5555"; + +struct manager +{ + char name[10]; + char password[10]; +}manage[5]; + //Ö÷º¯Êý int main() { - menu(); + int a=0; + n=0; + do + { + jiemian(); + getchar(); + printf("\n°´1¼ÌÐø»Øµ½Ö÷½çÃæ,ÆäËüÈÎÒâ¼üÍ˳ö£º"); + a=getch(); + }while(a==49); return 0; } //ͨѶ¼½á¹¹Ìå @@ -25,35 +54,192 @@ struct user char tel2[15]; //µç»°2 char email[20]; //µç×ÓÓÊÏä }user[50]; -//²Ëµ¥ -void menu() + +void jiemian() { system("cls"); system("color f3"); int n; printf("\n\n\n\n\n"); printf("\t\t:---------ͨѶ¼---------:\n"); - printf("\t\t: 1:н¨ :\n"); - printf("\t\t: 2:²éѯ :\n"); - printf("\t\t: 3:ÐÞ¸Ä :\n"); - printf("\t\t: 4:ä¯ÀÀ :\n"); - printf("\t\t: 5:Í˳ö :\n"); - printf("\t\t:--ÊäÈëÊý <1-5>Ñ¡Ôñ¹¦ÄÜ--:\n\n"); + printf("\t\t: 1.×¢²á :\n"); + printf("\t\t: 2.µÇ¼ :\n"); + printf("\t\t:--ÊäÈëÊý <1-2>Ñ¡Ôñ¹¦ÄÜ--:\n\n"); scanf("%d",&n); switch(n) { - case 1: add(); break; //Ìí¼Ó - case 2: search(); break; //²éѯ - case 3: modify(); break; //ÐÞ¸Ä - case 4: look(); break; //ä¯ÀÀ - case 5: return; break; //Í˳ö + case 1: enroll(); break; //×¢²á + case 2: login(); break; //µÇ¼ default: return; - } -} -//Ìí¼ÓÓû§ÐÅÏ¢ + } +} +//×¢²á +void enroll() +{ + system("cls"); + int anhao; //¹ÜÀíÔ±ÃÜ³× + char zhanghao[10]; + char mima1[10]; + char mima2[10]; + + //¹ÜÀíÔ±×¢²á + printf("\nÇëÊäÈë°µºÅ£º"); + ycmm(); + + //°µºÅºË¶ÔÕýÈ· + if(!strcmp(mima_,MIMA)){ + fp=fopen("manager.txt","ab"); + struct manager one; + printf("\n\t\t\tÇëÊäÈëйÜÀíÔ±Õ˺ţº"); + scanf("%s",zhanghao); + printf("\n\t\t\tÇëÊäÈëйÜÀíÔ±ÃÜÂ룺"); + ycmm(); + strcpy(mima1,mima_); + printf("\n\t\t\tÇëÔÙ´ÎÈ·ÈÏÃÜÂ룺"); + ycmm(); + strcpy(mima2,mima_); + if(!strcmp(mima1,mima2)){ + strcpy(one.name,zhanghao); + strcpy(one.password,mima1); + fprintf(fp,"%-10s%-10s",one.name,one.password); + } + else{ + fclose(fp);reenroll();return; + } + printf("\t\t\t×¢²á³É¹¦£¡\n\n"); + fclose(fp); + printf("3Ãëºó½øÈë¹ÜÀíÔ±½çÃæ"); + Sleep(3000); + menu(); + return; + } + + //°µºÅ´íÎó£¬ÔÙ´Î×¢²á + else + printf("°µºÅ´íÎó£¬×¢²áʧ°Ü£¡\n\n");reenroll(); + return; +} + +//¼ÌÐø×¢²á +void reenroll() +{ + printf("¼ÌÐø×¢²áÇë°´1£º"); + if(getch()==49) + enroll(); + else + jiemian(); +} + +//µÇ¼ +void login() +{ + system("cls"); + char zhanghao[20]; + char mima[20]; + char password2[20]; + if((fp=fopen("manager.txt","rb"))==NULL){ + printf("δ²éѯµ½¹ÜÀíÔ±Õ˺Å\nÇëÏÈ×¢²á£¬3ÃëºóΪÄúÌøתµ½×¢²á½çÃæ"); + Sleep(3000); + enroll(); + exit(0); + } + struct manager one; + printf("\n\t\t\t¹ÜÀíÔ±Õ˺ţº"); + scanf("%s",zhanghao); + printf("\n\t\t\t¹ÜÀíÔ±ÃÜÂ룺"); + ycmm(); + while(!feof(fp)){ + fscanf(fp,"%s%s",one.name,one.password); + if(!strcmp(zhanghao,one.name)&&!strcmp(mima_,one.password)){ + printf("\nµÇ¼³É¹¦£¡\n"); + n=1; + fclose(fp); + printf("3Ãëºó½øÈë¹ÜÀíÔ±½çÃæ"); + Sleep(3000); + menu();return; + } + } + printf("\t\t\tµÇ¼ʧ°Ü£¡\n\n"); + + fclose(fp); + relogin(); + return; +} + +//¼ÌÐøµÇ¼ +void relogin() +{ + printf("¼ÌÐøµÇ¼Çë°´1£¬°´ÆäËüÈÎÒâ¼ü·µ»ØÖ÷½çÃ棺"); + if(getch()==49) + login(); + else + jiemian(); +} + +//Òþ²ØÃÜÂë +void ycmm() +{ + int i=0; + for(;i<10;i++) + { + mima_[i]=getch(); + if(mima_[i]=='\b') + { + if(i<=0) + printf(" "),i=1; + printf("\b \b"); + i=i-2; + continue; + } + if(mima_[i]=='\r') + { + break; + } + printf("*"); + } + mima_[i]='\0'; +} + +//²Ëµ¥ +void menu() +{ + int b=0; + do{ + system("cls"); + system("color f3"); + int a; + b=0; + printf("\n\n\n\n\n"); + printf("\t\t:----ͨѶ¼¹ÜÀíÔ±½çÃæ----:\n"); + printf("\t\t: 1:н¨ :\n"); + printf("\t\t: 2:²éѯ :\n"); + printf("\t\t: 3:ÐÞ¸Ä :\n"); + printf("\t\t: 4:ɾ³ý :\n"); + printf("\t\t: 5:ä¯ÀÀ :\n"); + printf("\t\t: 6:·µ»Ø :\n"); + printf("\t\t: 7:Í˳ö :\n"); + printf("\t\t:--ÊäÈëÊý <1-7>Ñ¡Ôñ¹¦ÄÜ--:\n\n"); + scanf("%d",&a); + switch(a) + { + case 1: add(); break; //Ìí¼Ó + case 2: search(); break; //²éѯ + case 3: modify(); break; //ÐÞ¸Ä + case 4: dele(); break; //ɾ³ý + case 5: look(); break; //ä¯ÀÀ + case 6: jiemian(); break; //Í˳ö + case 7: exit(0); + default: return; + } + printf("°´1·µ»Øµ½¹ÜÀíÔ±½çÃ棺"); + b=getch(); + }while(b==49); +} + +//Ìí¼ÓÓû§ÐÅÏ¢ 1.н¨ void add() { - struct user one; + struct user one={'\0','\0','\0','\0','\0'};//³õʼ»¯£¬·ÀÖ¹Îı¾Àï³öÏÖÂÒÂë system("color e3"); if((fp=fopen("user.txt","ab"))==NULL) { @@ -70,106 +256,99 @@ void add() scanf("%s",one.tel2); printf("\nÇëÊäÈëÓû§µç×ÓÓÊÏ䣺"); scanf("%s",one.email); - fwrite(&one,sizeof(struct user),1,fp); //°ÑÊý¾Ý¿é¼Èëuser.txtÖÐ + fprintf(fp,"%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); fclose(fp); - printf("\n×¢²á³É¹¦£¡\n"); - system("pause"); - menu(); -}; - -//ÐÞ¸Ä -void modify() + printf("\nÌí¼Ó³É¹¦£¡\n"); +} +//²éѯ 2.²éѯ +void search() { + struct user one; char namekey[8]; printf("ÊäÈëÒª²éѯµÄÐÕÃû£º"); scanf("%s",namekey); - if((fp=fopen("user.txt","ab+"))==NULL) //´ò¿ªÎļþ + if((fp=fopen("user.txt","rb"))==NULL) //´ò¿ªÎļþ { printf("\n²»ÄÜ´ò¿ªÎļþ£¡\n"); exit(0); } - int m=0,t=1; + t=0; + m=0; while(!feof(fp)) { - fread(&user[m],sizeof(struct user),1,fp)==1; //¶ÁÈ¡½á¹¹ÌåÖÐÒ»¸öÓû§ÐÅÏ¢ - if(!strcmp(user[m].name,namekey)) //ÅжÏÊÇ·ñÏàͬ + offset=ftell(fp); + fscanf(fp,"%s%s%s%s%s\n",one.name,one.place,one.tel1,one.tel2,one.email); + if(!strcmp(one.name,namekey)) //ÅжÏÊÇ·ñÏàͬ { - t=0; //ÈôÏàͬ£¬ÔòÔÚºóÃæif¾äÖÐÊä³ö + t=1; + printf("%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); //ÈôÏàͬ£¬ÔòÔÚºóÃæif¾äÖÐÊä³ö break; } - m++; } - if(t) - printf("δÕÒµ½\n"),fclose(fp),system("pause"),menu(); - else - { - printf("ÕÒµ½\n"); - printf("ÐÕÃû\t\t¼®¹á\t\tµç»°1\t\tµç»°2\t\tÓÊÏä\n"); - printf("%-8s\t",user[m].name); - printf("%-15s\t",user[m].place); - printf("%-15s\t",user[m].tel1); - printf("%-15s\t",user[m].tel2); - printf("%-20s\t",user[m].email); - } - printf("\nÄúÏëÒªÐÞ¸ÄÄÄÌõÐÅÏ¢£¿\n\n1.ÐÕÃû\t2.¼®¹á\t3.µç»°1\t4.µç»°2\t5.ÓÊÏä\tÆäËüÈÎÒâ¼ü.²»ÐÞ¸Ä"); //ÐÞ¸ÄÐÅÏ¢Ìáʾ - int a; //Ð޸ĵÄÐÅÏ¢ÐòºÅ - scanf("%d",&a); - char *newname; - char nweplace[15]; - char newtel1[15]; - char newtel2[15]; - char newemail[20]; - switch(a) - { - case 1:scanf("%s",newname),strcpy(user[m].name,newname),printf("%s",user[m].name);break; - default:break; - } - printf("Ð޸ijɹ¦\n"); + if(!t) + printf("δÕÒµ½\n"); fclose(fp); - system("pause"); - menu(); } -//²éѯ -void search() +//ÐÞ¸Ä 3.ÐÞ¸Ä +void modify() { - char namekey[8]; - printf("ÊäÈëÒª²éѯµÄÐÕÃû£º"); - scanf("%s",namekey); - if((fp=fopen("user.txt","r"))==NULL) //´ò¿ªÎļþ + int a; + char ch[10]; + struct user one; + search(); + if(t==1) { - printf("\n²»ÄÜ´ò¿ªÎļþ£¡\n"); - exit(0); + printf("ÇëÊäÈëÄãÏëÐ޸ĵÄÐÅÏ¢£º1.ÐÕÃû 2.¼®¹á 3.µç»°1 4.µç»°2 5.ÓÊÏä\n"); //ÐÞ¸ÄÐÅÏ¢Ìáʾ + scanf("%d",&a); + fp=fopen("user.txt","r+"); + printf("ÇëÊäÈëÐÂÐÅÏ¢£º"); + switch(a) + { + case 1: scanf("%s",one.name);fprintf(fp,"%-8s",one.name);break; + case 2: fseek(fp,offset+8,SEEK_SET); + scanf("%s",one.place);fprintf(fp,"%-15s",one.place);break; + case 3: fseek(fp,offset+23,SEEK_SET); + scanf("%s",one.tel1);fprintf(fp,"%-15s",one.tel1);break; + case 4: fseek(fp,offset+38,SEEK_SET); + scanf("%s",one.tel2);fprintf(fp,"%-15s",one.tel2);break; + case 5: fseek(fp,offset+53,SEEK_SET); + scanf("%s",one.email);fprintf(fp,"%-20s",one.email);break; + default: break; + } + fclose(fp); + printf("Ð޸ijɹ¦\n"); } - int m=0,t=0; - while(!feof(fp)) +} +//ɾ³ý 4.ɾ³ý +void dele() +{ + char a; + for(;;) { - fread(&user[m],sizeof(struct user),1,fp)==1; //¶ÁÈ¡½á¹¹ÌåÖÐÒ»¸öÓû§ÐÅÏ¢ - if(!strcmp(user[m].name,namekey)) //ÅжÏÊÇ·ñÏàͬ - { - t=1; //ÈôÏàͬ£¬ÔòÔÚºóÃæif¾äÖÐÊä³ö + search(); + if(t==1) + break; + else + printf("Í˳öÇë°´N,°´ÆäËüÈÎÒâ¼ü¼ÌÐø£º"); + a=getchar(); + if(a=='n'||a=='N') break; - } - m++; } - if(t) + + + if(t==1) { - printf("ÕÒµ½\n"); - printf("ÐÕÃû\t\t¼®¹á\t\tµç»°1\t\tµç»°2\t\tÓÊÏä\n"); - printf("%-8s\t",user[m].name); - printf("%-15s\t",user[m].place); - printf("%-15s\t",user[m].tel1); - printf("%-15s\t",user[m].tel2); - printf("%-20s\t",user[m].email); + fp=fopen("user.txt","r+"); + fseek(fp,offset,SEEK_SET); + fprintf(fp,"\n%-8s%-15s%-15s%-15s%-20s\n","","","","",""); + fclose(fp); + printf("ɾ³ý³É¹¦£¡\n"); } - else - printf("δÕÒµ½\n"); - fclose(fp); - system("pause"); - menu(); -} -//ä¯ÀÀ +} +//ä¯ÀÀ 5.ä¯ÀÀ void look() { + struct user one; char c; if((fp=fopen("user.txt","r"))==NULL) //´ò¿ªÎļþuser.txt { @@ -177,36 +356,16 @@ void look() exit(0); } int m=0; + printf("%-8s%-15s%-15s%-15s%-20s\n","ÐÕÃû","¼®¹á","µç»°1","µç»°2","ÓÊÏä"); while(!feof(fp)) { - if(fread(&user[m],sizeof(struct user),1,fp)==1) + fscanf(fp,"%s%s%s%s%s\n",one.name,one.place,one.tel1,one.tel2,one.email); + printf("%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); m++; } - int i; - printf("ÐÕÃû\t\t¼®¹á\t\tµç»°1\t\tµç»°2\t\tÓÊÏä\n"); - for(i=0;i