haihongyuan.com
海量文库 文档专家
全站搜索:
您现在的位置:首页 > 幼儿教育 > 幼儿读物幼儿读物

用C语言编写通讯录

发布时间:2014-01-15 15:04:35  

#include<stdio.h>
#include<string.h>
typedef struct Friends
{
char sName[30];
int tel;
int sMovtel;
int home;
char Emaile[30];
char assort[30];
}Friends;
int c;
void jianli ();
void search ();
void amend ();
void change ();
void input (Friends *p);
void output (Friends *p);
void sdele ();
void fugai ();
int main()
{
int admin,a,b=1;
char s;
while(b==1)
{
printf("是否以管理员身份登录?(1是 OR 2否)");
fflush(stdin);
scanf("%d",&admin);
switch(admin)
{
case 1:
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)
{
case 1:jianli ();break;
case 2:search ();break;
case 3:amend ();break;
case 4:sdele();break;
}
printf("是否返回主菜单('Y'or'N')\n");
fflush(stdin);
scanf("%c",&s);
if(s=='y'|| s=='Y')
b=1;
else
break;
}
break;
case 2:
while(b==1)
{
printf("\t\t通讯录\n");
printf("\t1、查找\n");
printf("请选择.......\n");
scanf("%d",&a);
switch(a)
{
case 1:search ();break;
}
printf("是否返回主菜单('Y'or'N')\n");
fflush(stdin);
scanf("%c",&s);
if(s=='y'|| s=='Y')
b=1;
else
b=2;
break;
}
break;
}
}
return 0;
}
/*新建*/
void jianli ()
{
int b=1;
char d;
Friends x;
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;
}
else
break;
}
fclose(fp);
}

/*查找*/
void search ()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
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);
}
}
}
/*修改*/
void amend ()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
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);
}
}
}
/*删除*/
void sdele()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
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);
}
}
}
void change ()/*用于修改*/
{
int f,j;
FILE *fp;
Friends z ;
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)
{
case 1: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);
case 2: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;
case 3: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);
case 4: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;
case 5: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;
case 6: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;
}
}
void input (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);
}
void output (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);
}
void fugai ()/*用于删除 */
{
Friends d;
FILE *fp;
fp=fopen("e:\\通讯录.txt","rb");
fread(&d,sizeof(d),1,fp);
}

网站首页网站地图 站长统计
All rights reserved Powered by 海文库
copyright ©right 2010-2011。
文档资料库内容来自网络,如有侵犯请联系客服。zhit326@126.com