#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]
黄舟2017-04-17 13:10:23
編譯器問題,MinGW下把strcpy_s 改成strcpy,似乎strcpy_s 是VS下的
--更新--
void QueryByName(char *name),無論是否查詢成功都會輸出一個NULL,解決方法,第一種在函數內部設立bool型的變數做標誌,第二種,找到後直接return
其次有一個問題,第二個選項,你添加紀錄的時候,編碼用的是hashname編碼(也就是你Name,Tel,Add全部都是放在了以Name做索引的那個空間),而你確實在QueryByTel 裡面用hashtel 解碼,對應不來啊。
--12.5更新--
你說的改法也可以,但我建議這樣
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;
return;
}
p = p->next;
}
cout << "NULL" << endl;
}
或
bool QueryByName(char *name,Node *&result)//按姓名查找
{
int key1 = hashname(name);
Node *p = hashtable1[key1];
while (p != NULL)
{
if (strcmp(name, p->user.name)==0);
{
result = p;
return true;
}
p = p->next;
}
return false;
}
然後再在主函數裡用if判斷,是否輸出NULL還是reult的成員值
至於你說說建立了兩個哈希表的應該不會混亂的問題
你的確是建立了兩個哈希表,可是你在QueryByTel(char tel)*裡面是這麼寫的
Node *p = hashtable1[key2];
你造訪的還是第一個用名字建立索引的雜湊表,應該是
Node *p = hashtable2[key2];
還有在add_record(char name, char tel,char add )裡面
int key2 = hashname(tel);
照你的意思應該是
int key2 = hashntel(tel);
如果覺得有幫助,希望你能點一個贊,新人需要點聲望,謝謝。