哈希表实现电话号码查询系统

发布时间:2019-05-17 15:33:04   来源:文档文库   
字号:

设计性综合性实验

实验课题名称:

院系:计算机科学与技术学院 专业:计算机科学与技术

课程: 数据结构 教师:

学号: 姓名: (组长)

学号: 姓名:

学号: 姓名:

学号: 姓名:

学号: 姓名:

学号: 姓名:

学年度 学期

实验名称:哈希表实现电话号码查询系统 

实验性质:  设计性(*) 综合性(*

实验器材:PC机并装有VC++6.0环境

实验目的:利用《数据结构》课程的相关知识完成一个具有一定难度的综合设计题目,利用C/C++语言进行程序设计,并规范地完成课程设计报告。通过课程设计,巩固和加深对线性表、栈、队列、字符串、树、图、查找、排序等理论知识的理解;掌握现实复杂问题的分析建模和解决方法(包括问题描述、系统分析、设计建模、代码实现、结果分析等);提高利用计算机分析解决综合性实际问题的基本能力。

一、目的 

利用《数据结构》课程的相关知识完成一个具有一定难度的综合设计题目,利用C/C++语言进行程序设计,并规范地完成课程设计报告。通过课程设计,巩固和加深对线性表、栈、队列、字符串、树、图、查找、排序等理论知识的理解;掌握现实复杂问题的分析建模和解决方法(包括问题描述、系统分析、设计建模、代码实现、结果分析等);提高利用计算机分析解决综合性实际问题的基本能力。 

二、需求分析 

1 程序的功能 

1.1录入姓名、电话、住址

1.2查找功能

1.3散列方式

1.4清空记录

1.5保存成TXT格式

1.6退出

3、程序设计

3.1定义结构体

struct node //建节点

{

char name[8],address[20];

char num[11];

node * next;

};

3.1函数列表和功能说明

不同的函数来比较冲突

void hash(char num[11]) //哈希函数

void hash2(char name[8]) //哈希函数

不同的散列方式

void find(char num[11]) //查找用户信息

void find2(char name[8]) //查找用户信息

void save() //保存用户信息

void menu() //菜单

int main()

4、 输出形式

4.1数据文件“old.txt”存放原始电话号码数据。

4.2查找到相关信息时显示姓名、地址、电话号码。

5、调试结果

调试界面图如下

录入的联系人姓名、地址、联系电话截图如下

程序如下已通过上机调试

//#include "iostream.h"

#include

#include "string.h"

#include "fstream"

#define NULL 0

unsigned int key;

unsigned int key2;

int *p;

struct node //建节点

{

char name[8],address[20];

char num[11];

node * next;

};

typedef node* pnode;

typedef node* mingzi;

node **phone;

node **nam;

node *a;

using namespace std; //使用名称空间

void hash(char num[11]) //哈希函数

{

int i = 3;

key=(int)num[2];

while(num[i]!=NULL)

{

key+=(int)num[i];

i++;

}

key=key%20;

}

void hash2(char name[8]) //哈希函数

{

int i = 1;

key2=(int)name[0];

while(name[i]!=NULL)

{

key2+=(int)name[i];

i++;

}

key2=key2%20;

}

node* input() //输入节点

{

node *temp;

temp = new node;

temp->next=NULL;

cout<<"输入姓名:"<

cin>>temp->name;

cout<<"输入地址:"<

cin>>temp->address;

cout<<"输入电话:"<

cin>>temp->num;

return temp;

}

int apend() //添加节点

{

node *newphone;

node *newname;

newphone=input();

newname=newphone;

newphone->next=NULL;

newname->next=NULL;

hash(newphone->num);

hash2(newname->name);

newphone->next = phone[key]->next;

phone[key]->next=newphone;

newname->next = nam[key2]->next;

nam[key2]->next=newname;

return 0;

}

void create() //新建节点

{

int i;

phone=new pnode[20];

for(i=0;i<20;i++)

{

phone[i]=new node;

phone[i]->next=NULL;

}

}

void create2() //新建节点

{

int i;

nam=new mingzi[20];

for(i=0;i<20;i++)

{

nam[i]=new node;

nam[i]->next=NULL;

}

}

void list() //显示列表

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=phone[i]->next;

while(p)

{

cout<name<<'_'<address<<'_'<num<

p=p->next;

}

}

}

void list2() //显示列表

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=nam[i]->next;

while(p)

{

cout<name<<'_'<address<<'_'<num<

p=p->next;

}

}

}

void find(char num[11]) //查找用户信息

{

hash(num);

node *q=phone[key]->next;

while(q!= NULL)

{

if(strcmp(num,q->num)==0)

break;

q=q->next;

}

if(q)

cout<name<<"_" <address<<"_"<num<

else cout<<"无此记录"<

}

void find2(char name[8]) //查找用户信息

{

hash2(name);

node *q=nam[key2]->next;

while(q!= NULL)

{

if(strcmp(name,q->name)==0)

break;

q=q->next;

}

if(q)

cout<name<<"_" <address<<"_"<num<

else cout<<"无此记录"<

}

void save() //保存用户信息

{

int i;

node *p;

for(i=0;i<20;i++)

{

p=phone[i]->next;

while(p)

{

fstream iiout("out.txt", ios::out);

iiout<name<<"_"<address<<"_"<num<

p=p->next;

}

}

}

void menu() //菜单

{ cout<<"\t\t"<

cout<<"\t\t"<

cout<<"\t\t"<

cout<<"**************个人电话号码查询系统*******************\t\t"<

cout<<"\t\t0.请添加联系人记录!"<

cout<<"\t\t1.查找联系人记录"<

cout<<"\t\t2.联系人姓名散列"<

cout<<"\t\t3.联系人号码散列"<

cout<<"\t\t4.清空联系人记录"<

cout<<"\t\t5.保存到指定文件夹"<

cout<<"\t\t6.退出系统"<

cout<<"*****************************************************\t\t"<

}

int main()

{

char num[11];

char name[8];

create();

create2() ;

int sel;

while(1)

{

menu();

cin>>sel;

if(sel==1)

{ cout<<"7按号码查询,8按姓名查询"<

int b;

cin>>b;

if(b==7)

{ cout<<"请输入电话号码:"<

cin >>num;

cout<<"输出查找的信息:"<

find(num);

}

else

{ cout<<"请输入姓名:"<

cin >>name;

cout<<"输出查找的信息:"<

find2(name);}

}

if(sel==2)

{ cout<<"姓名散列结果:"<

list2();

}

if(sel==0)

{ cout<<"请输入要添加的内容:"<

apend();

}

if(sel==3)

{ cout<<"号码散列结果:"<

list();

}

if(sel==4)

{ cout<<"列表已清空:"<

create();

create2();

}

if(sel==5)

{ cout<<"通信录已保存:"<

save();

}

if(sel==6) return 0;

}

return 0;

}

实验成绩

课题小组自评分数

最后得分

本文来源:https://www.2haoxitong.net/k/doc/ba113934294ac850ad02de80d4d8d15abf23003a.html

《哈希表实现电话号码查询系统.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式