您好,欢迎访问三七文档
#includestdio.h#includestring.htypedefstructFriends{charsName[30];inttel;intsMovtel;inthome;charEmaile[30];charassort[30];}Friends;intc;voidjianli();voidsearch();voidamend();voidchange();voidinput(Friends*p);voidoutput(Friends*p);voidsdele();voidfugai();intmain(){inta,b=1;chars;while(b==1){printf(\t\t通讯录\n);printf(\t1、新建\n);printf(\t2、查找\n);printf(\t3、修改\n);printf(\t4、删除\n);printf(请选择.......\n);scanf(%d,&a);switch(a){case1:jianli();break;case2:search();break;case3:amend();break;case4:sdele();break;}printf(是否返回主菜单('Y'or'N')\n);fflush(stdin);scanf(%c,&s);if(s=='y'||s=='Y')b=1;elsebreak;}return0;}/*新建*/voidjianli(){intb=1;chard;Friendsx;FILE*fp;fp=fopen(e:\\通讯录.txt,ab);while(b==1){input(&x);fwrite(&x,sizeof(x),1,fp);printf(是否继续新建('Y'or'N')?\n);fflush(stdin);scanf(%c,&d);if(d=='y'||d=='Y'){b=1;}elsebreak;}fclose(fp);}/*查找*/voidsearch(){inth,j=1,n=0,num;chark[20];FILE*fp;Friendsy;printf(\t1、按号码查找\n);printf(\t2、按姓名查找\n);printf(请选择...........\n);scanf(%d,&h);if(h==1){printf(\t请输入你查找的号码!\n);while(j==1){scanf(%d,&num);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){fread(&y,sizeof(y),1,fp);if(num==y.sMovtel){output(&y);break;}}if(num!=y.sMovtel){printf(你输入的号码有误!!!\n请重新输入\n);j=1;}fclose(fp);}}if(h==2){printf(\t请输入你查找的姓名!\n);while(j==1){scanf(%s,k);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){fread(&y,sizeof(y),1,fp);if(strcmp(k,y.sName)==0){output(&y);break;}}if(strcmp(k,y.sName)!=0){printf(你输入的姓名有误!!!\n请重新输入\n);j=1;}fclose(fp);}}}/*修改*/voidamend(){inth,j=1,n=0,num;chark[20];FILE*fp;Friendsy;printf(\t1、按号码查找你所需要修改的原始资料\n);printf(\t2、按姓名查找你所需要修改的原始资料\n);printf(请选择...........\n);scanf(%d,&h);if(h==1){printf(\t请输入查找你的号码!\n);while(j==1){scanf(%d,&num);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){c++;fread(&y,sizeof(y),1,fp);if(num==y.sMovtel){change();break;}}if(num!=y.sMovtel){printf(你输入的号码有误!!!\n请重新输入\n);j=1;}fclose(fp);}}if(h==2){printf(\t请输入你查找的姓名!\n);while(j==1){c++;scanf(%s,k);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){fread(&y,sizeof(y),1,fp);if(strcmp(k,y.sName)==0){change();break;}}if(strcmp(k,y.sName)!=0){printf(你输入的姓名有误!!!\n请重新输入\n);j=1;}fclose(fp);}}}/*删除*/voidsdele(){inth,j=1,n=0,num;chark[20];FILE*fp;Friendsy;printf(\t1、按号码查找你所需要修改的原始资料\n);printf(\t2、按姓名查找你所需要修改的原始资料\n);printf(请选择...........\n);scanf(%d,&h);if(h==1){printf(\t请输入查找你的号码!\n);while(j==1){scanf(%d,&num);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){fread(&y,sizeof(y),1,fp);if(num==y.sMovtel){fugai();break;}}if(num!=y.sMovtel){printf(你输入的号码有误!!!\n请重新输入\n);j=1;}fclose(fp);}}if(h==2){printf(\t请输入你查找的姓名!\n);while(j==1){scanf(%s,k);fp=fopen(e:\\通讯录.txt,rb);while(!feof(fp)){fread(&y,sizeof(y),1,fp);if(strcmp(k,y.sName)==0){fugai();break;}}if(strcmp(k,y.sName)!=0){printf(你输入的姓名有误!!!\n请重新输入\n);j=1;}fclose(fp);}}}voidchange()/*用于修改*/{intf,j;FILE*fp;Friendsz;j=c*133;printf(你要修改的内容:\n);printf(\t1、姓名\n\t2、手机号\n\t3、固定电话号码\n);printf(\t4、电子信箱\n\t5、家庭住址、\n\t6、分类\n);printf(请选择...........\n);scanf(%d,&f);printf(输入你的新信息!\n);switch(f){case1:fp=fopen(e:\\通讯录.txt,wb);fread(&z,sizeof(z),1,fp);fseek(fp,j,SEEK_SET);scanf(%s,z.sName);fwrite(&z.sName,sizeof(z.sName),1,fp);fclose(fp);output(&z);break;fclose(fp);case2:fp=fopen(e:\\通讯录.txt,rb);fread(&z,sizeof(z),1,fp);fseek(fp,(j+31),SEEK_SET);scanf(%d,&z.sMovtel);fwrite(&z.sMovtel,sizeof(z.sMovtel),1,fp);fclose(fp);break;case3:fp=fopen(e:\\通讯录.txt,rb);fread(&z,sizeof(z),1,fp);fseek(fp,(j+35),SEEK_SET);scanf(%d,&z.tel);fwrite(&z.tel,sizeof(z.tel),1,fp);fclose(fp);case4:fp=fopen(e:\\通讯录.txt,rb);fread(&z,sizeof(z),1,fp);fseek(fp,(j+73),SEEK_SET);scanf(%s,z.Emaile);fwrite(&z.Emaile,sizeof(z.Emaile),1,fp);fclose(fp);break;case5:fp=fopen(e:\\通讯录.txt,rb);fread(&z,sizeof(z),1,fp);fseek(fp,(j+69),SEEK_SET);scanf(%d,&z.home);fwrite(&z.home,sizeof(z.home),1,fp);fclose(fp);break;case6:fp=fopen(e:\\通讯录.txt,rb);fread(&z,sizeof(z),1,fp);fseek(fp,(j+103),SEEK_SET);scanf(%d,&z.assort);fwrite(&z.assort,sizeof(z.assort),1,fp);fclose(fp);break;}}voidinput(Friends*p)/*输入*/{printf(请输入姓名:\n);scanf(%s,p-sName);printf(请输入手机号码:\n);scanf(%d,&p-sMovtel);printf(请输入固定电话号码:\n);scanf(%d,&p-tel);printf(请输入电子信箱:\n);scanf(%s,p-Emaile);printf(输入家庭住址);scanf(%s,p-home);printf(请分类:\n);scanf(%s,p-assort);}voidoutput(Friends*p)/*输出*/{printf(**********************\n);printf(%s\n,p-sName);printf(%d\n,p-sMovtel);printf(%d\n,p-tel);printf(%s\n,p-Emaile);printf(%d\n,p-home);printf(%s\n,p-assort);}voidfugai()/*用于删除*/{Friendsd;FILE*fp;fp=fopen(e:\\通讯录.txt,rb);fread(&d,sizeof(d),1,fp);}
本文标题:用C语言编写通讯录
链接地址:https://www.777doc.com/doc-5554000 .html