C语言编写1个简单的通讯录管理系统.docx
C语言 编写1个简单的通讯录管理系统/*-程序L14_9.C : 编写1个简单的通讯录管理系统。可以对一个通讯录进行输入、显示、查找、删除等,最后结果保存到一个文件中。-*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#define N 3#define AD struct address_liststruct personchar name20; /* 姓名 */char address40; /* 通讯地址 */long brithday;char sex;int age; /* 出生年月 */char phone15; ; /* 联系电话 */ADchar name20;char address40;long brithday;char sex;int age;char phone15;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,per.name); 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("nt请输入姓名:"); scanf("%s",temp->name);printf("nt请输入通讯地址:"); scanf("%s",temp->address);printf("nt请输入出生年月:"); scanf("%ld",&temp->brithday);printf("nt请输入性别:"); scanf("%s",temp->sex);printf("nt请输入年龄:"); scanf("%d",temp->age);printf("nt请输入电话号码:"); 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); elsep=head;while(p!=NULL) strcpy(per.name,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:%sn",p->name);printf("address:%sn",p->address);printf("brithday:%ldtt",p->brithday);printf("phone:%snn",p->phone);p=p->next;void find(AD *head) /* 按姓名查找通讯地址等 */ AD *p;char name20;printf("请输入要查找的人的姓名:");scanf("%s",name);p=head;while(p!=NULL) if (strcmp(name,p->name)=0) printf("name:%sn",p->name);printf("address:%sn",p->address);printf("brithday:%ldtt",p->brithday);printf("phone:%snn",p->phone);break;else p=p->next;if (p=NULL) printf("ntt查无此人nn");AD *delete(AD *head) /* 按姓名删除通讯录中的一条记录 */ AD *p,*q;char name20;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("ntt查无此人nn");return head;void mainAD *head;char fname20;char choise;printf("请输入通讯录文件名:");scanf("%s",fname);head=load(fname);while(1) printf("tt 通讯录管理系统n");printf("tt=n");printf("tt 1. 插入一条记录n");printf("tt 2. 显示所有记录n");printf("tt 3. 按姓名查找n");printf("tt 4. 按姓名删除n");printf("tt 5. 存盘并退出n");printf("nnt请选择:");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");