文档库 最新最全的文档下载
当前位置:文档库 › C语言 编写1个简单的通讯录管理系统

C语言 编写1个简单的通讯录管理系统

/*---------------------------------------------------------
程序L14_9.C : 编写1个简单的通讯录管理系统。可以对一个通讯录进行输入、显示、查找、删除等,

最后结果保存到一个文件中。
-----------------------------------------------------------*/

#include
#include
#include
#include
#define N 3
#define AD struct address_list
struct person
{
char name[20]; /* 姓名 */
char address[40]; /* 通讯地址 */
long brithday;
char sex;
int age; /* 出生年月 */
char phone[15]; } ; /* 联系电话 */

AD
{
char name[20];
char address[40];
long brithday;
char sex;
int age;
char phone[15];
AD *next; } ;

FILE *fp;

AD *load(char filename[]) /* 由文件中的数据生成一个通讯录的链表 , */
{ AD *p,*q,*head;
struct person per;
head=(AD *)malloc(sizeof(AD));
q=head=NULL;
if ((fp=fopen(filename,"rb"))==NULL)
return head;
else
{ while (!feof(fp))
{ if(fread(&per,sizeof(struct person),1,fp)==1)
{p=(AD *)malloc(sizeof(AD));
strcpy(p->name,https://www.wendangku.net/doc/0c926651.html,); strcpy(p->address,per.address);
p->brithday=per.brithday; strcpy(p->sex,per.sex);
strcpy(p->age,per.age); strcpy(p->phone,per.phone);

head=p; p->next=q; q=head;
}
else {printf("文件无法读取数据。"); exit(1);}
}
fclose(fp);
return(head);
}
}

/* 输入一条通讯录内容 */

AD *insert(AD *head)
{ AD *temp,*p;
p=head;
temp=(AD *)malloc(sizeof(AD));
printf("\n\t请输入姓名:"); scanf("%s",temp->name);
printf("\n\t请输入通讯地址:"); scanf("%s",temp->address);
printf("\n\t请输入出生年月:"); scanf("%ld",&temp->brithday);
printf("\n\t请输入性别:"); scanf("%s",temp->sex);
printf("\n\t请输入年龄:"); scanf("%d",temp->age);
printf("\n\t请输入电话号码:"); scanf("%s",temp->phone);
head=temp;
temp->next=p;
return head;}

void save(AD *head,char filename[]) /* 将通讯录链表中的内容保存到文件中 */
{ AD *p;
struct person per;
if ((fp=fopen(filename,"wb"))==NULL)
{printf("文件无法写入"); exit(1); }
else
{p=head;
while(p!=NULL)
{ strcpy(https://www.wendangku.net/doc/0c926651.html,,p->name); strcpy(per.address,p->address);
per.brithday=p->brithday; strcpy(per.sex,p->sex);
strcpy(per.age,p->age); strcpy(per.phone,p->phone);
if(fwrite(&per,sizeof(struct person),1,fp)!=1)
{ printf("文件不能写入数据,请检查后重新运行.\n");exit(1);}
p=p->next;
}
fclose(fp);
}
}

void show(AD *head) /* 显示通讯录内容 */
{ AD *p;
p=head;
while(p!=NULL)
{ printf("name:%s\n",p->name);
printf("address:

%s\n",p->address);
printf("brithday:%ld\t\t",p->brithday);
printf("phone:%s\n\n",p->phone);
p=p->next;
}
}

void find(AD *head) /* 按姓名查找通讯地址等 */
{ AD *p;
char name[20];
printf("请输入要查找的人的姓名:");
scanf("%s",name);
p=head;
while(p!=NULL)
{ if (strcmp(name,p->name)==0)
{ printf("name:%s\n",p->name);
printf("address:%s\n",p->address);
printf("brithday:%ld\t\t",p->brithday);
printf("phone:%s\n\n",p->phone);
break;}
else p=p->next;
}
if (p==NULL) printf("\n\t\t查无此人\n\n");
}

AD *delete(AD *head) /* 按姓名删除通讯录中的一条记录 */
{ AD *p,*q;
char name[20];
printf("请输入要删除的人的姓名:");
scanf("%s",name);
p=q=head;
while(p!=NULL)
{ if (strcmp(name,p->name)==0)
{ if (head==p) head=p->next;
else q->next=p->next;
free(p); break;}
else {q=p; p=p->next;}
}
if (p==NULL) printf("\n\t\t查无此人\n\n");
return head;}

void main()
{
AD *head;
char fname[20];
char choise;
printf("请输入通讯录文件名:");
scanf("%s",fname);
head=load(fname);
while(1)
{ printf("\t\t 通讯录管理系统\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("\n\n\t请选择(1~5):");
getchar();
scanf("%c",&choise);
switch(choise)
{case '1' : head=insert(head);break;
case '2' : show(head);break;
case '3' : find(head);break;
case '4' : head=delete(head);break;
case '5' : save(head,fname);exit(0);
default : printf("输入错误,请重新输入!\n");
}
}
}



相关文档
相关文档 最新文档