通讯录管理系1.docx
《通讯录管理系1.docx》由会员分享,可在线阅读,更多相关《通讯录管理系1.docx(27页珍藏版)》请在冰豆网上搜索。
通讯录管理系1
通讯录管理系统
1.引言
数据结构是一门理论性强、思维抽象、难度较大的课程,是基础课和专业课之间的桥梁。
该课程的先行课程是计算机基础、程序设计语言、离散数学等,后续课程有操作系统、编译原理、数据库原理、软件工程等。
通过本门课程的学习,我们应该能透彻地理解各种数据对象的特点,学会数据的组织方法和实现方法,并进一步培养良好的程序设计能力和解决实际问题的能力,而且该课程的研究方法对我们学生在校和离校后的学习和工作,也有着重要的意义。
2.系统设计
2.1.设计目的
本课程设计可加深对课堂理论学习的理解,增强动手能力,以培养学生合作的能力,为毕业设计作好实践环节上的准备。
通讯录系统是在学校常见的计算机信息管理系统。
它的主要任务是对学生信息进行管理,如学生信息的输入、查询、修改、增加、删除,迅速准确地完成各种学生信息的统计和查询。
2.2.设计内容
(1)数据包括:
姓名(name)街道(street)城市(city)邮编(eip)国家(state)。
(2)可删除记录。
(3)可显示所有保存的记录。
(4)可按人的姓名进行查询。
2.3.设计思路
通过visualc++(用的是C语言)编写的一个控制台程序,该程序通过链表的操作,文件存储来实现通讯录的基本功能。
structaddress{/*定义结构*/
charname[10];/*姓名*/
charstreet[50];/*街道*/
charcity[10];/*城市*/
charstate[15];/*国家*/
chareip[7];/*邮编*/
structaddress*next;/*后继指针*/
structaddress*prior;/*前驱指针*/
}
链表的插入,删除来实现通讯录里的内容的插入删除,当操作完成通过文件件来存储链表的信息,下次打开程序时,读取文件里的内容到内存中,放在链表,然后又可以对链表进行操作;
2.4.详细设计
1.主界面设计
通过switch语句调用各种函数,实现各种操作。
然后把switch嵌套到无限的for循环(for(;;))中,使完成每一步操作都回到到选择操作的主界面
函数之间的相互调用
voidmain()
{
start=last=NULL;
for(;;)/*无限循环*/
{
switch(menu_select())/*调用主界面的选择函数,带回返回值*/
{
case1:
enter();
continue;
case2:
ddelete(&start,&last);
continue;
case3:
list();
continue;
case4:
search();
continue;
case5:
save();
continue;
case6:
load();
continue;
case7:
exit(0);
}
}
}
intmenu_select(void)/*主目录*/
{
chars[80];
intc;
printf("………………^欢迎使用DOS通讯录系统^………………\n");
printf("************请在做其它操作前先导入*************\n");
printf("***********************************************\n");
printf("*****************1.输入信息******************\n");
printf("*****************2.删除信息******************\n");
printf("*****************3.显示信息******************\n");
printf("*****************4.查找******************\n");
printf("*****************5.存盘******************\n");
printf("*****************6.导入******************\n");
printf("*****************7.退出******************\n");
printf("***********************************************\n");
do{
printf("\nPleaseenteryourchoice:
\n");
gets(s);
c=atoi(s);/*将获取的字符串转换成整型*/
}while(c<0||c>7);
returnc;/*返回输入值*/
}
2.输入信息函数
structaddress*info;/*定义当前结点*/
for(;;)
{
info=(structaddress*)malloc(sizeof(structaddress));/*为当前结点分配空间*/
if(!
info)
{
printf("\nOutofmemory");
exit(0);/*如果分配空间失败,退出程序*/
}
printf("输入空姓名结束:
\n");
inputs("请输入姓名:
",info->name,10);
if(!
info->name[0])break;/*如果输入姓名为空,结束循环*/
inputs("请输入街道:
",info->street,50);
inputs("请输入城市:
",info->city,15);
inputs("请输入国家:
",info->state,15);
inputs("请输入邮编:
",info->eip,7);
insert(info,&start,&last);/*调用结点插入函数*/
}
输入函数调用到另外两个函数,inputs和insert,其中inputs中还用到fgets(str,n,fp),把键盘的输入信息传到字符串中
charp[255];
do
{
printf(prompt);
fgets(p,254,stdin);/*stdin,标准输入缓存,获取键盘输入信息*/
if(strlen(p)>count)
printf("\nTooLong\n");
}while(strlen(p)>count);
p[strlen(p)-1]=0;
strcpy(s,p);
insert是关键函数,每当输入完一条信息都会调用到insert函数,将信息插入到链表中
if(*last==NULL)/*如果尾结点为空,意味着当前链表为空*/
{/*则将该结点赋给头尾结点*/
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;
}
else/*如果链表不为空,则将信息插入到链表尾,作为尾结点*/
{
(*last)->next=i;
i->prior=*last;
i->next=NULL;
*last=(*last)->next;
}
NULL
NULL
3.删除·查找·显示函数
删除函数调用find函数,通过姓名,查找到该节点,然后删除该节点信息,这其中涉及到头尾节点,及其变化;
先判断是否为头结点,如果为头结点,则把原头结点的后继作为新的头结点
如果不为头结点,则该节点的前驱的next指向该节点的后继
如果该节点为尾结点,则让该节点的前驱作为新的尾结点
structaddress*info;
chars[80];
inputs("请输入姓名:
",s,10);
info=find(s);
if(info)
{
printf("Deleting......\n");
if(*start==info)
{
*start=info->next;
if(*start)
(*start)->prior=NULL;
else*last=NULL;
}
else
{
{info->prior->next=info->next;
if(info!
=*last)
info->next->prior=info->prior;
else
*last=info->prior;
}
free(info);
printf("-Ok,删除成功!
\n");
}
与删除相比,查找就简单的多,只需要调用find的函数,找到该节点记录并显示出来就行了,在search本身里面只要调整下输出的界面就行了
structaddress*find(char*name)/*查找函数,形参为欲查找结点的name域*/
{
structaddress*info;
info=start;
while(info)
{
if(!
strcmp(name,info->name))
returninfo;
info=info->next;
}
printf("Namenotfound.\n");
returnNULL;
}
输出函数更简单,直接输出链表即可
if(info==NULL)
printf("当前记录为空!
");
else
printf("姓名\t街道\t城市\t国家\t邮编\t\n");
while(info)
{
display(info);/*display为输出节点函数,一些列print组成*/
if(info->next==NULL)
{break;}info=info->next;
};
4.存储与导入
存储
存储时通过fopen打开文件(没有该文件时则创建)
fp=fopen("record.txt","wb");/*生成文件*/
if(!
fp)
{printf("Cannotopenfile.\n");return}
然后通过fwrite将链表信息写入文件
while(info)/*把链表写入文件*/
{fwrite(info,sizeof(structaddress),1,fp);
info=info->next;}
fwrite每次从info读取一个sizeof(structaddress)长度的数据,
写入fp文件中。
写入完毕后即关闭文件
fclose(fp);
导入
导入时先建立链表,为节点分配内存空间
然后打开文件,将文件的内容写入内存链表中
registerintt,size;
structaddress*info,*temp=0;
char*p;
FILE*fp;/*打开文件*/
if((fp=fopen("record.txt","r"))==NULL)
{
printf("Cannotopenfile!
\n");
return;
}
printf("\n\nLoading...\n");/*调用文件*/
size=sizeof(structaddress);/*为结点分配内存*/
start=(structaddress*)malloc(size);
if(!
start)/*如果读取失败,返回*/
{
printf("Outofmemory!
\n");
exit(0);
}
info=start;
p=(char*)info;
while((*p++=getc(fp))!
=EOF)
{
for(t=0;t*p++=getc(fp);
info->next=(structaddress*)malloc(size);
if(!
info->next)
{
printf("Outofmemory!
\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=(char*)info;
}
temp->next=0;
last=temp;
start->prior=0;
fclose(fp);
3.程序清单
#include
#include
#include
structaddress{/*定义结构*/
charname[10];
charstreet[50];
charcity[10];
charstate[15];
chareip[7];
structaddress*next;/*后继指针*/
structaddress*prior;/*前驱指针*/
};
structaddress*start;/*首结点*/
structaddress*last;/*尾结点*/
structaddress*find(char*);/*声明查找函数*/
voidenter();/*函数声明*/
voidsearch();
voidsave();
voidload();
voidlist();
voidddelete(structaddress**start,structaddress**last);
voidinsert(structaddress*i,structaddress**start,
structaddress**last);
voidinputs(char*,char*,int);
voiddisplay(structaddress*);
intmenu_select(void);
voidmain()
{
start=last=NULL;
for(;;)
{
switch(menu_select())
{
case1:
enter();
continue;
case2:
ddelete(&start,&last);
continue;
case3:
list();
continue;
case4:
search();
continue;
case5:
save();
continue;
case6:
load();
continue;
case7:
exit(0);
}
}
}
intmenu_select(void)/*主目录*/
{
chars[80];
intc;
printf("………………^欢迎使用DOS通讯录系统^………………\n");
printf("************请在做其它操作前先导入*************\n");
printf("***********************************************\n");
printf("*****************1.输入信息******************\n");
printf("*****************2.删除信息******************\n");
printf("*****************3.显示信息******************\n");
printf("*****************4.查找******************\n");
printf("*****************5.存盘******************\n");
printf("*****************6.导入******************\n");
printf("*****************7.退出******************\n");
printf("***********************************************\n");
do{
printf("\nPleaseenteryourchoice:
\n");
gets(s);
c=atoi(s);
}while(c<0||c>7);
returnc;/*返回输入值*/
}
voidenter()/*输入函数,本函数循环输入资料,当输入姓名为空时退出*/
{
structaddress*info;/*定义当前结点*/
for(;;)
{
info=(structaddress*)malloc(sizeof(structaddress));/*为当前结点分配空间*/
if(!
info)
{
printf("\nOutofmemory");
exit(0);/*如果分配空间失败,退出程序*/
}
printf("输入空姓名结束:
\n");
inputs("请输入姓名:
",info->name,10);
if(!
info->name[0])
break;/*如果输入姓名为空,结束循环*/
inputs("请输入街道:
",info->street,50);
inputs("请输入城市:
",info->city,15);
inputs("请输入国家:
",info->state,15);
inputs("请输入邮编:
",info->eip,7);
insert(info,&start,&last);/*调用结点插入函数*/
}
}
voidinputs(char*prompt,char*s,intcount)/*输入函数,有越界检测功能*/
{
charp[255];
do
{
printf(prompt);
fgets(p,254,stdin);
if(strlen(p)>count)
printf("\nTooLong\n");
}while(strlen(p)>count);
p[strlen(p)-1]=0;
strcpy(s,p);
}
voidinsert(/*数据插入函数*/
structaddress*i,
structaddress**start,
structaddress**last
)
{
if(*last==NULL)/*如果尾结点为空,意味着当前链表为空*/
{
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;
}
else
{
(*last)->next=i;
i->prior=*last;
i->next=NULL;
*last=(*last)->next;
}
}
voidddelete(structaddress**start,structaddress**last)/*删除函数*/
{
structaddress*info;
chars[80];
inputs("请输入姓名:
",s,10);/*输入欲删除结点的name域内容*/
info=find(s);/*查找该内容*/
if(info)/*如果找到*/
{
printf("Deleting......\n");
if(*start==info)/*如果该结点为首结点,把该结点的下驱作为新的首结点(入口)*/
{
*start=info->next;
if(*start)
(*start)->prior=NULL;
else*last=NULL;
}
else/*如果欲删除的结点不是首结点*/
{
info->prior->next=info->next;/*令该结点的前驱的next指针指向该结点的后驱,*又令该结点的后驱的prior指点指向该结点的前驱*/
if(info!
=*last)/*如果该结点是尾结点,则令该结点的前驱为尾结点*/
info->next->prior=info->prior;
else
*last=info->prior;
}
free(info);/*释放该结点所占用的内存*/
printf("-Ok,删除成功!
\n");
}
}
structaddress*find(char*name)/*查找函数,形参为欲查找结点的name域*/
{
structaddress*info;
info=start;
while(info)
{
if(!
strcmp(name,info->name))returninfo;
info=info->next;
}
printf("未找到相关信息.\n");
returnNULL;
}
/*输出整个链表*/
voidlist(void)
{
structaddress*info;
info=start;
if(info==NULL)
printf("当前记录为空!
");
elseprintf("姓名\t街道\t\t城市\t国家\t邮编\t\n");
while(info)
{
display(info);
if(info->next==NULL){break;}info=info->next;
};
printf("\n\n");
}
voiddisplay(structaddress*info)/*输出传入结点函数*/
{
printf("%s\t",info->name);
printf("%s\t",info->street);
printf("%s\t",info->city);
printf("%s\t",info->state);
printf("%s\t",info->eip);
printf("\n");
}
voidsearch(void)/*查找函数*/
{
charname[40];
structaddress*info;
printf("请输入要查找的姓名:
");/*输入欲查找的姓名*/
gets(name);
info=find(name);
if(!
info)
printf("姓名不存在\n");/*如果没找到,显示Notfound*/
else
display(info);/*如果找到,显示该结点资料*/
}
voidsave(void)/*保存函数*/
{
structaddress*info;
FILE*fp;
fp=fop