#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() { int a=0; n=0; do { jiemian(); getchar(); printf("\n按1继续回到主界面,其它任意键退出:"); a=getch(); }while(a==49); return 0; } //通讯录结构体 struct user { char name[8]; //姓名 char place[15]; //籍贯 char tel1[15]; //电话1 char tel2[15]; //电话2 char email[20]; //电子邮箱 }user[50]; 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:--输入数 <1-2>选择功能--:\n\n"); scanf("%d",&n); switch(n) { 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={'\0','\0','\0','\0','\0'};//初始化,防止文本里出现乱码 system("color e3"); if((fp=fopen("user.txt","ab"))==NULL) { printf("打开文件失败!\n"); exit(0); } printf("\n请输入用户姓名:"); scanf("%s",one.name); printf("\n请输入用户籍贯:"); scanf("%s",one.place); printf("\n请输入用户电话号码1:"); scanf("%s",one.tel1); printf("\n请输入用户电话号码2:"); scanf("%s",one.tel2); printf("\n请输入用户电子邮箱:"); scanf("%s",one.email); fprintf(fp,"%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); fclose(fp); printf("\n添加成功!\n"); } //查询 2.查询 void search() { struct user one; char namekey[8]; printf("输入要查询的姓名:"); scanf("%s",namekey); if((fp=fopen("user.txt","rb"))==NULL) //打开文件 { printf("\n不能打开文件!\n"); exit(0); } t=0; m=0; while(!feof(fp)) { 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=1; printf("%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); //若相同,则在后面if句中输出 break; } } if(!t) printf("未找到\n"); fclose(fp); } //修改 3.修改 void modify() { int a; char ch[10]; struct user one; search(); if(t==1) { 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("修改成功\n"); } } //删除 4.删除 void dele() { char a; for(;;) { search(); if(t==1) break; else printf("退出请按N,按其它任意键继续:"); a=getchar(); if(a=='n'||a=='N') break; } if(t==1) { fp=fopen("user.txt","r+"); fseek(fp,offset,SEEK_SET); fprintf(fp,"\n%-8s%-15s%-15s%-15s%-20s\n","","","","",""); fclose(fp); printf("删除成功!\n"); } } //浏览 5.浏览 void look() { struct user one; char c; if((fp=fopen("user.txt","r"))==NULL) //打开文件user.txt { printf("读取信息失败!\n"); exit(0); } int m=0; printf("%-8s%-15s%-15s%-15s%-20s\n","姓名","籍贯","电话1","电话2","邮箱"); while(!feof(fp)) { 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++; } if(fclose(fp)) //关闭文件 { printf("关闭文件失败!\n"); exit(0); } }