问题描述
- 做一个电话号码本,进行查找,添加等操作,不知道自己的代码哪里错了,求助~
-
#include
#include
#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;
}
编译过了,但不能继续运行下去。
解决方案
没严格测试:
#include<fstream>
#include <iostream>
using namespace std;
#define max 40
using namespace std;
struct User/*电话薄结构*/
{
char name[8];
char tel[11];
char add[20];
User *next; //cgl修改
}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;
opt=0;
while (opt!=5) //修改
{
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;
}
解决方案二:
#if 1
#include
#include
#define max 40
using namespace std;
****_//struct User/*电话薄结构*/
//{
// char name[8];
// char tel[11];
// char add[20];
// /*问题①:C语言基础不牢固,在结构题没有完整定义之前不能用此类型名声明对象,指针和引用可以, 可以看看c与指针这本书*/
// //User next;
//}user;
//这是我的代码
typedef struct _User
{
char name[8];
char tel[11];
char add[20];
struct _User *next;
}User;
//struct Node / 哈希表结构* /
//{
// User user;
// Node next;/问题②:编程规范很重要,此处尽量用typedef*/
//};
//这是我的代码
typedef struct _Node
{
User user;
struct _Node *next;
}Node;
****_
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); /问题③:此处的分号是有意的吗? /
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); /问题③:此处的分号是有意的吗? */
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;
}
#endif
解决方案三:
case输出能否进行呢?
具体是到哪一步执行不了?
解决方案四:
改了3个较明显的错误,你自己测试一下看看。
struct User/*电话薄结构*/
{
char name[8];
char tel[11];
char add[20];
User *next; //修改
}user;
if (strcmp(name, p->user.name)==0)//去掉一个;分号
{
cout << p->user.name << "_" << p->user.add << "_" << p->user.tel << endl;
break;
}
if (strcmp(tel, p->user.tel) == 0)//去掉一个;分号
{
cout << p->user.name << "_" << p->user.add << "_" << p->user.tel << endl;
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;
// }
解决方案八:
#if 1
#include
#include
#define max 40
using namespace std;
//struct User/*电话薄结构*/
//{
// char name[8];
// char tel[11];
// char add[20];
// /*问题①:C语言基础不牢固,在结构题没有完整定义之前不能用此类型名声明对象,指针和引用可以, 可以看看c与指针这本书*/
// //User next;
//}user;
//这是我的代码
typedef struct _User
{
char name[8];
char tel[11];
char add[20];
struct _User *next;
}User;
//struct Node / 哈希表结构* /
//{
// User user;
// Node next;/问题②:编程规范很重要,此处尽量用typedef*/
//};
//这是我的代码
typedef struct _Node
{
User user;
struct _Node *next;
}Node;
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); /问题③:此处的分号是有意的吗? /
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); /问题③:此处的分号是有意的吗? */
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;
}
#endif
我的运行没有崩,先看看我的代码,里面有你的错误,还有你的代码逻辑有问题,我没有细看...
解决方案九:
把tel改为tel[12],即电话号码11位加一个结束标志
#include<fstream>
#include <iostream>
using namespace std;
#define max 40
using namespace std;
struct User/*电话薄结构*/
{
char name[8];
char tel[11];
char add[20];
User *next; //cgl修改
}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[12], 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;
opt=0;
while (opt!=5) //修改
{
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;
}