-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash Table.c
157 lines (149 loc) · 4.3 KB
/
Hash Table.c
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
//Just for easy..
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct words{
char word[10];
char context[100];
}wordTable;
int num = 0;
unsigned int hashArray[0xffff] = {0,}; //for file read/write
unsigned int HashEncrypt(char* word){
int length = strlen(word);
unsigned int hashNum = 0;
for(unsigned int i = 0;i < length;i++){
if(i % 2 == 0){
hashNum ^= ((word[i] - 'A') << 8) ^ i;
}else{
hashNum ^= (word[i] - 'A') ^ i;
}
}
return hashNum;
}
int FromFileInputWords(wordTable* words){
wordTable subData = {"\0","\0"};
int size = sizeof(wordTable);
FILE *Textfp = NULL;
FILE *Hashfp = NULL;
Textfp = fopen("E:\\workspace\\wordtable.txt","rb");
Hashfp = fopen("E:\\workspace\\hash.txt","r");
if(Textfp == NULL || Hashfp == NULL){
return 0;
}
while(getc(Textfp) != EOF){
fseek(Textfp,-1,1);
fread(&subData,size,1,Textfp);
fscanf(Hashfp,"%d",&hashArray[num]);
fgetc(Hashfp);
if(num < 0xffff){
memcpy((words + hashArray[num++]),&subData,size);
}else{
printf("The Array is full!!\n");
}
}
fclose(Textfp);
fclose(Hashfp);
return 1;
}
void FillTable(wordTable* words){
int choice,subHash;
wordTable subData;
int size = sizeof(wordTable);
while(1){
if(num >= 0xffff){
printf("This Array is full!\n");
return;
}
printf("Please input the word:\n");
fgets(subData.word,10,stdin);
if(subData.word[strlen(subData.word) - 1] == '\n'){
subData.word[strlen(subData.word) - 1] = '\0';
}
subHash = HashEncrypt(subData.word);
if(words[subHash].word[0] != '\0'){
printf("Conflicting!!\n");
return;
}
printf("Now please input the context of this word:\n");
fgets(subData.context,100,stdin);
if(subData.context[strlen(subData.context) - 1] == '\n'){
subData.context[strlen(subData.context) - 1] = '\0';
}
memcpy(words + subHash,&subData,size);
hashArray[num++] = subHash;
printf("1.Continue 2.Break\n");
if(scanf("%d",&choice),choice == 2){ //No judge.Easy Best!
getchar();
break;
}
getchar();
}
}
void SearchTable(wordTable* words){
char word[10];
int loc = 0;
printf("Please input the word which you want to search:\n");
fgets(word,10,stdin);
if(word[strlen(word) - 1] == '\n'){
word[strlen(word) - 1] = '\0';
}
if(loc = HashEncrypt(word),*(words[loc].word) != '\0'){
printf("This word's meaning is %s\n",words[loc].context);
}else{
printf("No this word here.\n");
}
}
void PutWordsIntoFile(const wordTable* words){
int size = sizeof(wordTable);
FILE *Textfp = NULL;
FILE *Hashfp = NULL;
Textfp = fopen("E:\\workspace\\wordtable.txt","wb+");
Hashfp = fopen("E:\\workspace\\hash.txt","wt+");
if(Textfp == NULL || Hashfp == NULL){
return;
}
for(int i = 0;i < num;i++){
fwrite(words + hashArray[i],size,1,Textfp);
fprintf(Hashfp,"%d",hashArray[i]);
fputc('\n',Hashfp);
}
fclose(Textfp);
fclose(Hashfp);
}
int main(){
int choice;
wordTable* words = (wordTable*)malloc(sizeof(wordTable) * 0xffff);
memset(words,0,sizeof(wordTable) * 0xffff);
if(!FromFileInputWords(words)){
printf("Load File failed!!!\n");
return 0;
}
while(1){
printf("Welcome to the dic!\n");
printf("Please choose the fuc which you would like to use:\n");
printf("1.Fill the dic 2.Search\n");
printf("3.Exit\n");
retry:
scanf("%d",&choice);
getchar();
switch(choice){
case 1:
FillTable(words);
break;
case 2:
SearchTable(words);
break;
case 3:
printf("Looking forward to the next time\n");
goto end;
default:
printf("Please input the right num\n");
goto retry;
}
}
end:
PutWordsIntoFile(words);
system("pause");
return 0;
}