数据结构课程设计通讯录查询系统的设计与实现.docx
《数据结构课程设计通讯录查询系统的设计与实现.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计通讯录查询系统的设计与实现.docx(17页珍藏版)》请在冰豆网上搜索。
数据结构课程设计通讯录查询系统的设计与实现
通讯录查询系统的设计与实现
一、需求分析
1、问题描述
为某个单位建立一个员工通讯录管理系统,可以方便查询每一个员工的电话与地址。
设计散列表存储,设计并实现通讯录查找系统。
2、基本要求
a.每个记录有下列数据项:
电话号码、用户名、地址;
b.从键盘输入各记录,分别以电话号码为关键字建立散列表;
c.采用二次探测再散列法解决冲突;
d.查找并显示给定电话号码的记录;
e.通讯录信息文件保存。
二、概要设计
1.数据结构
本程序需要用到两个结构体,分别为通讯录message以及哈希表HxList
2.程序模块
本程序包含两个模块,一个是实现功能的函数的模块,另一个是主函数模块。
系统子程序及功能设计
本系统共有三个子程序,分别是:
intHx(longlongkey,intdata)//哈希函数
voidBulidHx(HxList&L)//建立通讯录
intSearch(HxList&L)//查找
3.各模块之间的调用关系以及算法设计
主函数调用BulidHx以及Search函数。
函数BulidHx调用函数Hx。
三、详细设计
1.数据类型定义
typedefstruct
{
char*name;
char*add;
longlongphonenumber;
}message;
typedefstruct
{
message*list;
intnumber;//记录数
}HxList;
2.系统主要子程序详细设计
a.建立通讯录
voidBulidHx(HxList&L)//建立通讯录
{
FILE*f=fopen("E:
\\tongxunlu.txt","w");
charbuf[20]={0},str[20]={0};
longlongkey;
cout<<"输入要建立的记录数:
";
cin>>L.number;
L.number+=1;
L.list=newmessage[L.number];//分配哈希表的存储空间
for(inti=0;i{
L.list[i].phonenumber=-1;
}
L.list[L.number-1].name=NULL;
L.list[L.number-1].add=NULL;
cout<<"输入记录信息(电话号码用户名地址)"<for(inti=0;i{
cin>>key>>buf>>str;
intpose=Hx(key,L.number);//获取理论上的存储位置
if(L.list[pose].phonenumber==-1)
{
}
else
{
//用二次探测再散列法解决冲突
//1^2-1^22^2-2^2
intdi,count=1;
xunhuan:
if(count%2==0)
di=-(count/2)*(count/2);
else
di=((count/2)+1)*((count/2)+1);
intsite=Hx(key+di,L.number);
if(site>=0)
{
if(L.list[site].phonenumber==-1)
{
pose=site;
}
else
{
count++;
gotoxunhuan;
}
}
else
{
site=L.number-abs(site);
if(L.list[site].phonenumber==-1)
{
pose=site;
}
else
{
count++;
gotoxunhuan;
}
}
}
L.list[pose].phonenumber=key;
fprintf(f,"%lld",key);
fprintf(f,"");
L.list[pose].name=newchar[strlen(buf)+1];
strcpy(L.list[pose].name,buf);
fprintf(f,"%s",buf);
fprintf(f,"");
L.list[pose].add=newchar[strlen(str)+1];
strcpy(L.list[pose].add,str);
fprintf(f,"%s",str);
fprintf(f,"\n");
}
}
b.查找
intSearch(HxList&L)//查找
{
longlongkey;
cout<<"输入要查找记录的关键字(电话号码):
";
cin>>key;
intpose=Hx(key,L.number);//计算理论上的位置
if(L.list[pose].phonenumber==key)
{
}
else
{
intcount=1,di;//二次探测再散列,查找
xunhuan:
if(count%2==0)
{
di=-(count/2)*(count/2);
}
else
{
di=((count/2)+1)*((count/2)+1);
}
intsite=Hx(key+di,L.number);
if(site>=0)
{
if(L.list[site].phonenumber==key)
{
pose=site;
}
else
{
count++;
if(L.list[site].phonenumber==-1)
{
cout<<"没有找到"<return-1;//没有找到
}
gotoxunhuan;
}
}
else
{
site=L.number-abs(site);
if(L.list[site].phonenumber==key)
{
pose=site;
}
else
{
count++;
if(L.list[site].phonenumber==-1)
{
cout<<"没有找到"<return-1;//没有找到
}
gotoxunhuan;
}
}
}
if(L.list[pose].phonenumber==key)
{
cout<<"电话号码\t"<<"用户名\t"<<"地址"<cout<returnpose;
}
}
四、测试与分析
1.显示主菜单,运行程序可以显示出如下界面。
2.建立通讯录
在主菜单下选1,建立通讯录,按照规定格式输入电话号码姓名以及地址,即可在E盘根目录下建立tongxunlu.txt文件,保存通讯录信息。
3.查找
在主菜单下选2,完成查找功能,输入要查找的电话号码,即可得到该电话号码对应的姓名以及地址。
4.退出系统
在主菜单下选3,退出程序。
五、附录
1.通讯查询系统.h
#include
usingnamespacestd;
typedefstruct
{
char*name;
char*add;
longlongphonenumber;
}message;
typedefstruct
{
message*list;
intnumber;//记录数
}HxList;
intHx(longlongkey,intdata)
{
returnkey%(data-1);
}
voidBulidHx(HxList&L)//建立通讯录
{
FILE*f=fopen("E:
\\tongxunlu.txt","w");
charbuf[20]={0},str[20]={0};
longlongkey;
cout<<"输入要建立的记录数:
";
cin>>L.number;
L.number+=1;
L.list=newmessage[L.number];//分配哈希表的存储空间
for(inti=0;i{
L.list[i].phonenumber=-1;
}
L.list[L.number-1].name=NULL;
L.list[L.number-1].add=NULL;
cout<<"输入记录信息(电话号码用户名地址)"<for(inti=0;i{
cin>>key>>buf>>str;
intpose=Hx(key,L.number);//获取理论上的存储位置
if(L.list[pose].phonenumber==-1)
{
}
else
{
//用二次探测再散列法解决冲突
//1^2-1^22^2-2^2
intdi,count=1;
xunhuan:
if(count%2==0)
di=-(count/2)*(count/2);
else
di=((count/2)+1)*((count/2)+1);
intsite=Hx(key+di,L.number);
if(site>=0)
{
if(L.list[site].phonenumber==-1)
{
pose=site;
}
else
{
count++;
gotoxunhuan;
}
}
else
{
site=L.number-abs(site);
if(L.list[site].phonenumber==-1)
{
pose=site;
}
else
{
count++;
gotoxunhuan;
}
}
}
L.list[pose].phonenumber=key;
fprintf(f,"%lld",key);
fprintf(f,"");
L.list[pose].name=newchar[strlen(buf)+1];
strcpy(L.list[pose].name,buf);
fprintf(f,"%s",buf);
fprintf(f,"");
L.list[pose].add=newchar[strlen(str)+1];
strcpy(L.list[pose].add,str);
fprintf(f,"%s",str);
fprintf(f,"\n");
}
}
intSearch(HxList&L)//查找
{
longlongkey;
cout<<"输入要查找记录的关键字(电话号码):
";
cin>>key;
intpose=Hx(key,L.number);//计算理论上的位置
if(L.list[pose].phonenumber==key)
{
}
else
{
intcount=1,di;//二次探测再散列,查找
xunhuan:
if(count%2==0)
{
di=-(count/2)*(count/2);
}
else
{
di=((count/2)+1)*((count/2)+1);
}
intsite=Hx(key+di,L.number);
if(site>=0)
{
if(L.list[site].phonenumber==key)
{
pose=site;
}
else
{
count++;
if(L.list[site].phonenumber==-1)
{
cout<<"没有找到"<return-1;//没有找到
}
gotoxunhuan;
}
}
else
{
site=L.number-abs(site);
if(L.list[site].phonenumber==key)
{
pose=site;
}
else
{
count++;
if(L.list[site].phonenumber==-1)
{
cout<<"没有找到"<return-1;//没有找到
}
gotoxunhuan;
}
}
}
if(L.list[pose].phonenumber==key)
{
cout<<"电话号码\t"<<"用户名\t"<<"地址"<cout<returnpose;
}
}
2.通讯录查询系统.cpp(主函数)
#include"通讯录查询系统.h"
intmain()
{
HxListL;
intch;
while
(1)
{
cout<<"----------------通讯录查询系统----------------"<cout<<"1.建立通讯录"<cout<<"2.查找"<cout<<"3.退出"<cout<<"请输入功能编号:
"<cin>>ch;
if(ch>3||ch<1)
{
do{
cout<<"输入有误,请重新输入!
"<cin>>ch;
}while(ch<=3&&ch>=1);
}
switch(ch)
{
case1:
{
cout<<"建立通讯录"<BulidHx(L);
break;
}
case2:
{
cout<<"显示通讯录"<Search(L);
break;
}
case3:
exit(0);
default:
break;
}
}
return0;
}六、用户手册
运行通讯录查询系统.exe,即可运行该通讯录查询系统并完成相应的功能。