#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;/*与malloc.h差不多*/
#include %26lt;string.h%26gt;
#define maxlen 100
strUCt persons
{char name[10];/*定义结构体数组用于缓存数据*/
char addr[20];
char phnum[10];
}persons[maxlen];
;
;
;
typedef struct lnode{/*通讯录结构中结点的定义*/
char name[10];/*姓名*/
char addr[20];/*地址*/
char phnum[10];/*电话*/
struct lnode *next;
}listnode,*linklist;
;
;
;
linklist head=NULL,r=NULL;/*定义头指针和尾指针*/
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7;
int i;
char name1[10],ch;
char str1[20];
FILE *fp;/*定义文件指针*/
;
;
void creat()/*将文件的信息读入结构体数组在转存入链表中*/
{ int j;
long k;
fp=fopen("people.txt","r+t");/*打开文件*/
if(fp!=NULL)
{for(i=1;i%26lt;maxlen;i++)
{j=fgetc(fp);
if(j==EOF)
return;
k=i-1;
fseek(fp,k*sizeof(struct persons),0);/*读取一个人的信息*/
fread(%26amp;persons[i],sizeof(struct persons),1,fp);
s=(linklist)malloc(sizeof(listnode));/*装存入链表中*/
strcpy(s-%26gt;name,persons[i].name);
strcpy(s-%26gt;addr,persons[i].addr);
strcpy(s-%26gt;phnum,persons[i].phnum);
if(head==NULL)/*用尾插法将其插入链表中*/
head=s;
else
r-%26gt;next=s;
r=s;}
}
else
{ fp=fopen("people.txt","w");/*不能打开另开辟一个文件*/
i=1;
}
}
;
;
;
void Show()/*显示所有的信息*/
{ p1=head;
while(p1!=NULL)
{printf(" name:%s ",p1-%26gt;name);
printf(" addr:%s",p1-%26gt;addr);
printf(" phnum:%s",p1-%26gt;phnum);
p1=p1-%26gt;next;
}
}
;
;
;
void Delete()/*定义一个删除的函数*/
{printf(" please input the name:");
gets(name1);/*输入要删除人的姓名*/
p4=head;
if(strcmp(p4-%26gt;name,name1)==0)
{ p4=p4-%26gt;next;/*根据各种情况判定可能性*/
head=p4;
}
else
{ while(strcmp(p4-%26gt;next-%26gt;name,name1)!=0)
p4=p4-%26gt;next;
p5=p4-%26gt;next;
p4-%26gt;next=p5-%26gt;next;
free(p5);
}
}
;
;
;
void Find()/*查找的函数定义*/
{ printf(" please input the name:");
p0=head;
gets(name1);/*查找人的姓名输入*/
while(strcmp(name1,p0-%26gt;name)!=0%26amp;%26amp;p0!=NULL)
p0=p0-%26gt;next;
if(p0==NULL)
printf(" It is not exit in the addr-book!");
else
{ printf(" name:%s ",p0-%26gt;name);
printf(" addr:%s",p0-%26gt;addr);
printf(" phnum:%s",p0-%26gt;phnum);
}
}
;
;
;
void Input()/*向通讯录中输入一个人的信息*/
{s=(linklist)malloc(sizeof(listnode));
printf(" please input the sb's meg:");
printf(" name:");
scanf("%s",s-%26gt;name);
printf(" Addr:");
scanf("%s",s-%26gt;addr);
printf(" phnum:");
scanf("%s",s-%26gt;phnum);
if(head==NULL)
head=s;
else
r-%26gt;next=s;
r=s;
}
;
;
;
void Alter()/*改变一个人的信息*/
{
int j;
printf(" Please input the name:");
gets(name1);/*输入要人的姓名*/
p3=head;
while(strcmp(name1,p3-%26gt;name)!=0%26amp;%26amp;p3!=NULL)
p3=p3-%26gt;next;
if(p3==NULL)
printf(" It is not exit in the addr-book!");
else
{ printf(" please input the new meg!");/*输入要改人的新信息*/
printf(" name:");
scanf("%s",name1);
strcpy(p3-%26gt;name,name1);
printf(" Addr:");
scanf("%s",name1);
strcpy(p3-%26gt;addr,name1);
printf(" phnum:");
scanf("%s",name1);
strcpy(p3-%26gt;phnum,name1);
}
}
;
;
;
void Save()/*保存信息*/
{int j;
fp=fopen("people.txt","w");
for(p2=head,j=0;p2!=NULL;j++,p2=p2-%26gt;next)/*将信息装出入结构体数组在出入链表中*/
{ /*避免地址的出入,造成乱码文件*/
strcpy(persons[j].name,p2-%26gt;name);
strcpy(persons[j].addr,p2-%26gt;addr);
strcpy(persons[j].phnum,p2-%26gt;phnum);
fwrite(%26amp;persons[j],sizeof(struct persons),1,fp);
}
}
;
;
;
void main()
{creat();
do
{ printf(" WELCOME TO USE Pan Weifeng's Address book");/*显示提示的信息*/
printf(" Please make a choice below:");
printf(" 1.Show all the meg");
printf(" 2.Delete a piece of meg");
printf(" 3.Find a piece of meg");
printf(" 4.Insert a piece of meg");
printf(" 5.Alter a piece of meg");
printf(" 6.Save and Exit");
printf(" ");
printf(" Input Your Choice:");
ch=getche();
switch(ch)
{ case '1': Show();/*用单条件多选择语句实现调用与循环*/
break;
case '2': Delete();
break;
case '3': Find();
break;
case '4': Input();
break;
case '5': Alter();
break;
case '6': Save();
fclose(fp);
exit(0);
break;
default:
printf(" ********************************* ");
printf(" The num should 1-4!!! ");
printf(" **********************************");
break;
}
}while(1);
}
