数据结构通讯录系统源代码.docx
《数据结构通讯录系统源代码.docx》由会员分享,可在线阅读,更多相关《数据结构通讯录系统源代码.docx(20页珍藏版)》请在冰点文库上搜索。
数据结构通讯录系统源代码
头文件:
/*单链表中每个结点的联系人结构体定义*/
typedefstructNode/*每个联系人结构体*/
{
charname[10];//姓名
chartelnum[20];//电话号码
charaddress[256];//地址
structNode*next;
}SLNode;
voidInitiate(SLNode**head)/*初始化单链表*/
if((*head=(SLNode*)malloc(sizeof(SLNode)))==NULL)
printf("内存空间不足!
\n");
exit
(1);
}
(*head)->next=NULL;/*置链尾标记NULL*/
intLength(SLNode*head)//求当前通讯录中的联系人数
SLNode*p;/*p指向头结点*/
intsize;/*size初始值为0*/
p=head->next;
size=0;
while(p!
=NULL&&head!
=NULL)/*循环计数*/
k=k->next;
size++;
returnsize;/*返回其大小*/
voidDataWrite(SLNode*head)
SLNode*p;
inti;
FILE*fp;
fp=fopen("Addresslist.dat","wb+");//打开文件,覆盖以前的文件
for(i=0;i{fwrite(p,sizeof(SLNode),1,fp);p=p->next;}fclose(fp);//关闭文件}voidLoad(SLNode*head)/*初始化*/{SLNode*q,*p,*m;FILE*fp;intcount=0;fp=fopen("Addresslist.dat","rb");//以读的方式打开二进制文件//rewind(fp);if(fp==NULL){fp=fopen("Addresslist.dat","wb+");if(fp!=NULL){printf("通讯录打开失败!创建新的通讯录!\n");}else{printf("通讯录打开失败!创建新的通讯录失败!\n");exit(0);}}else{q=(SLNode*)malloc(sizeof(SLNode));while(fread(q,sizeof(SLNode),1,fp)){//fread(q,sizeof(SLNode),1,fp);if(ferror(fp)){printf("error!\n");break;}p=head;m=(SLNode*)malloc(sizeof(SLNode));strcpy(m->name,q->name);strcpy(m->telnum,q->telnum);strcpy(m->address,q->address);count++;m->next=p->next;/*将新结点插入队头结点*/p->next=m;memset(q,'\0',sizeof(SLNode));}}fclose(fp);printf("\t\t\t\t通讯录总联系人数:%d\n",Length(head));}intPrintAll(SLNode*head)/*打印所有出所有联系人信息*/{inti;SLNode*q;q=head->next;if(q==NULL){printf("通讯录为空!\n");return-1;}else{for(i=0;i{printf("编号:%d\t姓名:%s\t号码:%s\t地址:%s\n",i+1,q->name,q->telnum,q->address);q=q->next;}return0;}}voidANParam(SLNode*head)/*在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//*插入成功则返回1,失败则返回0*/{SLNode*p,*q;q=(SLNode*)malloc(sizeof(SLNode));/*生成新结点由指针p指示*/printf("请输入新联系人姓名:");fflush(stdin);gets(q->name);printf("请输入新联系人的电话号码:");fflush(stdin);gets(q->telnum);printf("请输入新联系人的地址:");fflush(stdin);gets(q->address);p=head;q->next=p->next;/*将新结点插入队头结点*/p->next=q;DataWrite(head);//将信息以覆盖方式重新写入通讯录中printf("添加成功!\n");}voidAWParam(SLNode*head,chartelnum[11],charname[10],charaddress[256])//带形参的添加函数/*在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//*插入成功则返回1,失败则返回0*/{SLNode*p,*q;q=(SLNode*)malloc(sizeof(SLNode));/*生成新结点由指针p指示*/strcpy(q->name,name);strcpy(q->telnum,telnum);strcpy(q->address,address);p=head;q->next=p->next;/*将新结点插入队头结点*/p->next=q;DataWrite(head);printf("添加成功!\n");}voidDelete(SLNode*head)/*删除带头结点的单链表head的第i(1~n,n为结点个数)个结点*//*删除结点的元素有x带回,删除成功返回1,否则返回0*/{SLNode*p,*s;intchoice,i,m,ret;if(PrintAll(head))//如果通讯录为空,则无法删除,退出返回主菜单{return;}m=Length(head);//求当前通讯录人数printf("请输入你要删除联系人的前面编号:");ret=scanf("%d",&choice);while(!ret||choice>m||choice<1)//判断输入是否为数字,是否为,若否,则重新输入{printf("请正确输入你要删除联系人的前面编号!\n");fflush(stdin);ret=scanf("%d",&choice);}p=head;i=0;//由于我们打印出来时是由1开始,所以此处定义为0while(p->next!=NULL&&p->next->next!=NULL&&i<(choice-1))//查找所选择编号前一个结点{p=p->next;i++;}s=p->next;p->next=p->next->next;free(s);DataWrite(head);//将更新的数据重新写入文件中printf("删除成功!");}voidSBName(SLNode*head)/*查找函数,如果不存在,可添加*/{SLNode*p;intchoice;chartelnum[11],name[10],address[256];p=head->next;printf("请输入你要查找联系人的姓名:");fflush(stdin);gets(name);while(p!=NULL){if(!strcmp(p->name,name)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p==NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);//判断输入是否为数字,且数字范围是1~m之间,否则重新输入if(!choice){printf("该联系人姓名:%s\n",name);printf("请输入该联系人的电话:\n");fflush(stdin);gets(telnum);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}voidSBTell(SLNode*head)/*查找函数,如果不存在,可添加*/{SLNode*p;intchoice;chartelnum[20],name[10],address[256];p=head->next;printf("请输入你要查找联系人的号码:");fflush(stdin);gets(telnum);while(p!=NULL){if(!strcmp(p->telnum,telnum)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p==NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);if(!choice){printf("该联系人电话号码:%s\n",telnum);printf("请输入该联系人的姓名:\n");fflush(stdin);gets(name);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}voidDestroy(SLNode**head)/*因为单链表的结点空间是在程序运行中申请的,而系统只负责自动收回程序中静态分配的内存空间,所以在程序退出前释放动态申请的内存空间*/{SLNode*p,*p1;p=*head;while(p!=NULL)/*通过循环释放所有的动态开辟的内存空间*/{p1=p;p=p->next;free(p1);}*head=NULL;}voidAlter(SLNode*head){SLNode*p;intchoice,num,i,m;if(PrintAll(head))//通讯录为空无法进行修改,退出返回主菜单{return;}printf("请输入你要更改的信息人的前面编号:");fflush(stdin);m=Length(head);//获取当前通讯录人数i=scanf("%d",&num);//判断是否输入数字while(!i||num>m||num<1)//判断输入是否为数字,若否,则重新输入{printf("请正确输入你要更改的信息人的前面编号!\n");fflush(stdin);}p=head->next;//p指向第一个联系人for(i=0;i{if((i+1)==num){printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);lable1:printf("请输入你要更改的信息:\n");printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");m=scanf("%d",&choice);while(!m||choice>3||choice<0)//判断输入是否为数字,若否,则重新输入{printf("请正确输入选项!\n");fflush(stdin);m=scanf("%d",&choice);}switch(choice){case1:printf("请输入新姓名:");fflush(stdin);gets(p->name);break;case2:printf("请输入新号码:");fflush(stdin);gets(p->telnum);break;case3:printf("请输入新地址:");fflush(stdin);gets(p->address);break;default:printf("请输入新姓名:");fflush(stdin);gets(p->name);printf("请输入新号码:");fflush(stdin);gets(p->telnum);printf("请输入新地址:");fflush(stdin);gets(p->address);break;}printf("以下是刚更改的信息,请确认:");printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);printf("是否继续更改该联系人?\n");printf("继续修改请按0,否则保存修改!\n");printf("输入:");scanf("%d",&choice);if(!choice){gotolable1;}else{DataWrite(head);//将修改后的文件全部写入通讯录中}}p=p->next;}}intmenu(){intret,i;printf("\t\t******************通讯录******************\n\n");printf("\t\t1、查找联系人\t");printf("2、添加联系人\n\n");printf("\t\t3、显示所有联系人信息\t");printf("4、删除联系人\n\n");printf("\t\t5、更改联系人信息\t");printf("6、退出\n\n");printf("\t\t**************请选择相应功能**************\n");printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);while(!i||ret>6||ret<1)//判断输入是否为数字,若否,则重新输入{printf("\t\t请正确输入功能编号!\n");fflush(stdin);printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);}returnret;}voidDeleteAll(SLNode*head){FILE*fp;fp=fopen("Addresslist.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的Destroy(&head);Initiate(&head);//初始化单链表,创建新的头结点printf("清除成功!\n");}测试主函数:#include#include#include#include#include"LinkList.h"intmain(){intchoice=0,ret;SLNode*head;chars[500]="\t\t**谢谢使用**\n",a[]="\t\t华中农业大学楚天学院通讯录系统\n";system("title华中农业大学楚天学院通讯录系统");system("color30");while(choice{putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case1:system("cls");if(head->next==NULL){printf("通讯录为空!\n");break;}else{printf("请选择查找方式:");printf("1、姓名\t2、电话号码\t3、退出\n");ret=scanf("%d",&choice);while(!ret||choice>3||choice<1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret=scanf("%d",&choice);}switch(choice){case1:SBName(head);break;case2:SBTell(head);break;case3:break;default:break;}}break;case2:system("cls");ANParam(head);break;case3:system("cls");PrintAll(head);break;case4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret=scanf("%d",&choice);while(!ret||choice<1||choice>2){printf("输入错误!请正确选择删除方式!\n");ret=scanf("%d",&choice);}if(choice==1){DeleteAll(head);}elseif(choice==2){Delete(head);}break;case5:system("cls");Alter(head);break;case6:choice=0;while(choice{putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return0;}
fwrite(p,sizeof(SLNode),1,fp);
p=p->next;
fclose(fp);//关闭文件
voidLoad(SLNode*head)/*初始化*/
SLNode*q,*p,*m;
intcount=0;
fp=fopen("Addresslist.dat","rb");//以读的方式打开二进制文件
//rewind(fp);
if(fp==NULL)
fp=fopen("Addresslist.dat","wb+");
if(fp!
=NULL)
printf("通讯录打开失败!
创建新的通讯录!
else
创建新的通讯录失败!
exit(0);
q=(SLNode*)malloc(sizeof(SLNode));
while(fread(q,sizeof(SLNode),1,fp))
//fread(q,sizeof(SLNode),1,fp);
if(ferror(fp))
printf("error!
break;
p=head;
m=(SLNode*)malloc(sizeof(SLNode));
strcpy(m->name,q->name);
strcpy(m->telnum,q->telnum);
strcpy(m->address,q->address);
count++;
m->next=p->next;/*将新结点插入队头结点*/
p->next=m;
memset(q,'\0',sizeof(SLNode));
fclose(fp);
printf("\t\t\t\t通讯录总联系人数:
%d\n",Length(head));
intPrintAll(SLNode*head)/*打印所有出所有联系人信息*/
SLNode*q;
q=head->next;
if(q==NULL)
printf("通讯录为空!
return-1;
for(i=0;i{printf("编号:%d\t姓名:%s\t号码:%s\t地址:%s\n",i+1,q->name,q->telnum,q->address);q=q->next;}return0;}}voidANParam(SLNode*head)/*在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//*插入成功则返回1,失败则返回0*/{SLNode*p,*q;q=(SLNode*)malloc(sizeof(SLNode));/*生成新结点由指针p指示*/printf("请输入新联系人姓名:");fflush(stdin);gets(q->name);printf("请输入新联系人的电话号码:");fflush(stdin);gets(q->telnum);printf("请输入新联系人的地址:");fflush(stdin);gets(q->address);p=head;q->next=p->next;/*将新结点插入队头结点*/p->next=q;DataWrite(head);//将信息以覆盖方式重新写入通讯录中printf("添加成功!\n");}voidAWParam(SLNode*head,chartelnum[11],charname[10],charaddress[256])//带形参的添加函数/*在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//*插入成功则返回1,失败则返回0*/{SLNode*p,*q;q=(SLNode*)malloc(sizeof(SLNode));/*生成新结点由指针p指示*/strcpy(q->name,name);strcpy(q->telnum,telnum);strcpy(q->address,address);p=head;q->next=p->next;/*将新结点插入队头结点*/p->next=q;DataWrite(head);printf("添加成功!\n");}voidDelete(SLNode*head)/*删除带头结点的单链表head的第i(1~n,n为结点个数)个结点*//*删除结点的元素有x带回,删除成功返回1,否则返回0*/{SLNode*p,*s;intchoice,i,m,ret;if(PrintAll(head))//如果通讯录为空,则无法删除,退出返回主菜单{return;}m=Length(head);//求当前通讯录人数printf("请输入你要删除联系人的前面编号:");ret=scanf("%d",&choice);while(!ret||choice>m||choice<1)//判断输入是否为数字,是否为,若否,则重新输入{printf("请正确输入你要删除联系人的前面编号!\n");fflush(stdin);ret=scanf("%d",&choice);}p=head;i=0;//由于我们打印出来时是由1开始,所以此处定义为0while(p->next!=NULL&&p->next->next!=NULL&&i<(choice-1))//查找所选择编号前一个结点{p=p->next;i++;}s=p->next;p->next=p->next->next;free(s);DataWrite(head);//将更新的数据重新写入文件中printf("删除成功!");}voidSBName(SLNode*head)/*查找函数,如果不存在,可添加*/{SLNode*p;intchoice;chartelnum[11],name[10],address[256];p=head->next;printf("请输入你要查找联系人的姓名:");fflush(stdin);gets(name);while(p!=NULL){if(!strcmp(p->name,name)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p==NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);//判断输入是否为数字,且数字范围是1~m之间,否则重新输入if(!choice){printf("该联系人姓名:%s\n",name);printf("请输入该联系人的电话:\n");fflush(stdin);gets(telnum);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}voidSBTell(SLNode*head)/*查找函数,如果不存在,可添加*/{SLNode*p;intchoice;chartelnum[20],name[10],address[256];p=head->next;printf("请输入你要查找联系人的号码:");fflush(stdin);gets(telnum);while(p!=NULL){if(!strcmp(p->telnum,telnum)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p==NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);if(!choice){printf("该联系人电话号码:%s\n",telnum);printf("请输入该联系人的姓名:\n");fflush(stdin);gets(name);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}voidDestroy(SLNode**head)/*因为单链表的结点空间是在程序运行中申请的,而系统只负责自动收回程序中静态分配的内存空间,所以在程序退出前释放动态申请的内存空间*/{SLNode*p,*p1;p=*head;while(p!=NULL)/*通过循环释放所有的动态开辟的内存空间*/{p1=p;p=p->next;free(p1);}*head=NULL;}voidAlter(SLNode*head){SLNode*p;intchoice,num,i,m;if(PrintAll(head))//通讯录为空无法进行修改,退出返回主菜单{return;}printf("请输入你要更改的信息人的前面编号:");fflush(stdin);m=Length(head);//获取当前通讯录人数i=scanf("%d",&num);//判断是否输入数字while(!i||num>m||num<1)//判断输入是否为数字,若否,则重新输入{printf("请正确输入你要更改的信息人的前面编号!\n");fflush(stdin);}p=head->next;//p指向第一个联系人for(i=0;i{if((i+1)==num){printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);lable1:printf("请输入你要更改的信息:\n");printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");m=scanf("%d",&choice);while(!m||choice>3||choice<0)//判断输入是否为数字,若否,则重新输入{printf("请正确输入选项!\n");fflush(stdin);m=scanf("%d",&choice);}switch(choice){case1:printf("请输入新姓名:");fflush(stdin);gets(p->name);break;case2:printf("请输入新号码:");fflush(stdin);gets(p->telnum);break;case3:printf("请输入新地址:");fflush(stdin);gets(p->address);break;default:printf("请输入新姓名:");fflush(stdin);gets(p->name);printf("请输入新号码:");fflush(stdin);gets(p->telnum);printf("请输入新地址:");fflush(stdin);gets(p->address);break;}printf("以下是刚更改的信息,请确认:");printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);printf("是否继续更改该联系人?\n");printf("继续修改请按0,否则保存修改!\n");printf("输入:");scanf("%d",&choice);if(!choice){gotolable1;}else{DataWrite(head);//将修改后的文件全部写入通讯录中}}p=p->next;}}intmenu(){intret,i;printf("\t\t******************通讯录******************\n\n");printf("\t\t1、查找联系人\t");printf("2、添加联系人\n\n");printf("\t\t3、显示所有联系人信息\t");printf("4、删除联系人\n\n");printf("\t\t5、更改联系人信息\t");printf("6、退出\n\n");printf("\t\t**************请选择相应功能**************\n");printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);while(!i||ret>6||ret<1)//判断输入是否为数字,若否,则重新输入{printf("\t\t请正确输入功能编号!\n");fflush(stdin);printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);}returnret;}voidDeleteAll(SLNode*head){FILE*fp;fp=fopen("Addresslist.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的Destroy(&head);Initiate(&head);//初始化单链表,创建新的头结点printf("清除成功!\n");}测试主函数:#include#include#include#include#include"LinkList.h"intmain(){intchoice=0,ret;SLNode*head;chars[500]="\t\t**谢谢使用**\n",a[]="\t\t华中农业大学楚天学院通讯录系统\n";system("title华中农业大学楚天学院通讯录系统");system("color30");while(choice{putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case1:system("cls");if(head->next==NULL){printf("通讯录为空!\n");break;}else{printf("请选择查找方式:");printf("1、姓名\t2、电话号码\t3、退出\n");ret=scanf("%d",&choice);while(!ret||choice>3||choice<1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret=scanf("%d",&choice);}switch(choice){case1:SBName(head);break;case2:SBTell(head);break;case3:break;default:break;}}break;case2:system("cls");ANParam(head);break;case3:system("cls");PrintAll(head);break;case4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret=scanf("%d",&choice);while(!ret||choice<1||choice>2){printf("输入错误!请正确选择删除方式!\n");ret=scanf("%d",&choice);}if(choice==1){DeleteAll(head);}elseif(choice==2){Delete(head);}break;case5:system("cls");Alter(head);break;case6:choice=0;while(choice{putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return0;}
printf("编号:
%d\t姓名:
%s\t号码:
%s\t地址:
%s\n",i+1,q->name,q->telnum,q->address);
q=q->next;
return0;
voidANParam(SLNode*head)
/*在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*/
/*插入成功则返回1,失败则返回0*/
SLNode*p,*q;
q=(SLNode*)malloc(sizeof(SLNode));/*生成新结点由指针p指示*/
printf("请输入新联系人姓名:
");
fflush(stdin);
gets(q->name);
printf("请输入新联系人的电话号码:
gets(q->telnum);
printf("请输入新联系人的地址:
gets(q->address);
q->next=p->next;/*将新结点插入队头结点*/
p->next=q;
DataWrite(head);//将信息以覆盖方式重新写入通讯录中
printf("添加成功!
voidAWParam(SLNode*head,chartelnum[11],charname[10],charaddress[256])//带形参的添加函数
strcpy(q->name,name);
strcpy(q->telnum,telnum);
strcpy(q->address,address);
DataWrite(head);
voidDelete(SLNode*head)/*删除带头结点的单链表head的第i(1~n,n为结点个数)个结点*/
/*删除结点的元素有x带回,删除成功返回1,否则返回0*/
SLNode*p,*s;
intchoice,i,m,ret;
if(PrintAll(head))//如果通讯录为空,则无法删除,退出返回主菜单
return;
m=Length(head);//求当前通讯录人数
printf("请输入你要删除联系人的前面编号:
ret=scanf("%d",&choice);
while(!
ret||choice>m||choice<1)//判断输入是否为数字,是否为,若否,则重新输入
printf("请正确输入你要删除联系人的前面编号!
i=0;//由于我们打印出来时是由1开始,所以此处定义为0
while(p->next!
=NULL&&p->next->next!
=NULL&&i<(choice-1))//查找所选择编号前一个结点
i++;
s=p->next;
p->next=p->next->next;
free(s);
DataWrite(head);//将更新的数据重新写入文件中
printf("删除成功!
voidSBName(SLNode*head)/*查找函数,如果不存在,可添加*/
intchoice;
chartelnum[11],name[10],address[256];
printf("请输入你要查找联系人的姓名:
gets(name);
if(!
strcmp(p->name,name))
printf("姓名:
%s\t电话:
%s\n",p->name,p->telnum,p->address);
if(p==NULL)
printf("错误!
所查找联系人不在通讯录,是否将该联系人添加至通讯录?
printf("请按0确认添加,否则不添加\n");
scanf("%d",&choice);//判断输入是否为数字,且数字范围是1~m之间,否则重新输入
choice)
printf("该联系人姓名:
%s\n",name);
printf("请输入该联系人的电话:
gets(telnum);
printf("请输入该联系人的地址:
gets(address);
AWParam(head,telnum,name,address);
voidSBTell(SLNode*head)/*查找函数,如果不存在,可添加*/
chartelnum[20],name[10],address[256];
printf("请输入你要查找联系人的号码:
strcmp(p->telnum,telnum))
scanf("%d",&choice);
printf("该联系人电话号码:
%s\n",telnum);
printf("请输入该联系人的姓名:
voidDestroy(SLNode**head)
/*因为单链表的结点空间是在程序运行中申请的,而系统只负责自动收回程序中静态分配的内存空间,所以在程序退出前释放动态申请的内存空间*/
SLNode*p,*p1;
p=*head;
=NULL)/*通过循环释放所有的动态开辟的内存空间*/
p1=p;
free(p1);
*head=NULL;
voidAlter(SLNode*head)
intchoice,num,i,m;
if(PrintAll(head))//通讯录为空无法进行修改,退出返回主菜单
printf("请输入你要更改的信息人的前面编号:
m=Length(head);//获取当前通讯录人数
i=scanf("%d",&num);//判断是否输入数字
i||num>m||num<1)//判断输入是否为数字,若否,则重新输入
printf("请正确输入你要更改的信息人的前面编号!
p=head->next;//p指向第一个联系人
for(i=0;i{if((i+1)==num){printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);lable1:printf("请输入你要更改的信息:\n");printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");m=scanf("%d",&choice);while(!m||choice>3||choice<0)//判断输入是否为数字,若否,则重新输入{printf("请正确输入选项!\n");fflush(stdin);m=scanf("%d",&choice);}switch(choice){case1:printf("请输入新姓名:");fflush(stdin);gets(p->name);break;case2:printf("请输入新号码:");fflush(stdin);gets(p->telnum);break;case3:printf("请输入新地址:");fflush(stdin);gets(p->address);break;default:printf("请输入新姓名:");fflush(stdin);gets(p->name);printf("请输入新号码:");fflush(stdin);gets(p->telnum);printf("请输入新地址:");fflush(stdin);gets(p->address);break;}printf("以下是刚更改的信息,请确认:");printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);printf("是否继续更改该联系人?\n");printf("继续修改请按0,否则保存修改!\n");printf("输入:");scanf("%d",&choice);if(!choice){gotolable1;}else{DataWrite(head);//将修改后的文件全部写入通讯录中}}p=p->next;}}intmenu(){intret,i;printf("\t\t******************通讯录******************\n\n");printf("\t\t1、查找联系人\t");printf("2、添加联系人\n\n");printf("\t\t3、显示所有联系人信息\t");printf("4、删除联系人\n\n");printf("\t\t5、更改联系人信息\t");printf("6、退出\n\n");printf("\t\t**************请选择相应功能**************\n");printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);while(!i||ret>6||ret<1)//判断输入是否为数字,若否,则重新输入{printf("\t\t请正确输入功能编号!\n");fflush(stdin);printf("\t\t输入对应功能编号:");i=scanf("%d",&ret);}returnret;}voidDeleteAll(SLNode*head){FILE*fp;fp=fopen("Addresslist.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的Destroy(&head);Initiate(&head);//初始化单链表,创建新的头结点printf("清除成功!\n");}测试主函数:#include#include#include#include#include"LinkList.h"intmain(){intchoice=0,ret;SLNode*head;chars[500]="\t\t**谢谢使用**\n",a[]="\t\t华中农业大学楚天学院通讯录系统\n";system("title华中农业大学楚天学院通讯录系统");system("color30");while(choice{putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case1:system("cls");if(head->next==NULL){printf("通讯录为空!\n");break;}else{printf("请选择查找方式:");printf("1、姓名\t2、电话号码\t3、退出\n");ret=scanf("%d",&choice);while(!ret||choice>3||choice<1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret=scanf("%d",&choice);}switch(choice){case1:SBName(head);break;case2:SBTell(head);break;case3:break;default:break;}}break;case2:system("cls");ANParam(head);break;case3:system("cls");PrintAll(head);break;case4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret=scanf("%d",&choice);while(!ret||choice<1||choice>2){printf("输入错误!请正确选择删除方式!\n");ret=scanf("%d",&choice);}if(choice==1){DeleteAll(head);}elseif(choice==2){Delete(head);}break;case5:system("cls");Alter(head);break;case6:choice=0;while(choice{putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return0;}
if((i+1)==num)
lable1:
printf("请输入你要更改的信息:
printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");
m=scanf("%d",&choice);
m||choice>3||choice<0)//判断输入是否为数字,若否,则重新输入
printf("请正确输入选项!
switch(choice)
case1:
printf("请输入新姓名:
gets(p->name);
case2:
printf("请输入新号码:
gets(p->telnum);
case3:
printf("请输入新地址:
gets(p->address);
default:
printf("以下是刚更改的信息,请确认:
printf("是否继续更改该联系人?
printf("继续修改请按0,否则保存修改!
printf("输入:
gotolable1;
DataWrite(head);//将修改后的文件全部写入通讯录中
intmenu()
intret,i;
printf("\t\t******************通讯录******************\n\n");
printf("\t\t1、查找联系人\t");
printf("2、添加联系人\n\n");
printf("\t\t3、显示所有联系人信息\t");
printf("4、删除联系人\n\n");
printf("\t\t5、更改联系人信息\t");
printf("6、退出\n\n");
printf("\t\t**************请选择相应功能**************\n");
printf("\t\t输入对应功能编号:
i=scanf("%d",&ret);
i||ret>6||ret<1)//判断输入是否为数字,若否,则重新输入
printf("\t\t请正确输入功能编号!
returnret;
voidDeleteAll(SLNode*head)
fp=fopen("Addresslist.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的
Destroy(&head);
Initiate(&head);//初始化单链表,创建新的头结点
printf("清除成功!
测试主函数:
#include
#include"LinkList.h"
intmain()
intchoice=0,ret;
SLNode*head;
chars[500]="\t\t**谢谢使用**\n",a[]="\t\t华中农业大学楚天学院通讯录系统\n";
system("title华中农业大学楚天学院通讯录系统");
system("color30");
while(choice{putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case1:system("cls");if(head->next==NULL){printf("通讯录为空!\n");break;}else{printf("请选择查找方式:");printf("1、姓名\t2、电话号码\t3、退出\n");ret=scanf("%d",&choice);while(!ret||choice>3||choice<1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret=scanf("%d",&choice);}switch(choice){case1:SBName(head);break;case2:SBTell(head);break;case3:break;default:break;}}break;case2:system("cls");ANParam(head);break;case3:system("cls");PrintAll(head);break;case4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret=scanf("%d",&choice);while(!ret||choice<1||choice>2){printf("输入错误!请正确选择删除方式!\n");ret=scanf("%d",&choice);}if(choice==1){DeleteAll(head);}elseif(choice==2){Delete(head);}break;case5:system("cls");Alter(head);break;case6:choice=0;while(choice{putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return0;}
putchar(a[choice]);
Sleep(80);
choice++;
Initiate(&head);
Load(head);
while
(1)
switch(menu())
system("cls");
if(head->next==NULL)
printf("请选择查找方式:
printf("1、姓名\t2、电话号码\t3、退出\n");
ret||choice>3||choice<1)
printf("输入有误!
请正确选择查找方式!
SBName(head);
SBTell(head);
ANParam(head);
PrintAll(head);
case4:
printf("请选择删除方式:
printf("1、清空通讯录\t2、删除指定联系人\n");
ret||choice<1||choice>2)
printf("输入错误!
请正确选择删除方式!
if(choice==1)
DeleteAll(head);
elseif(choice==2)
Delete(head);
case5:
Alter(head);
case6:
choice=0;
while(choice{putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return0;}
putchar(s[choice]);
请重新输入!
Destroy(&head);//清除动态申请空间
system("pause");
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2