1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
| #include
#include
#include
#include
#define NONEKEY -1
double ASL;
int number;
typedef struct
{
int key;
int count;
char name[19];
}HashTable[50];
typedef struct
{
int key;
char name[19];
}Person[50];
void InitHashTable(HashTable &ha)
{
int i,j;
for(i=0;i<50;i++)
{
ha[i].key=NONEKEY;
ha[i].count=0;
for(j=0;j<19;j++)
{
ha[i].name[j]=NULL;
}
}
}
void InitPerson(Person &person)
{
int total=0,i,j;
for(i=0;i;i++)
{
person[i].key=-1;
for(j=0;(j<19)&&(person[i].name[j]!='\0');j++)
{
total+=://www.opengroup.org/onlinepubs/009695399/functions/toascii.html">toascii(person[i].name[j]);
}
person[i].key=total%1000;
total=0;
}
}
void ReadFile(Person &person)
{
//char filename[20];
//printf("请输入要读取的文件名");
//scanf("%s",filename);
FILE *fp=://www.opengroup.org/onlinepubs/009695399/functions/fopen.html">fopen("D:\\xingming.txt","r");
int i;
if(fp==NULL)
{
printf("file read error");
exit(1);
}
for(i=0;i;i++)
{
fscanf(fp,"%s",person[i].name);
}
fclose(fp);
}
void InsertHashTable(HashTable &ha,Person person)
{
int i,address=0,j;
double sum=0.0;
for(i=0;i;i++)
{
int countnumber=1,d=0;
address=(person[i].key%47);
bool f = ha[address].key == NONEKEY;
sum+=1.0;
if(f)
{
ha[address].key =person[i].key;
ha[address].count+=1;
for(j=0;(j<19)&&(person[i].name[j]!='\0');j++)
{
ha[address].name[j]=person[i].name[j];
}
}
else
{
do
{
d=(d+person[i].key%47+1)%50;
countnumber++;
sum+=1.0;
}while(ha[d].key!=-1);
ha[d].count=countnumber;
ha[d].key = person[i].key;
for(j=0;(j<19)&&(person[i].name[j]!='\0');j++)
{
ha[d].name[j]=person[i].name[j];
}
}
}
ASL=sum/number;
}
void DisplayHashTable(HashTable ha)
{
printf("***********************************哈希表***************************************\n");
printf("序号\t\t姓名\t\t查找次数\t关键字\n");
for(int i=0;i<50;i++)
{
printf("%d\t",i);
printf("%8s\t\t%d\t\t%d",ha[i].name,ha[i].count,ha[i].key );
printf("\n");
}
printf("查找长度:%f\n",ASL);
printf("********************************************************************************\n");
}
void SearchHashTable(HashTable ha)
{
char name[19],a;
int address=0,j;
do
{
int d=0,sum=1,total=0;
printf("请输入要查找人的姓名:");
scanf("%s",name);
for(j=0;(j<19)&&(name[j]!='\0');j++)
{
total=total+://www.opengroup.org/onlinepubs/009695399/functions/toascii.html">toascii(name[j]);
}
address=((total%1000)%47);
if(ha[address].key==-1)
{
printf("不存在该姓名\n");
}
else
{
if(strcmp(name,ha[address].name)==0)
{
printf("输出所处位置%d\n",address);
}
else
{
do
{
d=(d+address+1)%50;
sum++;
}while((strcmp(name,ha[d].name)!=0)&&(sum<=number));
if(sum>number)
{
printf("查无此人\n");
}
else
{
printf("输出所处位置%d\n",d);
}
}
}
printf("继续查找输Y或y,结束输N或n:");
scanf("%s",&a);
}while((a=='y')||(a=='Y'));
}
void main()
{
char choice;
Person person;
HashTable ha;
printf("请输入要选择的人数(1至47):");
scanf("%d",&number);
getchar();
ReadFile(person);
InitHashTable(ha);
InitPerson(person);
InsertHashTable(ha,person);
printf("********************************哈希表功能目录**********************************\n");
printf("\t\t\t显示哈希表:A或a\n");
printf("\t\t\t查找姓名:B或b\n");
printf("\t\t\t退出:C或c\n");
printf("********************************************************************************\n");
printf("请输入选择:");
scanf("%c",&choice);
while((choice!='C')&&(choice!='c'))
{
switch(choice)
{
case 'A':
case 'a':
DisplayHashTable(ha);
break;
case 'B':
case 'b':
SearchHashTable(ha);
break;
default:;
}
getchar();
printf("下步操作:");
scanf("%c",&choice);
}
printf("******************************欢迎下次再次使用哈希表****************************\n");
}
|