#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct people)
struct people
{
char nam[20];
char mobilephone[20];
char officephone[20];
char homephone[20];
char email[20];
char province[20];
char workunit[20];
char familyaddress[20];
char classes[20];
struct people * next;
};
int n;
struct people *creat(void);
struct people *assign(struct people *head);
void add();
void del();
void modify();
void search();
void shuchu();
void main()
{struct people *p=NULL;
int m;
while(1)
{printf("\n **********通讯录管理系统**********\n");
printf("\n** 创建,请按1 **");
printf("\n** 追加,请按2 **");
printf("\n** 删除,请按3 **");
printf("\n** 修改,请按4 **");
printf("\n** 查询,请按5 **");
printf("\n** 输出,请按6 **");
printf("\n** 退出,请按0 **\n");
scanf("%d",&m);
if(m>=0&&m<=6)
{ switch(m)
{ case 1:p=creat();
break;
case 2: add();
break;
case 3: del();
break;
case 4: modify();
break;
case 5: search();
break;
case 6: shuchu();
break;
case 0: exit(0);
}
printf("操作完成,再次输入\n");
}
else
printf("操作错误,再次选择\n");
}
}//主函
struct people *creat(void)
{FILE *fp;
int a=1;
struct people *p1,*p2,*head;
p1=p2=head=(struct people*)malloc(LEN);
n=0;
while(a)
{p1=(struct people*)malloc(LEN);
printf("输入姓名");
scanf("%s",p1->nam);
printf("输入手机号");
scanf("%s",p1->mobilephone);
printf("输入办公室电话号");
scanf("%s",p1->officephone);
printf("输入家庭号码");
scanf("%s",p1->homephone);
printf("输入邮件号");
scanf("%s",p1->email);
printf("输入省市");
scanf("%s",p1->province);
printf("输入工作单位");
scanf("%s",p1->workunit);
printf("输入住址");
scanf("%s",p1->familyaddress);
printf("输入类别");
scanf("%s",p1->classes);
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
printf("是否输继续入信息,'是按1'or'否按0':\n");
scanf("%d",&a);
p1=(struct people*)malloc(LEN);
}
p2->next=NULL;
head=assign(head);
fp=fopen("peo.txt","w");
p1=head;
while(p1!=NULL)
{
fwrite(p1,LEN,1,fp);
p1=p1->next;
}
free(p1);
fclose(fp);
return(head);
}//建立连表、文件,排序
struct people *assign(struct people *head)
{struct people *first; //排列后有序链的表头指针
struct people *tail; //排列后有序链的表尾指针
struct people *p_min; //保留键值更小的节点的前驱节点的指针
struct people *min; //存储最小节点
struct people *p; //当前比较的节点
first = NULL;
while(head!=NULL)
{
for(p=head,min=head;p->next!=NULL; p=p->next)
{if(strcmp(p->next->nam,min->nam)<0)
{min=p->next;
p_min=p;
}
}
if(min==head)
head=head->next;
else
p_min->next=min->next;//中间和尾部节点删除
if(first==NULL)
{first=min;
tail=min;
}
else
{
tail->next=min;
tail=min;
}
}
tail->next=NULL;
head=first;
return(head);
}//排序
void add()
{ FILE *fp;
struct people *head,*p1,*p2;
p1=(struct people*)malloc(LEN);
if((fp=fopen("peo.txt","a"))==NULL)
{printf("无法打开文件");
exit(0);
}
printf("输入姓名");
scanf("%s",p1->nam);
printf("输入手机号");
scanf("%s",p1->mobilephone);
printf("输入办公室电话号");
scanf("%s",p1->officephone);
printf("输入家庭号码");
scanf("%s",p1->homephone);
printf("输入邮件号");
scanf("%s",p1->email);
printf("输入省市");
scanf("%s",p1->province);
printf("输入工作单位");
scanf("%s",p1->workunit);
printf("输入住址");
scanf("%s",p1->familyaddress);
printf("输入类别");
scanf("%s",p1->classes);
fwrite(p1,LEN,1,fp);
rewind(fp);
p1=(struct people*)malloc(LEN);
if((fp=fopen("peo.txt","r"))==NULL)
{printf("无法打开文件");
exit(0);
}
fread(p1,LEN,1,fp);
p2=p1;
head=p1;
while(!feof(fp))
{p1=(struct people*)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
head=assign(head);
p2=p1=(struct people*)malloc(LEN);
if((fp=fopen("stu.txt","w"))==NULL)
{printf("无法打开文件");
exit(0);
}
while(head!=NULL)
{fwrite(head,LEN,1,fp);
head=head->next;
}
free(p1);
fclose(fp);
}//添加
void del()
{ FILE *fp;
int i=1;
struct people *head,*p1,*p2;
while(i)
{p1=(struct people*)malloc(LEN);
if((fp=fopen("peo.txt","r+"))==NULL)
{printf("无法打开文件");
exit(0);
}
fread(p1,LEN,1,fp);
p2=p1;
head=p1;
while(!feof(fp))
{p1=(struct people*)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
rewind(fp);
p1=(struct people*)malloc(LEN);
printf("请输入要删除的姓名");
scanf("%s",p1->nam);
if(strcmp(p1->nam,head->nam)==0)
head=head->next;
else
{ p2=head;
while(p2->next!=NULL&&(p2->next->nam!=p1->nam))
p2=p2->next;
if(p2->next!=NULL)
{ if(p2->next->next==NULL)
p2->next=NULL;
else
p2->next=p2->next->next;
}
else
printf("要删除的人不存在");
}
if((fp=fopen("peo.txt","w"))==NULL)
{printf("无法打开文件");
exit(0);
}
rewind(fp);
while(head!=NULL)
{fwrite(head,LEN,1,fp);
head=head->next;
}
fclose(fp);
free(p1);
printf("\n已删除\n继续删除请输入1,否则输入0");
scanf("%d",&i);
}
}//删除
void modify()
{FILE *fp;
int m,i=1,k;
struct people *p1,*p2,*p;
p1=(struct people*)malloc(LEN);
while(i)
{ k=0;
printf("请输入要修改的人的姓名");
scanf("%s",p1->nam);
if((fp=fopen("peo.txt","r+"))==NULL)
{printf("无法打开文件");
exit(0);
}
while(!feof(fp))
{p2=(struct people*)malloc(LEN);
fread(p2,LEN,1,fp);
if(strcmp(p1->nam,p2->nam)==0)
{k=1;
break;
}
}
if(k)
{while(m!=7)
{ printf("\n 已找到该人,\n 修改姓名请输入1\n");
printf("\n 修改手机请输入2\n");
printf("\n 修改办公电话请输入3\n");
printf("\n 修改家庭电话请输入4\n");
printf("\n 修改电子邮箱请输入5\n");
printf("\n 修改所在省市请输入6\n");
printf("\n 修改工作单位请输入7\n");
printf("\n 修改家庭住址请输入8\n");
printf("\n 修改群组请输入9\n");
printf("\n 修改完成请输入10\n");
scanf("%d",&m);
if(m>=1&&m<=10)
{ switch(m)
{ case 1:{printf("输入新姓名");
scanf("%s",p2->nam);
fseek(fp,-LEN,1);
fw