//设计哈希表实现电话号码查询系统
#include <iostream.h>
//nclude "string.h"
#include "fstream" //fstram=ifstream+ofstream 文件的输入输出
#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<<"输入姓名:"<<endl;
cin>>temp->name;
cout<<"输入地址:"<<endl;
cin>>temp->address;
cout<<"输入电话:"<<endl;
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() //新建电话号码
{
phone=new pnode[20]; //动态创建对象数组
for(int i=0;i<20;i++)
{
phone[i]=new node;
phone[i]->next=NULL;
}
}
void create2() //新建用户名
{
nam=new mingzi[20]; //动态创建对象数组
for(int i=0;i<20;i++)
{
nam[i]=new node;
nam[i]->next=NULL;
}
}
void list() //根据电话号显示列表
{
node *p;
for(int i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}
void list2() //根据用户名显列表
{
node *p;
for(int i=0;i<20;i++)
{
p=nam[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}
void find(char num[11]) //根据电话号码查找用户信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)//比较num和q->num
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}
void find2(char name[8]) //根据用户名查找用户信息
{
hash2(name);
node *q=nam[key2]->next;
while(q!= NULL)
{
if(strcmp(name,q->name)==0) //比较name和q>name
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}
void save() //保存用户信息
{
node *p;
for(int i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
fstream iiout("out.txt", ios::out); //可以向该文件输出数据
iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;
p=p->next;
}
}
}
void menu() //菜单函数
{
cout<<"******用哈希表实现电话号码查询系统******"<<endl<<endl;
cout<<"*********欢迎进入本系统*****************"<<endl<<endl;
cout<<"1.添加记录"<<endl;
cout<<"2.查找记录"<<endl;
cout<<"3.姓名散列"<<endl;
cout<<"4.号码散列"<<endl;
cout<<"5.清空记录"<<endl;
cout<<"6.保存记录"<<endl;
cout<<"7.退出系统"<<endl;
cout<<"*******高加林、吴雷、田二超制作***********"<<endl<<endl;
cout<<"**********谢谢光临本系统******************"<<endl<<endl;
}
int main() //主函数
{
char num[11];
char name[8];
create();
create2() ;
int sel;
while(1)
{
menu();
cin>>sel;
if(sel==2)
{ cout<<"9号码查询,8姓名查询"<<endl;
int b;
cin>>b;
if(b==9)
{ cout<<"请输入电话号码:"<<endl;
cin >>num;
cout<<"输出查找的信息:"<<endl;
find(num);
}
else
{ cout<<"请输入姓名:"<<endl;
cin >>name;
cout<<"输出查找的信息:"<<endl;
find2(name);}
}
if(sel==3)
{ cout<<"姓名散列结果:"<<endl;
list2();
}
if(sel==1)
{ cout<<"请输入要添加的内容:"<<endl;
apend();
}
if(sel==4)
{ cout<<"号码散列结果:"<<endl;
list();
}
if(sel==5)
{ cout<<"列表已清空:"<<endl;
create();
create2();
}
if(sel==6)
{ cout<<"记录已保存:"<<endl;
save();
}
if(sel==7) return 0;
}
return 0;
}