dico.c
4.96 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
#include "dico.h"
void make_empty_dico(dico d){
for(int i=0;i<NBCHAR;i++)
d[i]=NULL;
}
void delete_dico(dico d){
for(int i=0;i<NBCHAR;i++)
delete_tree(d+i);
}
byte addto_dico(dico d,const string word){
//return values : -1 if word can't enter, 0if allready in dico, 1 if added
int i;
byte isIn=0, endKind=0;
endKind=end_kind(word);
if(!is_word(endKind)){
return -1;
}
tree*tmp=&d[hash(word[0])],*temp=tmp;
//1st, let's go through the tree, until the word is not in tree
for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
temp=tmp;
if(isalpha(word[i+1]))
tmp=&((*tmp)->next[hash(word[i+1])]);
}
//if word is not ended at the end of the tree
if(is_empty(*tmp)){
isIn=0;
for(; word[i]!='\0' && word[i]!='\'';i++){
*tmp=make_node(tolower(word[i]),NOT_AN_END);
temp=tmp;
if(isalpha(word[i+1]))
tmp=&((*tmp)->next[hash(word[i+1])]);
}
(*temp)->isEnd = endKind;
}
//if word is ended
else{
isIn=endKind & (*temp)->isEnd;
(*temp)->isEnd = endKind | (*temp)->isEnd;
}
return isIn;
}
void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
void loadfrom_file(dico d,FILE*stream){
if(stream==NULL){
printf("sorry, we can't open the file\n");
return;
}
char word[30]={0};
while(fscanf(stream,"%30s",word)==1){
addto_dico(d,word);
}
printf("load success\n");
}
void printto_terminal(dico d){printto_file(d,stdout);}
void printto_file(dico d,FILE*stream){
if(stream==NULL){
printf("sorry, we can't open the file\n");
return;
}
if(d==NULL){
printf("sorry, we can't open the dictionary\n");
return;
}
printf("\n\nThis is what the dictionnary contains :\n");
for(int i=0;i<NBCHAR;i++){
print(d[i],stream,"");
}
}
void print(tree t,FILE*stream,string prefix){
//needs to check stream!=NULL
if(is_empty(t))
return;
string word=calloc((strlen(prefix)+2),sizeof(char));
strcpy(word,prefix);
strncat(word,&(t->letter),1);
if(is_end(t)){
string word2=calloc((strlen(prefix)+4),sizeof(char));
strcpy(word2,word);
//common_end
if(is_common_end(t) && is_straight_end(t)){
fprintf(stream,"%s\n",word2);
}
if(is_common_end(t) && ends_with_apostrophe(t)){
fprintf(stream,"%s's\n",word2);
}
//proper_end1
word2[0]=toupper(word2[0]);
if(is_proper_end(t) && is_straight_end(t)){
fprintf(stream,"%s\n",word2);
}
if(is_proper_end(t) && ends_with_apostrophe(t)){
fprintf(stream,"%s's\n",word2);
}
//acronyme_end
strupper(word2);
if(is_acronyme_end(t) && is_straight_end(t)){
fprintf(stream,"%s\n",word2);
}
if(is_acronyme_end(t) && ends_with_apostrophe(t)){
fprintf(stream,"%s's\n",word2);
}
free(word2);
}
for(int i=0;i<NBCHAR;i++){
print(t->next[i],stream,word);
}
free(word);
}
//uppers all letters in str
void strupper(string str){
for(int i=0;str[i]!='\0';i++)
str[i]=toupper(str[i]);
}
byte is_in(dico d,string word){
//return : -1 if incorrect word, 1 if is in, else 0
byte endKind=end_kind(word);
if(!is_word(endKind)){
return -1;
}
tree tmp=d[hash(word[0])];
for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){
tmp=tmp->next[hash(word[i])];
}
return !is_empty(tmp) && (endKind & is_end(tmp));
}
void test_words(dico d,FILE*inStream,FILE*outStream){
stringLIFO absWords=init_stringLIFO();
char word[30]={0};
byte isIn=0,someWrong=0;
while(fscanf(inStream,"%30s",word)!=EOF){
/*//get rid of punctuation marks
isIn=0;
while(ischar_of_word(word[(int)isIn]))
isIn++;
word[(int)isIn]='\0';
*/
isIn=is_in(d,word);
if(isIn<1){
if(someWrong==0){
someWrong=1;
fprintf(outStream,"Voici les mots absent du dictionnaire\n");
}
fprintf(outStream,"%s est absent dans le dictionnaire",word);
if(isIn)
fprintf(outStream,", car il est incorrect.\n");
else{
push(&absWords,word);
fprintf(outStream,".\n");
}
}
}
//fermer outStream pour permettre à l'utilisateur de lire le compte rendu des tests
if(outStream!=stdout)
fclose(outStream);
outStream=stdout;
//aucun mot ne peut être ajouté au dictionnaire ?
if(is_emptyLIFO(absWords)){
if(someWrong)
printf("Parmi les mots absents du dictionnaire, aucun ne peut être ajouté au dictionnaire.\n");
else
printf("Felicitations !!!\nAucun des mots que vous avez entre n'est absents du dictionnaire.\n");
return;
}
//ajout des mots absent?
printf("Voulez-vous ajouter certains de ces mots au dictionnaire?\n(1 for yes,0 for no)\n");
someWrong=saisie(0,1);
//ajout de certains mots au cas par cas
if(someWrong){
string aword;
while(!is_emptyLIFO(absWords)){
aword=pop(&absWords);
printf("Voulez-vous ajouter %s au dictionnaire? (1 for yes,0 for no)\n",aword);
isIn=saisie(0,1);
if(isIn){
addto_dico(d,aword);
}
free(aword);
}
}
delete_LIFO(&absWords);
}