Commit 293e356ce9231c2ecb41fd692003dbef3f2934fb
1 parent
12c9a42f
Version quasi définitive sans la fonction free
Showing
4 changed files
with
132 additions
and
51 deletions
Show diff stats
doc.txt
lib2.txt
@@ -8,4 +8,11 @@ aspirateur | @@ -8,4 +8,11 @@ aspirateur | ||
8 | chaussette | 8 | chaussette |
9 | chien | 9 | chien |
10 | chienne | 10 | chienne |
11 | -chiens | ||
12 | \ No newline at end of file | 11 | \ No newline at end of file |
12 | +chiens | ||
13 | +Doucement | ||
14 | +Etrange | ||
15 | +Odorat | ||
16 | +odorant | ||
17 | +L'ile | ||
18 | +L'ocean | ||
19 | +aaee''ee | ||
13 | \ No newline at end of file | 20 | \ No newline at end of file |
@@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
1 | +The ant and the cicada | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + In the old days, ants and cicadas were friends. They were very different. The ants were hardworking, but the cicadas were lazy. | ||
6 | + | ||
7 | + In the summer, the ant families were very busy. They knew that in the winter they would have to stay in their anthill. They wanted to have enough food for the whole winter. | ||
8 | + | ||
9 | + While the ants worked hard, the cicadas didn't do anything. They sang and danced all day. When they were hungry, they could fly to the farm and get something to eat. | ||
10 | + | ||
11 | + One day the cicadas were singing and dancing. They saw a long line of ants bringing food to their anthill. The cicadas said, Stop, my silly friends. It's a very nice day. Come and dance with us. The ants said, Don't you know about winter? If you don't work now, you'll have trouble later. | ||
12 | + | ||
13 | + But the cicadas said, We have strong wings. We can fly anywhere we want. Stupid ants! And they continued to sing and dance. | ||
14 | + | ||
15 | + In the winter, it rained or snowed all the time, and it was very cold. In the anthill, there was singing and dancing. But the cicadas had nothing to eat. They asked the ants for some food. The ants said, We thought you could fly anywhere. Now who is stupid and silly? | ||
16 | + | ||
17 | + The cicadas cried and said that their wings were wet from the rain. The ants said, We're sorry, but now it's to late. If we help you, there won't be enough food for us. Sorry, very sorry. And the ants closed their door. | ||
18 | + | ||
19 | + The next day, when the ants opened their door, all the cicadas were dead! That's why we can hear cicadas sing in the summer, but in the winter they are silent. |
1 | +// -------------------------------------------------------- | ||
2 | +// Projet IMA3 2019 - Lecture d'une bibliothèque | ||
3 | +// Décompte du nombre de fautes d'orthographe dans un texte | ||
4 | +// Normand Quentin & Rouillé Guillaume | ||
5 | +// -------------------------------------------------------- | ||
6 | + | ||
7 | +// Initialisation des variables et inclusion des bibliothèques | ||
1 | #include <stdio.h> | 8 | #include <stdio.h> |
2 | #include <stdlib.h> | 9 | #include <stdlib.h> |
3 | #include <stdbool.h> | 10 | #include <stdbool.h> |
4 | -#define MAX 30 | ||
5 | -#define NB_CARAC 26 | ||
6 | -#define nb_car 5 | 11 | +#define MAX 30 // taille maximale d'une chaîne lue dans un fichier |
12 | +#define NB_CARAC 27 // nombre de caractères différents pouvant être identifiés -> 89 avec accentués | ||
7 | 13 | ||
14 | +// Déclaration de la structure 'trie' ou 'arbre indexé', ainsi que des pointeurs associés | ||
8 | typedef struct node* Node; | 15 | typedef struct node* Node; |
9 | 16 | ||
10 | typedef struct node { | 17 | typedef struct node { |
@@ -13,11 +20,13 @@ typedef struct node { | @@ -13,11 +20,13 @@ typedef struct node { | ||
13 | bool endWord; | 20 | bool endWord; |
14 | }node; | 21 | }node; |
15 | 22 | ||
23 | +// Fonction permettant de savoir si la structure est vide | ||
16 | bool is_empty_tree(Node Tree) | 24 | bool is_empty_tree(Node Tree) |
17 | { | 25 | { |
18 | return(Tree==NULL); | 26 | return(Tree==NULL); |
19 | } | 27 | } |
20 | 28 | ||
29 | +// Fonction permettant de savoir si le tableau 'next' est un tableau de pointeurs NULL | ||
21 | bool is_leaf(Node Tree) | 30 | bool is_leaf(Node Tree) |
22 | { | 31 | { |
23 | for(int i=0; i<NB_CARAC; i++) | 32 | for(int i=0; i<NB_CARAC; i++) |
@@ -26,63 +35,79 @@ bool is_leaf(Node Tree) | @@ -26,63 +35,79 @@ bool is_leaf(Node Tree) | ||
26 | return true; | 35 | return true; |
27 | } | 36 | } |
28 | 37 | ||
38 | +// Initialisation de la structure accueillant le dictionnaire | ||
29 | void init_tree(Node* Tree) | 39 | void init_tree(Node* Tree) |
30 | { | 40 | { |
31 | if(is_empty_tree(*Tree)) | 41 | if(is_empty_tree(*Tree)) |
32 | { | 42 | { |
33 | *Tree = malloc(sizeof(node)); | 43 | *Tree = malloc(sizeof(node)); |
34 | - (*Tree)->letter = '?'; | 44 | + (*Tree)->letter = '?'; // caractère choisi arbitrairement |
35 | (* Tree)->endWord = false; | 45 | (* Tree)->endWord = false; |
36 | for(int i=0; i<NB_CARAC; i++) | 46 | for(int i=0; i<NB_CARAC; i++) |
37 | - (*Tree)->next[i] = NULL; | 47 | + (*Tree)->next[i] = NULL; // initialisation du tableau 'next' à un tableau de pointeurs NULL |
38 | } | 48 | } |
39 | } | 49 | } |
40 | 50 | ||
51 | +// Détermine l'indice de rangement dans le tableau 'next' du caractère 'letter' | ||
52 | +int find_caract_indice(char letter) // Ne fonctionne pas pour les caractères accentués | ||
53 | +{ | ||
54 | + //printf("__%d__\n", letter); | ||
55 | + if(letter>=97 && letter<=122) return letter-'a'; | ||
56 | + if(letter>=65 && letter<=90) return letter-'A'; | ||
57 | + if(letter == 39) return letter-13; // l'apostrophe est placée en 27ème position | ||
58 | + //if(letter>=192) {printf("%d", letter-166); return letter-166;} //-192+26 | ||
59 | +} | ||
60 | + | ||
61 | +// Fonction d'ajout d'un mot 'word' dans la structure 'tree' de type 'arbre indexé' | ||
41 | void add_in_tree(Node Tree, char word[]) | 62 | void add_in_tree(Node Tree, char word[]) |
42 | { | 63 | { |
43 | - int j=0; | 64 | + int j=0; // indice du caractère dans le mot 'word' |
65 | + int ind; // indice du caractère dans le tableau 'next' | ||
44 | Node Tree2 = Tree; | 66 | Node Tree2 = Tree; |
45 | - while(word[j] != '\0') | 67 | + while(word[j] != '\0') // on parcourt tout le mot 'word' |
46 | { | 68 | { |
47 | - if(is_leaf(Tree2)) | ||
48 | - printf("empty\t"); | 69 | + /*if(is_leaf(Tree2)) // a retirer |
70 | + printf("empty\t");*/ | ||
49 | char letter = word[j]; | 71 | char letter = word[j]; |
50 | - if(Tree2->next[letter-'a']!=NULL) | ||
51 | - { | ||
52 | - Tree2 = Tree2->next[letter-'a']; | ||
53 | - printf("%c %d\t", letter, letter-'a'); | ||
54 | - printf("okA %d\n", j); | ||
55 | - } | ||
56 | - else | ||
57 | - { | ||
58 | - Node new = NULL; | ||
59 | - new = malloc(sizeof(node)); | ||
60 | - new->letter = letter; | ||
61 | - for(int i=0; i<26; i++) | ||
62 | - { | ||
63 | - new->next[i]=NULL; | ||
64 | - } | ||
65 | - Tree2->next[letter-'a'] = new; | ||
66 | - if(!(Tree2->endWord)) | ||
67 | - Tree2->endWord = false; | ||
68 | - Tree2=Tree2->next[letter-'a']; | ||
69 | - printf("%c %d\t", letter, letter-'a'); | ||
70 | - printf("okB %d\n", j); | ||
71 | - } | 72 | + ind = find_caract_indice(letter); |
73 | + if(Tree2->next[ind]!=NULL) // si le pointeur du tableau 'next' corresdant au caractère lu n'est pas NULL, on s'insère dans cette 'branche' de l'arbre indexé et on continue avec le caractère suivant | ||
74 | + { | ||
75 | + Tree2 = Tree2->next[ind]; | ||
76 | + /* printf("%c %d\t", letter, ind); | ||
77 | + printf("okA %d\n", j);*/ | ||
78 | + } | ||
79 | + else // sinon, on ajoute une nouvelle cellule de type 'struct node' et on y insère les informations concernant le caractères | ||
80 | + { | ||
81 | + Node new = NULL; | ||
82 | + new = malloc(sizeof(node)); | ||
83 | + new->letter = letter; // le caractère | ||
84 | + for(int i=0; i<NB_CARAC; i++) // un tableau de pointeurs NULL | ||
85 | + { | ||
86 | + new->next[i]=NULL; | ||
87 | + } | ||
88 | + Tree2->next[ind] = new; // on fait pointé le tableau du caractère précédent vers cette cellule (vers ce caractère) | ||
89 | + /*if(!(Tree2->endWord)) // si le caractère n'est pas un caractère de fin, on le met à 'false' -> UTILITE ? | ||
90 | + Tree2->endWord = false;*/ | ||
91 | + Tree2=Tree2->next[ind]; // on se place au niveau du caractère suivant dans l'arbre indexé | ||
92 | + /*printf("%c %d\t", letter, ind); | ||
93 | + printf("okB %d\n", j);*/ | ||
94 | + } | ||
72 | j++; | 95 | j++; |
73 | - if(word[j]=='\0') | ||
74 | - Tree2->endWord = true; | 96 | + if(word[j]=='\0') // si le caractère suivant est la fin de la chaîne, on dit que le caractère précédent est un caractère de fin |
97 | + Tree2->endWord = true; | ||
75 | } | 98 | } |
76 | - printf("ok\n"); | 99 | + //printf("ok\n"); |
77 | } | 100 | } |
78 | 101 | ||
102 | +// Fonction qui détermine si le caractère est un caractère de fin de mot (espace, ',', ';', '.', etc..) | ||
79 | bool is_end_caract(char letter) | 103 | bool is_end_caract(char letter) |
80 | { | 104 | { |
81 | if(letter==0) return true; | 105 | if(letter==0) return true; |
82 | - if((letter>=32 && letter<=47)||(letter>=58 && letter<=64)||(letter>=123 && letter<=126)||(letter==128)) return true; | 106 | + if((letter>=32 && letter<=38)||(letter>=40 && letter<=47)||(letter>=58 && letter<=64)||(letter>=123 && letter<=126)||(letter==128)) return true; |
83 | return false; | 107 | return false; |
84 | } | 108 | } |
85 | 109 | ||
110 | +// Renvoi l'indice maximum du mot 'word' | ||
86 | char max_index(char word[]) | 111 | char max_index(char word[]) |
87 | { | 112 | { |
88 | int index = 0; | 113 | int index = 0; |
@@ -93,29 +118,32 @@ char max_index(char word[]) | @@ -93,29 +118,32 @@ char max_index(char word[]) | ||
93 | 118 | ||
94 | void scan_word(Node Tree, char word[], int* error) // si un mot démarre juste après un caractère de fin, la fonction ne lit pas les mots séparément | 119 | void scan_word(Node Tree, char word[], int* error) // si un mot démarre juste après un caractère de fin, la fonction ne lit pas les mots séparément |
95 | { | 120 | { |
96 | - bool endWord; | 121 | + bool endWord = false; |
97 | bool stop = false; | 122 | bool stop = false; |
98 | int ind = 0; | 123 | int ind = 0; |
124 | + int indice; | ||
99 | char letter; | 125 | char letter; |
100 | Node Tree2 = Tree; | 126 | Node Tree2 = Tree; |
101 | while(!is_end_caract(word[ind])) | 127 | while(!is_end_caract(word[ind])) |
102 | { | 128 | { |
103 | stop = false; | 129 | stop = false; |
104 | letter = word[ind]; | 130 | letter = word[ind]; |
105 | - if(Tree2->next[letter-'a']!=NULL) | 131 | + indice = find_caract_indice(letter); |
132 | + | ||
133 | + if(Tree2 != NULL && Tree2->next[indice]!=NULL) | ||
106 | { | 134 | { |
107 | ind++; | 135 | ind++; |
108 | - Tree2 = Tree2->next[letter-'a']; | 136 | + Tree2 = Tree2->next[indice]; |
109 | endWord = Tree2->endWord; | 137 | endWord = Tree2->endWord; |
110 | - if(endWord) printf("end :: %s ::\n", word); | 138 | + /*if(endWord) printf("end :: %s ::\n", word);*/ |
111 | } | 139 | } |
112 | else | 140 | else |
113 | { | 141 | { |
114 | printf("mot : %s erreur :%c %d \n", word, word[ind], word[ind]); | 142 | printf("mot : %s erreur :%c %d \n", word, word[ind], word[ind]); |
115 | (*error)++; | 143 | (*error)++; |
116 | - printf("%d\n", ind); | 144 | + //printf("%d\n", ind); |
117 | ind = max_index(word); | 145 | ind = max_index(word); |
118 | - printf("%d\n", ind); | 146 | + //printf("%d\n", ind); |
119 | stop = true; | 147 | stop = true; |
120 | } | 148 | } |
121 | } | 149 | } |
@@ -126,6 +154,14 @@ void scan_word(Node Tree, char word[], int* error) // si un mot démarre juste a | @@ -126,6 +154,14 @@ void scan_word(Node Tree, char word[], int* error) // si un mot démarre juste a | ||
126 | } | 154 | } |
127 | } | 155 | } |
128 | 156 | ||
157 | +bool no_accent(char word[]) | ||
158 | +{ | ||
159 | + int index_max = max_index(word); | ||
160 | + for(int i=0; i<index_max; i++) | ||
161 | + if(word[i]<0) return false; | ||
162 | + return true; | ||
163 | +} | ||
164 | + | ||
129 | void read_txt(FILE* fp, Node* Tree, int* error) | 165 | void read_txt(FILE* fp, Node* Tree, int* error) |
130 | { | 166 | { |
131 | char word[MAX]; | 167 | char word[MAX]; |
@@ -144,9 +180,10 @@ void read_lib(FILE* fp, Node* Tree) | @@ -144,9 +180,10 @@ void read_lib(FILE* fp, Node* Tree) | ||
144 | { | 180 | { |
145 | if(fscanf(fp, "%s", word)!=1) | 181 | if(fscanf(fp, "%s", word)!=1) |
146 | return; | 182 | return; |
147 | - printf("--%s--\n", word); | 183 | + // printf("--%s--\n", word); |
148 | //fflush(stdout); | 184 | //fflush(stdout); |
149 | - add_in_tree(*Tree, word); | 185 | + if(no_accent(word)) |
186 | + add_in_tree(*Tree, word); | ||
150 | } | 187 | } |
151 | } | 188 | } |
152 | 189 | ||
@@ -168,11 +205,11 @@ void print_tree(Node Tree, int index) | @@ -168,11 +205,11 @@ void print_tree(Node Tree, int index) | ||
168 | { | 205 | { |
169 | printf("%c\n", (cpTree->next[index])->letter); | 206 | printf("%c\n", (cpTree->next[index])->letter); |
170 | if((cpTree->next[index])->endWord) | 207 | if((cpTree->next[index])->endWord) |
171 | - { | ||
172 | - printf("fin\n"); | ||
173 | - //print_tree(cpTree, 0); | ||
174 | - return; | ||
175 | - } | 208 | + { |
209 | + printf("fin\n"); | ||
210 | + //print_tree(cpTree, 0); | ||
211 | + return; | ||
212 | + } | ||
176 | print_tree(cpTree->next[index], 0); | 213 | print_tree(cpTree->next[index], 0); |
177 | } | 214 | } |
178 | } | 215 | } |
@@ -199,6 +236,19 @@ void print_first(Node Tree) | @@ -199,6 +236,19 @@ void print_first(Node Tree) | ||
199 | } | 236 | } |
200 | } | 237 | } |
201 | 238 | ||
239 | +/*void free_tree(Node Tree) | ||
240 | +{ | ||
241 | + int index=0; | ||
242 | + while(index<NB_CARAC) | ||
243 | + { | ||
244 | + if(Tree->next[index]!=NULL) | ||
245 | + free_tree(Tree->next[index]); | ||
246 | + else | ||
247 | + index++; | ||
248 | + } | ||
249 | + free(Tree); | ||
250 | + }*/ | ||
251 | + | ||
202 | int main(int argc, char *argv[]) | 252 | int main(int argc, char *argv[]) |
203 | { | 253 | { |
204 | Node tree = NULL; | 254 | Node tree = NULL; |
@@ -214,11 +264,12 @@ int main(int argc, char *argv[]) | @@ -214,11 +264,12 @@ int main(int argc, char *argv[]) | ||
214 | 264 | ||
215 | // printf("%p\n", tree); | 265 | // printf("%p\n", tree); |
216 | 266 | ||
217 | - print_first(tree); | 267 | + //print_first(tree); |
218 | //printf("\n"); | 268 | //printf("\n"); |
219 | //print_tree(tree, 0); | 269 | //print_tree(tree, 0); |
220 | 270 | ||
221 | printf("erreurs : %d\n", error); | 271 | printf("erreurs : %d\n", error); |
222 | 272 | ||
273 | + //free_tree(tree); | ||
223 | return 0; | 274 | return 0; |
224 | } | 275 | } |