Repository navigation
Expand file tree
/
Copy pathHashFile_self.cpp
More file actions
241 lines (231 loc) · 4.67 KB
/
Copy pathHashFile_self.cpp
File metadata and controls
241 lines (231 loc) · 4.67 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma warning(disable:4996);
#include<iostream>
using namespace std;
const int N = 13;
typedef int keyType;
struct elemType {
keyType key;
char value[20];
};
struct Node {
struct elemType data;
int next;
};
const char Hash[] = "Hash";
const char Index[] = "HashIndex";
const int keySize = sizeof(keyType);
const int elemSize = sizeof(Node);
int haveData(int);
/*
if (mod == 1) {
初始化
}
else {
新建文件
}
*/
int getHash(int key) {
return key % N;
}
bool reWrite(int mod) {//初始化散列文件
if (mod == 1) {
cout << "--- 确定要初始化散列文件(y/n)? ---\n";
char a;
cin >> a;
if (a != 'y' && a != 'Y') {
cout << "--- 未执行初始化操作! ---\n";
return false;
}
}
FILE* fp, * fp2;
fp = fopen(Hash, "wb+");
fp2 = fopen(Index, "wb+");
if (fp == NULL || fp2 == NULL) {
cout << "--- 文件打开失败!---\n";
return false;
}
int* arr = (int*)calloc(N, keySize);
//初始化链表,-1代表空
for (int i = 0; i < N; i++) {
arr[i] = -1;
}
fwrite((char*)arr, N * keySize, 1, fp2);
fclose(fp);
fclose(fp2);
cout << "--- 执行成功! ---\n";
return true;
}
//检测是否有散列文件
bool create() {
FILE* fp, * fp2;
fp = fopen(Hash, "rb");
fp2 = fopen(Index, "rb");
//文件未打开
if (fp == NULL || fp2 == NULL) {
cout << "--- 文件不存在,是否要新建Hash文件(y/n)? ---\n";
char a;
cin >> a;
if (a == 'y' || a == 'Y') {
cout << "--- 正在新建Hash文件! ---\n";
}
else {
cout << "--- 未执行新建操作! ---\n";
return false;
}
reWrite(0);
}
return true;
}
bool insertOne() {
FILE* fp, * fp2;
fp = fopen(Hash, "rb+");
fp2 = fopen(Index, "rb+");
if (fp == NULL || fp2 == NULL) {
cout << "--- 文件打开失败! ---\n";
return false;
}
cout << "请输入待插入元素x的值(一个整数(关键字)和一个字符串(附加信息,长度小于20)):\n";
struct Node x;
int* arr = (int*)calloc(N, keySize);
cin >> x.data.key;
cin >> x.data.value;
if (haveData(x.data.key)) {
cout << "--- 该关键值已存在,不能重复录入! ---\n";
return false;
}
//获取链表头
fread((char*)arr, (N)*keySize, 1, fp2);
int hash = getHash(x.data.key);
x.next = arr[hash];
//文件内指针移至文件尾
fseek(fp, 0L, SEEK_END);
//计算出结点位置序号
int len = ftell(fp) / elemSize;
fwrite((char*)&x, elemSize, 1, fp);
arr[hash] = len;
//指针移到文件头
fseek(fp2, 0L, SEEK_SET);
fwrite((char*)arr, N * keySize, 1, fp2);
fclose(fp);
fclose(fp2);
return true;
}
//b==true 手动输入关键字
//b=false 不需要输入关键字
void searchOne() {
int key;
cout << "请输入关键值\n";
cin >> key;
int hash = getHash(key);
FILE* fp, * fp2;
fp = fopen(Hash, "rb+");
fp2 = fopen(Index, "rb+");
int* arr = (int*)calloc(N, keySize);
fread((char*)arr, N * keySize, 1, fp2);
int p = arr[hash];
if (p == -1) {
cout << "不存在此元素!\n";
return;
}
while (p != -1) {
Node x;
fseek(fp, p * elemSize, SEEK_SET);
fread((char*)&x, elemSize, 1, fp);
if (x.data.key == key) {
cout << "value= " << x.data.value << endl;
}
p = x.next;
}
}
int haveData(int key) {
int hash = getHash(key);
FILE* fp, * fp2;
fp = fopen(Hash, "rb+");
fp2 = fopen(Index, "rb+");
int* arr = (int*)calloc(N, keySize);
fread((char*)arr, N * keySize, 1, fp2);
int p = arr[hash];
if (p == -1) {
return false;
}
while (p != -1) {
Node x;
fseek(fp, p * elemSize, SEEK_SET);
fread((char*)&x, elemSize, 1, fp);
if (x.data.key == key) {
return true;
}
p = x.next;
}
return false;
}
void display() {
FILE* fp, * fp2;
fp = fopen(Hash, "rb+");
fp2 = fopen(Index, "rb+");
if (fp == NULL || fp2 == NULL) {
cout << "文件打开失败!\n";
return;
}
int* arr = (int*)calloc(N, keySize);
if (arr == NULL) {
cout << "数组空间申请失败!\n";
return;
}
fread((char*)arr, keySize, N, fp2);
for (int i = 0; i < N; i++) {
int p = arr[i];
cout << i;
while (p != -1) {
Node x;
fseek(fp, p * elemSize, SEEK_SET);
fread((char*)&x, elemSize, 1, fp);
cout << " ->" << x.data.key << "(" << x.data.value << ") ";
p = x.next;
}
cout << endl;
}
fclose(fp);
fclose(fp2);
}
void start() {
cout << "--- 1.初始化散列文件 ---\n";
cout << "--- 2.插入新值 ---\n";
cout << "--- 3.输出全部数据 ---\n";
cout << "--- 4.查找元素 ---\n";
cout << "--- 0.退出程序 ---\n";
int a;
cin >> a;
switch (a)
{
case 1: {
reWrite(1);
break;
}
case 2: {
insertOne();
break;
}
case 3: {
display();
break;
}
case 4: {
searchOne();
break;
}
case 0: {
exit(0);
}
default:
cout << "输入错误!请重新输入!\n";
return;
}
}
int main() {
create();
while (1) {
start();
}
return 0;
}