69,377
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <errno.h>
typedef struct person //定义一个结构体
{
char name[10]; //姓名
char addr[30]; //地址
char offphnum[15]; //办公电话
char hmphnum[15]; //家庭电话
char mbphum[15]; //移动电话
struct person * next;
}listnode,*listlink; //结构体的别名
struct add_person
{
char name[10]; //姓名
char addr[30]; //地址
char offphnum[15]; //办公电话
char hmphnum[15]; //家庭电话
char mbphum[15]; //移动电话
};
listlink readfp() //将文件的信息读出并转存入链表中
{
FILE * fp ; //定义文件指针
struct add_person persons;
listnode * s ;
listlink head = NULL , end = NULL ;
fp = fopen("people.txt" , "rb");
if(fp == NULL)
{
printf("cannot open file \n");
return head;
}
fread(&persons , sizeof(struct add_person) , 1 , fp);
while(!feof(fp)) //判断文件是否结束(文件位置指示器是否到达了文件结尾)
{
s=(listnode*)malloc(sizeof(listnode)); //存入链表中
strcpy(s->name , persons.name) ; //提取数据并复制
strcpy(s->addr , persons.addr) ;
strcpy(s->offphnum , persons.offphnum) ;
strcpy(s->hmphnum , persons.hmphnum) ;
strcpy(s->mbphum , persons.mbphum) ;
s->next = NULL;
if(head==NULL)
head=end=s ;
else
{
end->next=s ;
end=s;
}
fread(&persons , sizeof(struct add_person) , 1 , fp) ;
}
return head;
}
void Save(listlink head) //保存信息
{
FILE * fp;
static struct add_person persons;
listlink p1;
fp=fopen("people.txt" , "wb");
if (!fp) {
fprintf(stderr, "%s\n", strerror(errno));
return;
}
//for(p1=head ; p1!=NULL ; p1=p1->next); //多一个分号
for(p1=head ; p1!=NULL ; p1=p1->next)
{
strcpy(persons.name , p1->name);
strcpy(persons.addr , p1->addr);
strcpy(persons.hmphnum , p1->hmphnum);
strcpy(persons.mbphum , p1->mbphum);
strcpy(persons.offphnum , p1->offphnum);
fwrite(&persons , sizeof(struct add_person ) , 1 ,fp);
}
fclose(fp);
}
listlink create() //建立通讯录函数
{
listlink s;
listlink head=NULL , end=NULL;
while(1)
{
s=(listlink)malloc(sizeof(listnode));
printf("\n\n\tcreate add_book's file \n");
printf("\n\n\t\tname:('#' is end)\n\t name:");
scanf("%s" , s->name);
if(strcmp(s->name , "#")==0)
break;
printf("\n\nAddr:\toffphnum: \thmphnum: \tmbphnum:\n");
scanf("%s %s %s %s", s->addr , s->offphnum , s->hmphnum , s->mbphum);
s->next=NULL;
if(head==NULL)
head=end=s;
else
{
end->next=s;
end=s;
}
}
free(s); //last node not be inserted into link
return(head);
}
void Show(listlink head) //显示所有信息
{
listnode *p1;
p1=head;
while(p1!=NULL)
{
printf("%s\t", p1->name);
printf("%s\t", p1->addr);
printf("%s\t", p1->offphnum);
printf("%s\t", p1->hmphnum);
printf("%s\t\n", p1->mbphum);
p1=p1->next;
}
}
void Delete(listlink *head) //定义一个删除的函数
{
listlink p1 , p2;
char name1[10];
p1=p2=*head;
if(p1==NULL)
{
printf("no record\n");
return;
}
printf("\n\n\tDelete----please input the name");
scanf("%s" , name1); //输入要删除人的姓名
while(strcmp(p1->name , name1)!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name , name1)==0) //根据判断情况做相应处理
{
if(p1==*head) {
//p1=p1->next;
*head = (*head)->next;
}
else
p2->next = p1->next;
free(p1);
return;
}
//else
printf("It is not exist in the addr-book!\n"); //要删除的记录不存在
}
void Find(listlink head) //查找指定记录信息
{
listlink p1;
char name1[10];
while(1)
{
p1=head;
printf("\n\n\tFind----please input the name: ('#' is end)\n\t name:");
scanf("%s", name1); //查找人的姓名
if(strcmp(name1,"#")==0)return;
while(strcmp(name1,p1->name)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(name1,p1->name)!=0)
printf("\n\n\tIt is not exist in the addr-book!");
else
{
printf("\tname:%s",p1->name);
printf("\taddr:%s",p1->addr);
printf("\tphnum:%s",p1->offphnum);
printf("\tphnum:%s",p1->hmphnum);
printf("\tphnum:%s",p1->mbphum);
}
}
}
void Input(listlink head) //向通讯录追加一个人的信息
{
listlink s , end;
s=(listlink)malloc(sizeof(listnode));
do
{
printf("\n\n\tInput----please input the sb's meg: \n\tname:('#' is end)\n\t name:");
scanf("%s",s->name);
if(strcmp(s->name,"#")==0)
return;
end=head;
if(end==NULL)break;
while((strcmp(end->name, s->name)!=0) && end->next!=NULL)
end=end->next;
}while(strcmp(end->name , s->name)==0);
printf("\n\nAddr:\toffphnum: \thmphnum: \tmbphnum:\n");
scanf("%s %s %s %s", s->addr , s->offphnum , s->hmphnum , s->mbphum);
s->next=NULL;
end->next=s;
}
void Alter(listlink head) //改变一个人的信息
{
listlink p1;
char name1[10] , phnum1[15] , phnum2[15] , phnum3[15] , add1[30];
printf("\n\n\tAlter----Please input the sname:");
scanf("%s",name1); //输入要修改人的姓名
p1=head;
while(strcmp(name1, p1->name)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(name1, p1->name)!=0)
{
printf("\n\n\tIt is not exist in the addr-book!");
return;
}
else
{
printf("\n\n\tPlease input the Alter meg!"); //输入要修改人的新信息
printf("\n\n addr: offphnum: hmphnum: mbphnum\n");
scanf("%s %s %s %s", add1 , phnum1 , phnum2 , phnum3);
strcpy(p1->name , name1);
strcpy(p1->addr, add1);
strcpy(p1->offphnum , phnum1);
strcpy(p1->hmphnum , phnum2);
strcpy(p1->mbphum , phnum3);
}
}
int main()
{
listlink head=NULL;
int sel;
head=readfp();
if(head==NULL)
head = create();
do
{
printf("\n WELCOME TO USE Address book"); //显示提示的信息
printf("\n 1.Show all the meg 2.Delete a piece of meg");
printf("\n 3.Find a piece of meg 4.Insert a piece of meg");
printf("\n 5.Alter a piece of meg");
printf("\n 0.Save and Exit");
printf("\n Input Your Choice(0-5):");
scanf("%d",&sel);
switch(sel)
{
case 1: Show(head);
printf("press anykey to return \n");
getchar();
break;
case 2: Delete(&head); //用二重指针解决无法删除第一个的问题,内部修改详见函数内部实现
break;
case 3: Find(head);
printf("press anykey to return \n");
getchar();
break;
case 4: Input(head);
break;
case 5: Alter(head);
break;
case 0: Save(head);
break;
}
}while(1);
}