୧( ⁼̴̶̤̀ω⁼̴̶̤́ )૭507 2022-01-20 14:58 采纳率: 0%
浏览 16

int value=hash(phone)中hash isambiguous怎么解决

#include#includeusing namespace std;#define MAX 10000#define MOD 9873struct node //定义储存学生信息的结构体{ string phone; string name; string address; node *next;};node *infor_phone[MAX]; //存放信息的指针数组node infor_name[MAX];void init() //初始化初始化指针数组{ for(int i=0;i<MAX;i++) { infor_phone[i]=NULL; infor_name[i]=NULL; }}bool usersayyes() //帮助函数要求用户输入正确的选项{ string sig; bool continu=true; cout<<"请输入(N/n)或(Y/y),(N/n)代表退出,(Y/y)代表继续:"; while(cin>>sig&&(sig!="Y"&&sig!="y")&&(sig!="N"&&sig!="n")) cout<<"输入错误,请重新输入!"<<endl; return sig=="Y"||sig=="y";}//再哈希法int hash_agin(int numble,int key) //再哈希法获得地址的函数{ return numble%key;}int create(string key){ int result=1,cur=0,i; if(key.size()<=4) i=key.size()-1; else i=4; for(;i>=0;i--) { cur=key[i]-'0'; result=result9+cur; } return result;}void add_infors(string phone,string name,string address) //再希法添//加用户信息到哈希表{ int numble_phone=create(phone),key=MOD,pos_phone,pos_name; while(infor_phone[pos_phone=hash_agin(numble_phone,key)]!=NULL) key--; key=MOD; int numble_name=create(name); while(infor_name[pos_name=hash_agin(numble_name,key)]!=NULL) key--; node *inforphone=new node; node inforname=new node; inforphone->name=inforname->name=name; inforphone->phone=inforname->phone=phone; inforphone->address=inforname->address=address; inforphone->next=inforname->next=NULL; infor_phone[pos_phone]=inforphone; infor_name[pos_name]=inforname;}void find_byname(string name) //以名字为关键字查询用户信息{ int numble_name=create(name),key=MOD,pos_name; while(true) { pos_name=hash_agin(numble_name,key); if(infor_name[pos_name]==NULL) { cout<<"不存在你要查找的信息!"<<endl; cout<<"-------------------------------------------"<<endl; return; } if(infor_name[pos_name]->name==name) { cout<<"姓名:"; cout<<infor_name[pos_name]->name<<endl; cout<<"电话:"; cout<<infor_name[pos_name]->phone<<endl; cout<<"地址:"; cout<<infor_name[pos_name]->address<<endl; cout<<"-------------------------------------------"<<endl; return ; } key--; }}void find_byphone(string phone) //以电话为关键字查询用户信息{ int numble_phone=create(phone),key=MOD,pos_phone; while(true) { pos_phone=hash_agin(numble_phone,key); if(infor_phone[pos_phone]==NULL) { cout<<"不存在你要查找的信息!"<<endl; cout<<"-------------------------------------------"<<endl; return; } if(infor_phone[pos_phone]->phone==phone) { cout<<"姓名:"; cout<<infor_phone[pos_phone]->name<<endl; cout<<"电话:"; cout<<infor_phone[pos_phone]->phone<<endl; cout<<"地址:"; cout<<infor_phone[pos_phone]->address<<endl; cout<<"-------------------------------------------"<<endl; return ; } key--; }}void find(){ string sig,infor; cout<<"请输入(1)或(2),(1)代表姓名,(2)代表电话:"; while(cin>>sig&&(sig!="1"&&sig!="2")) cout<<"输入错误,请重新输入!"<<endl; cout<<"请输入要查找的信息:"; cin>>infor; if(sig=="1") find_byname(infor); else find_byphone(infor);}//链表法int hash(string key) //链表法获得哈希码{ int result=1,cur=0,i; if(key.size()<=4) i=key.size()-1; else i=4; for(;i>=0;i--) { cur=key[i]-'0'; result=result9+cur; } result%=(MOD); return result;}node * build_infor(string phone,string name,string address) { //存储用户信息到哈希表 node *information=new node; information->phone=phone; information->name=name; information->address=address; information->next=NULL; return information;}void add_infor_phone(string phone,string name,string address){ int value=hash(phone); node *infor=build_infor(phone,name,address); if(infor_phone[value]==NULL) infor_phone[value]=infor; else { node *cur=infor_phone[value]; while(cur->next) cur=cur->next; cur->next=infor; }}void add_infor_name(string phone,string name,string address){ int value=hash(name); node *infor=build_infor(phone,name,address); if(infor_name[value]==NULL) infor_name[value]=infor; else { node *cur=infor_name[value]; while(cur->next) cur=cur->next; cur->next=infor; }}void findinfor() //分别以名字和电话为关键字查询用户信息{ string infor; int sig,pos; cout<<"请输入要供查找的信息代号:1代表电话号码,2代表姓名:"; while(cin>>sig&&(sig!=1&&sig!=2)) cout<<"输入错误,请重新输入!"<<endl; cout<<"请输入要供查找的信息:"; cin>>infor; if(sig==1) { bool flag=true; pos=hash(infor); node * cur=infor_phone[pos]; while(cur) { if(cur->phone==infor) { cout<<"姓名:"<name<<endl; cout<<"电话:"<phone<<endl; cout<<"地址:"<address<<endl; flag=false; break; } cur=cur->next; } if(flag) cout<<"不存在要查找的记录!"<<endl; cout<<"-------------------------------------------"<<endl; } else if(sig==2) { bool flag=true; pos=hash(infor); node * cur=infor_name[pos]; while(cur) { if(cur->name==infor) { cout<<"姓名:"<name<<endl <<"电话:"<phone<<endl <<"地址:"<address<<endl; flag=false; break; } cur=cur->next; } if(flag) cout<<"不存在要查找的记录!"<<endl; cout<<"--------------------------------------"<<endl; } else { cout<<"输入错误,请重新输入:"<<endl; cout<<"--------------------------------------"<<endl; findinfor(); }}void hash_frist(){ int numble; string phone,name,address,sig; init(); cout<<"请输入添加的人数:"; cin>>numble; while(numble--) { cout<<"请输入----姓名:"; cin>>name; cout<<"请输入电话号码:"; cin>>phone; cout<<"请输入----地址:"; cin>>address; cout<<"--------------------------------------"<<endl; add_infor_phone(phone,name,address); add_infor_name(phone,name,address); } do findinfor(); while(usersayyes()); }void hash_second(){ int numble; string phone,name,address,sig,infor; init(); cout<<"请输入添加的人数:"; cin>>numble; while(numble--) { cout<<"请输入----姓名:"; cin>>name; cout<<"请输入电话号码:"; cin>>phone; cout<<"请输入----地址:"; cin>>address; cout<<"--------------------------------------"<<endl; add_infors(phone,name,address); } do find(); while(usersayyes());}int main() //主函数{ string chois; cout<<"请输入(1)或(2)--(1)链式法(2)再哈希:"; while(cin>>chois&&(chois!="1"&&chois!="2")) cout<<"输入错误,请重新输入!"<<endl; if(chois=="1") hash_frist(); if(chois=="2") hash_second(); return 0;}

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2022-01-20 15:04
    关注

    代码请发代码段格式。

    img

    评论

报告相同问题?

问题事件

  • 修改了问题 1月20日
  • 创建了问题 1月20日

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大