#include<iostream>
#include<string.h>
#define max 40
using namespace std;
struct User/*电话薄结构*/
{
char name[8];
char tel[11];
char add[20];
User *next;
}user;
struct Node/*哈希表结构*/
{
User user;
Node *next;
};
int user_num;
Node *hashtable1[max];
Node *hashtable2[max];
int hashname(char *name)//按名字建立哈希表
{
int sum = 0;
for (int i = 0; i < 8; i++)
{
sum = sum + name[i];
}
return sum % 17;
}
int hashtel(char *tel)//按号码建立哈希表
{
int sum = 0;
for (int i = 0; i < 11; i++)
{
sum = sum + tel[i];
}
return sum % 17;
}
void add_record(char *name, char *tel,char *add )//添加一个记录
{
int key1 = hashname(name);
Node *node1 = new Node;
strcpy_s(node1->user.name, name);
strcpy_s(node1->user.tel, tel);
strcpy_s(node1->user.add, add);
node1->next = hashtable1[key1];
hashtable1[key1] = node1;
int key2 = hashname(tel);
Node *node2 = new Node;
strcpy_s(node2->user.name, name);
strcpy_s(node2->user.tel, tel);
strcpy_s(node2->user.add, add);
node2->next = hashtable2[key2];
hashtable2[key2] = node2;
}
void QueryByName(char *name)//按姓名查找
{
int key1 = hashname(name);
Node *p = hashtable1[key1];
while (p != NULL)
{
if (strcmp(name, p->user.name)==0);
{
cout << p->user.name << "_" << p->user.add << "_" << p->user.tel << endl;
break;
}
p = p->next;
}
cout << "NULL" << endl;
}
void QueryByTel(char *tel)//按电话号码查找
{
int key2 = hashtel(tel);
Node *p = hashtable1[key2];
while (p != NULL)
{
if (strcmp(tel, p->user.tel) == 0);
{
cout << p->user.name << "_" << p->user.add << "_" << p->user.tel << endl;
break;
}
p = p->next;
}
cout << "NULL" << endl;
}
int main()
{
int opt;
char name[8], tel[11], add[20];
memset(hashtable1, 0, sizeof(hashtable1));
memset(hashtable2, 0, sizeof(hashtable2));
cout << "系统初始化:请输入用户的个数" << endl;
cin >> user_num;
cout << "请输入用户的姓名、地址、电话号码:" << endl;
for (int i = 0; i < user_num; i++)
{
cin >> name >> add >> tel;
add_record(name, tel, add);
}
cout << "系统初始化完毕!" << endl;
while (1)
{
cout << "请选择要进行的操作:" << endl;
cout << "0:增加一条记录" << endl;
cout << "1:根据输入的姓名搜索记录并输出" << endl;
cout << "2:根据输入的电话搜索记录并输出" << endl;
cout << "3:根据姓名查找表输出全部记录" << endl;
cout << "4:根据电话查找表输出全部记录" << endl;
cout << "5:退出" << endl;
cin >> opt;
switch (opt)
{
case 0:
cin >> name >> add >> tel;
add_record(name, tel, add);
break;
case 1:
cin >> name;
QueryByName(name);
break;
case 2:
cin >> tel;
QueryByTel(tel);
break;
case 3:
{
int key1 = 0;
Node *p = hashtable1[key1];
while (p != NULL)
{
cout << p->user.name << "_" << p->user.add << "_" << p->user.tel << endl;
p = p->next;
}
break;
}
case 4:
{
int key2 = 0;
Node *p2 = hashtable2[key2];
while (p2 != NULL)
{
cout << p2->user.name << "_" << p2->user.add << "_" << p2->user.tel << endl;
p2 = p2->next;
}
break;
}
case 5:
break;
default:
cout << "请输入0-5之间的数字!" << endl;
break;
}
}
return 0;
}
![图片描述][1]
Compiler problem, strcpy_s is changed to strcpy under MinGW. It seems that strcpy_s is under VS.
after finding it.--Update--
void QueryByName(char *name), whether the query is successful or not, a NULL will be output. Solved Method, the first method is to set up a bool type variable inside the function as a mark, the second method is to directly return
Secondly, there is a problem. In the second option, when you add a record, the encoding uses hashname encoding (that is, your Name, Tel, and Add are all placed in the space indexed by Name), and You did use hashtel for decoding in QueryByTel, but it doesn't work.
--12.5 update--
The modification you mentioned is also possible, but I suggest this
or
}
Then use if in the main function to determine whether to output NULL or the member value of result
As for your problem of creating two hash tables, there should be no confusion.
You did create two hash tables, but you did it in QueryByTel(char tel)* This is what it says
You are still accessing the first hash table indexed by name, which should be
Also in add_record(char name, char tel, char add )
According to what you mean, it should be
If you find it helpful, I hope you can give it a like. Newbies need some reputation, thank you.
Can you give me a full version? Give me an email: 540746963@qq.com Thank you