#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct person /*通讯录结构中结点的定义*/
{
char name[10];
char addr[30];
char offphnum[15];
char hmphnum[15];
char mbphnum[15];
struct person *next;
}listnode,*listlink;
struct add_person /*通讯录记录结构*/
{
char name[10];
char addr[30];
char offphnum[15];
char hmphnum[15];
char mbphnum[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("不能打开文件\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->mbphnum,persons.mbphnum);
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;
}
listlink create() /* 建立通讯录函数 */
{
listlink s;
listlink head=NULL,end=NULL;
while(1)
{
s=(listlink)malloc(sizeof(listnode));
printf("\n\t 创建一个addr-book\n");
printf("\n\n\t 姓名:('#' 结束)\n 姓名:");
scanf("%s",s->name);
if(strcmp(s->name,"#")==0)
break;
printf("\n地址:");
scanf("%s",s->addr);
printf("\n办公号码:");
scanf("%s",s->offphnum);
printf("\n家庭号码:");
scanf("%s",s->hmphnum);
printf("\n移动号码:");
scanf("%s",s->mbphnum);
s->next=NULL;
if(head==NULL)
head=end=s;
else
{
end->next=s;
end=s;
}
}
return(head);
}
void save(listlink head)/*保存所有信息*/
{
FILE *fp;
static struct add_person persons;
listlink p1;
fp=fopen("people.txt","wb");
for(p1=head;p1!=NULL;p1=p1->next)
{
strcpy(persons.name,p1->name);
strcpy(persons.addr,p1->addr);
strcpy(persons.offphnum,p1->offphnum);
strcpy(persons.hmphnum,p1->hmphnum);
strcpy(persons.mbphnum,p1->mbphnum);
fwrite(&persons,sizeof(struct add_person),1,fp);
}
fclose(fp);
}
void show(listlink head)/* 显示所有的信息 */
{
listnode *p1;
p1=head;
while(p1!=NULL)
{
printf("\n名字:");
printf("%s\n",p1->name);
printf("地址:");
printf("%s\n",p1->addr);
printf("办公号码:");
printf("%s\n",p1->offphnum);
printf("家庭号码:");
printf("%s\n",p1->hmphnum);
printf("移动号码:");
printf("%s\n",p1->mbphnum);
p1=p1->next;
}
}
void deleted(listlink head)/* 删除信息 */
{
listlink p1,p2;
char name1[10];
p1=p2=head;
if(p1==NULL)
{
printf("没有记录\n");
return;
}
printf("\n\n删除-----请输入要删除的名字:");
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;
else
p2->next=p1->next;
free(p1);
}
else
printf("它不存在于 addr-book!\n");
}
void input(listlink head)/*向通讯录添加一个人信息 */
{
listlink s,end;
s=(listlink)malloc(sizeof(listnode));
do
{
printf("\n\n\t输入---请输入个人名字
\t('#'结束输入) \n\t名字:");
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地址:");
scanf("%s",s->addr);
printf("\n办公号码:");
scanf("%s",s->offphnum);
printf("\n家庭号码:");
scanf("%s",s->hmphnum);
printf("\n移动号码:");
scanf("%s",s->mbphnum);
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\t修改----请输入修改的名字");
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\t它不存在于 addr-book!");
return;
}
else
{ printf("请输入要修改的信息");
printf("\n地址:");
scanf("%s",add1);
printf("\n办公号码:");
scanf("%s",phnum1);
printf("\n家庭号码:");
scanf("%s",phnum2);
printf("\n移动号码:");
scanf("%s",phnum3);
strcpy(p1->name,name1);
strcpy(p1->addr,add1);
strcpy(p1->offphnum,phnum1);
strcpy(p1->hmphnum,phnum2);
strcpy(p1->mbphnum,phnum3);
}
}
void find(listlink head)
{
listlink p1;
char name1[10];
while(1)
{
p1=head;
printf("\n\n\tfind---请输入名字:('#'is end)\n名字:");
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\t 它不存在于 addr-book!");
else
{
printf("\n名字:%s",p1->name);
printf("\n地址:%s",p1->addr);
printf("\n电话1:%s",p1->offphnum);
printf("\n电话2:%s",p1->hmphnum);
printf("\n电话3:%s",p1->mbphnum);
}
}
}
void main()
{ printf("\n\n\n\n\n\t\t\t\t简易通讯录");
printf("\n\t\t\t 我的学号:2013914204");
printf("\n\t\t\t\t按任何键开始");
getch();
system("CLS");
listlink head=NULL;
int sel;
head=readfp();
if(head==NULL)head=create();
do
{
printf("\n 欢迎使用 ADDRESS BOOK");
printf("\n 1.显示所有信息 2.删除一个信息");
printf("\n 3.寻找一个信息 4.插入一个信息");
printf("\n 5.修改一个信息 0.保存和退出");
printf("\n 输入你的选择(0-5):");
scanf("%d",&sel);
switch(sel)
{
case 1:show(head);
printf("请输入任何键返回 \n");
getch();
system("CLS");
break;
case 2:deleted(head);
break;
case 3:find(head);
break;
printf("请输入任何键返回 \n");
getchar();
break;
case 4:input(head);
break;
case 5:alter(head);
break;
case 0:save(head);return;
}
}
while(1);
getch();
}
总结:
1.编程过程要不断调试,才能发现错误
2.这个程序核心是用链表知识和文件知识
我要赚赏金
