-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.c
executable file
·209 lines (184 loc) · 3.73 KB
/
trie.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
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
#include<limits.h>
//#include "trie.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_EMAIL_LENGTH 321
#define MAX_NEXT 62
#define MAX_CHAR_ASCII 123
FILE *fp = NULL;
char email[MAX_EMAIL_LENGTH];
int charTran[MAX_CHAR_ASCII+1];
int lineNum = 1;
int fileEnd = -1;
typedef struct trieNode
{
int result;
struct trieNode *next[MAX_NEXT];
}trieNode;
trieNode *root = NULL;
int char_transfer()
{
FILE *fp2 = NULL;
char aChar;
int i=0;
if((fp2=fopen("ch.txt","r"))!=NULL)
{
while((aChar=fgetc(fp2))!= EOF)
{
charTran[aChar]=i;
//printf("\n%d",charTran[aChar]);
i++;
}
printf("\nChar ASCII transfer complete");
}
else
{
printf("\nOpen ch.txt error");
exit(1);
}
fclose(fp2);
}
void initial()
{
int i=0;
root=(trieNode*)malloc(sizeof(trieNode));
for(i=0; i<MAX_NEXT; i++)
{
root->next[i]=NULL;
}
root->result=-1;
}
trieNode* creat_node()
{
int i=0;
trieNode *p=(trieNode*)malloc(sizeof(trieNode));
for(i=0; i<MAX_NEXT; i++)
{
p->next[i]=NULL;
}
p->result=-1;
return p;
}
void insert()
{
//trieNode *p = root;
// printf("-----root's result is %d",p->result);
// int j=charTran[email[0]];
// p=p->next[j];
int i=0;
trieNode *p = root;
for(i=0; i<MAX_EMAIL_LENGTH; i++)
{
//printf("\n------j is %d -------",j);
if((p->next[charTran[email[i]]]) == NULL)
{
p->next[charTran[email[i]]]=creat_node();
}
p = p->next[charTran[email[i]]];
if(email[i+1]=='\n' || email[i+1]=='\0')
{
p->result=1;
break;
}
}
}
int trie_check()
{
int i=0;
trieNode *p=root;
for(i=0; i<MAX_EMAIL_LENGTH; i++)
{
if((p->next[charTran[email[i]]]) == NULL)
return -1;
else
{
p = p->next[charTran[email[i]]];
if(email[i+1]=='\n' || email[i+1]=='\0')
{
if(p->result < 0)
return -1;
else
return 1;
}
}
}
}
void file_open(char *fileName)
{
if((fp=fopen(fileName,"r"))==NULL)
{
printf("\n%s was not successfully opened",fileName);
exit(1);
}
else
{
printf("\n%s was opened successfully for read",fileName);
}
}
int read_line()
{
int i;
memset(email,0,sizeof(email));
if(fgets(email,MAX_EMAIL_LENGTH,fp)!= NULL)
{
for(i=0;i<sizeof(email);i++)
{
if(email[i]==NULL)
break;
email[i]=tolower(email[i]);
}
lineNum++;
if(!(lineNum%100000))printf("\nRead the %d line: %s",lineNum,email);
return 1;
//printf("\n --%d--",i);
}
else
{
if(feof(fp))
{
printf("\nEnd of the file");
return -1;
}
else
{
printf("\nRead error");
exit(1);
}
}
}
int main()
{
char *strPool = malloc(sizeof(char)*20);
char *checkedEmail = malloc(sizeof(char)*20);
int checkedResult;
printf("Please enter the email pool file you want to input: ");
scanf("%s", strPool);
char_transfer();
initial();
file_open(strPool);
while(read_line()>0)
{
insert();
}
fclose(fp);
printf("\nDone! finish inputing the email pool");
printf("\n------------------------------------------------------");
printf("\nPlease enter the checkedemail filename for search: ");
scanf("%s", checkedEmail);
// fileEnd=-1;
lineNum = 1;
file_open(checkedEmail);
while(read_line()>0)
{
checkedResult=trie_check();
if(checkedResult<0)
printf("\nThe %d line: -NO-",lineNum-1);
else
printf("\nThe %d line: -YES-",lineNum-1);
}
fclose(fp);
printf("\nDone! finish checking");
printf("\n------------------------------------------------------");
return 0;
}