0bc3d1ad
bjeanlou
dico created
|
1
2
3
|
#include "dico.h"
void make_empty_dico(dico d){
|
003d3e48
bjeanlou
update6 withHash
|
4
|
for(int i=0;i<NBCHAR;i++)
|
0bc3d1ad
bjeanlou
dico created
|
5
6
|
d[i]=NULL;
}
|
a0fe64d2
bjeanlou
Update dico
|
7
|
void delete_dico(dico d){
|
cf53766d
bjeanlou
update2 dico
|
8
|
for(int i=0;i<NBCHAR;i++)
|
0bc3d1ad
bjeanlou
dico created
|
9
10
11
|
delete_tree(d[i]);
}
|
11c78e6d
bjeanlou
update5 withHash
|
12
13
|
|
8c671959
bjeanlou
No more seg fault
|
14
15
16
17
|
byte addto_dico(dico d,const string word){
//-1 if word can't enter, 0if allready in dico, 1 if added
int i;
byte isIn, endKind=end_kind(word);
|
08822d77
bjeanlou
Update 9 withHash
|
18
|
printf("%x\n",endKind);
|
11c78e6d
bjeanlou
update5 withHash
|
19
|
if(!is_word(endKind)){
|
08822d77
bjeanlou
Update 9 withHash
|
20
|
printf("%s is incorrect\n",word);
|
8c671959
bjeanlou
No more seg fault
|
21
|
return -1;
|
a0fe64d2
bjeanlou
Update dico
|
22
|
}
|
8c671959
bjeanlou
No more seg fault
|
23
24
|
tree*tmp=&d[hash(word[0])],*temp;
//1st, let's go through the tree, until the word is not in tree
|
08822d77
bjeanlou
Update 9 withHash
|
25
|
for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
|
8c671959
bjeanlou
No more seg fault
|
26
|
temp=tmp;
|
08822d77
bjeanlou
Update 9 withHash
|
27
|
tmp=&((*tmp)->next[hash(word[i+1])]);
|
8c671959
bjeanlou
No more seg fault
|
28
29
30
31
32
33
34
35
36
37
|
}
//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;
tmp=&((*tmp)->next[hash(word[i])]);
}
(*temp)->isEnd = endKind;
|
08822d77
bjeanlou
Update 9 withHash
|
38
|
printf("%s added\n",word);
|
8c671959
bjeanlou
No more seg fault
|
39
40
41
42
|
fflush(stdout);
}
//if word is ended
else{
|
08822d77
bjeanlou
Update 9 withHash
|
43
44
45
|
isIn=endKind & (*temp)->isEnd;
(*temp)->isEnd = endKind | is_end(*temp);
printf("%s allready in\n",word);
|
8c671959
bjeanlou
No more seg fault
|
46
47
48
|
fflush(stdout);
}
return isIn;
|
a0fe64d2
bjeanlou
Update dico
|
49
50
|
}
|
8c671959
bjeanlou
No more seg fault
|
51
52
53
54
55
|
|
11c78e6d
bjeanlou
update5 withHash
|
56
57
58
|
void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
void loadfrom_file(dico d,FILE*stream){
if(stream==NULL){
|
a89bb625
bjeanlou
Update 8 withHash
|
59
|
printf("sorry, we can't open the file\n");
|
11c78e6d
bjeanlou
update5 withHash
|
60
|
return;
|
62535895
bjeanlou
Update1 dico
|
61
|
}
|
003d3e48
bjeanlou
update6 withHash
|
62
|
char word[30]={0};
|
8c671959
bjeanlou
No more seg fault
|
63
|
while(fscanf(stream,"%30s",word)!=EOF){
|
003d3e48
bjeanlou
update6 withHash
|
64
|
addto_dico(d,word);
|
a89bb625
bjeanlou
Update 8 withHash
|
65
66
|
}
printf("load success\n");
|
11c78e6d
bjeanlou
update5 withHash
|
67
68
|
}
|
a89bb625
bjeanlou
Update 8 withHash
|
69
70
71
72
73
74
75
76
77
78
79
80
81
|
|
cf53766d
bjeanlou
update2 dico
|
82
|
void printto_terminal(dico d){printto_file(d,stdout);}
|
003d3e48
bjeanlou
update6 withHash
|
83
84
|
void printto_file(dico d,FILE*stream){
if(stream==NULL){
|
a89bb625
bjeanlou
Update 8 withHash
|
85
|
printf("sorry, we can't open the file\n");
|
003d3e48
bjeanlou
update6 withHash
|
86
87
88
|
return;
}
if(d==NULL){
|
a89bb625
bjeanlou
Update 8 withHash
|
89
|
printf("sorry, we can't open the dictionary\n");
|
003d3e48
bjeanlou
update6 withHash
|
90
91
|
return;
}
|
08822d77
bjeanlou
Update 9 withHash
|
92
|
printf("\n\nThis is what the dictionnary contains :\n");
|
003d3e48
bjeanlou
update6 withHash
|
93
94
95
96
97
98
99
100
101
|
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;
|
cf53766d
bjeanlou
update2 dico
|
102
103
104
105
106
|
string word=calloc((strlen(prefix)+2),sizeof(char));
strcpy(word,prefix);
strncat(word,&(t->letter),1);
|
003d3e48
bjeanlou
update6 withHash
|
107
|
if(is_end(t)){
|
cf53766d
bjeanlou
update2 dico
|
108
109
110
|
string word2=calloc((strlen(prefix)+4),sizeof(char));
strcpy(word2,word);
//common_end
|
08822d77
bjeanlou
Update 9 withHash
|
111
112
|
if(is_common_end(t) && is_straight_end(t)){
fprintf(stream,"%s\n",word2);
|
003d3e48
bjeanlou
update6 withHash
|
113
|
}
|
08822d77
bjeanlou
Update 9 withHash
|
114
115
116
117
118
119
120
121
122
123
|
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);
|
003d3e48
bjeanlou
update6 withHash
|
124
|
}
|
cf53766d
bjeanlou
update2 dico
|
125
|
//acronyme_end
|
08822d77
bjeanlou
Update 9 withHash
|
126
127
128
129
130
131
|
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);
|
003d3e48
bjeanlou
update6 withHash
|
132
|
}
|
08822d77
bjeanlou
Update 9 withHash
|
133
|
|
cf53766d
bjeanlou
update2 dico
|
134
|
free(word2);
|
003d3e48
bjeanlou
update6 withHash
|
135
136
|
}
for(int i=0;i<NBCHAR;i++){
|
cf53766d
bjeanlou
update2 dico
|
137
|
print(t->next[i],stream,word);
|
003d3e48
bjeanlou
update6 withHash
|
138
|
}
|
cf53766d
bjeanlou
update2 dico
|
139
140
141
142
143
144
|
free(word);
}
void strupper(string str){
for(int i=0;str[i]!='\0';i++)
str[i]=toupper(str[i]);
|
0bc3d1ad
bjeanlou
dico created
|
145
|
}
|
fd1ee590
bjeanlou
Update7 withHash
|
146
147
148
149
150
151
152
153
|
bool is_in(dico d,string word){
byte endKind=end_kind(word);
if(!is_word(endKind)){
return false;
}
tree tmp=d[hash(word[0])];
|
a89bb625
bjeanlou
Update 8 withHash
|
154
|
for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){
|
fd1ee590
bjeanlou
Update7 withHash
|
155
156
157
158
|
tmp=tmp->next[hash(word[i])];
}
return !is_empty(tmp) && (endKind & is_end(tmp));
}
|