Commit 54951130110cff7b70e012a4f7153251ff10e04e
1 parent
d74431cd
Ajout du fichier source capable de trouver le nombre d'erreurs dans un texte à p…
…artir d'un dictionnaire entré en paramètre. Ajout de fichiers de test.
Showing
5 changed files
with
181 additions
and
30 deletions
Show diff stats
lib.txt
@@ -2,60 +2,129 @@ | @@ -2,60 +2,129 @@ | ||
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <stdbool.h> | 3 | #include <stdbool.h> |
4 | #define MAX 30 | 4 | #define MAX 30 |
5 | +#define NB_CARAC 26 | ||
6 | +#define nb_car 5 | ||
5 | 7 | ||
6 | typedef struct node* Node; | 8 | typedef struct node* Node; |
7 | 9 | ||
8 | typedef struct node { | 10 | typedef struct node { |
9 | char letter; | 11 | char letter; |
10 | - Node next[26]; | 12 | + Node next[NB_CARAC]; |
11 | bool endWord; | 13 | bool endWord; |
12 | }node; | 14 | }node; |
13 | 15 | ||
14 | -void mk_empty_tree(Node* Tree) | 16 | +bool is_empty_tree(Node Tree) |
15 | { | 17 | { |
16 | - *Tree = NULL; | 18 | + return(Tree==NULL); |
17 | } | 19 | } |
18 | 20 | ||
19 | -bool is_empty_tree(Node* Tree) | 21 | +bool is_leaf(Node Tree) |
20 | { | 22 | { |
21 | - return(*Tree==NULL); | 23 | + for(int i=0; i<NB_CARAC; i++) |
24 | + if(Tree->next[i] != NULL) | ||
25 | + return false; | ||
26 | + return true; | ||
22 | } | 27 | } |
23 | 28 | ||
24 | void init_tree(Node* Tree) | 29 | void init_tree(Node* Tree) |
25 | { | 30 | { |
26 | - if(is_empty_tree(Tree)) | 31 | + if(is_empty_tree(*Tree)) |
27 | { | 32 | { |
28 | - *Tree = malloc(sizeof(Node)); | 33 | + *Tree = malloc(sizeof(node)); |
29 | (*Tree)->letter = '?'; | 34 | (*Tree)->letter = '?'; |
30 | (* Tree)->endWord = false; | 35 | (* Tree)->endWord = false; |
31 | - for(int i=0; i<26; i++) | 36 | + for(int i=0; i<NB_CARAC; i++) |
32 | (*Tree)->next[i] = NULL; | 37 | (*Tree)->next[i] = NULL; |
33 | } | 38 | } |
34 | } | 39 | } |
35 | 40 | ||
36 | -void add_in_tree(Node *Tree, char word[]) | 41 | +void add_in_tree(Node Tree, char word[]) |
37 | { | 42 | { |
38 | - char letter = word[0]; | ||
39 | int j=0; | 43 | int j=0; |
40 | - while(letter != '/0') | 44 | + Node Tree2 = Tree; |
45 | + while(word[j] != '\0') | ||
41 | { | 46 | { |
47 | + if(is_leaf(Tree2)) | ||
48 | + printf("empty\t"); | ||
42 | char letter = word[j]; | 49 | char letter = word[j]; |
43 | - if((*Tree)->next[letter-'a']!=NULL) | ||
44 | - *Tree = (*Tree)->next[letter-'a']; | 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 | + } | ||
45 | else | 56 | else |
46 | { | 57 | { |
47 | Node new = NULL; | 58 | Node new = NULL; |
48 | - new = malloc(sizeof(Node)); | 59 | + new = malloc(sizeof(node)); |
49 | new->letter = letter; | 60 | new->letter = letter; |
50 | for(int i=0; i<26; i++) | 61 | for(int i=0; i<26; i++) |
51 | { | 62 | { |
52 | new->next[i]=NULL; | 63 | new->next[i]=NULL; |
53 | } | 64 | } |
54 | - (*Tree)->next[letter-'a'] = new; | 65 | + Tree2->next[letter-'a'] = new; |
66 | + Tree2->endWord = false; | ||
67 | + Tree2=Tree2->next[letter-'a']; | ||
68 | + printf("%c %d\t", letter, letter-'a'); | ||
69 | + printf("okB %d\n", j); | ||
55 | } | 70 | } |
56 | j++; | 71 | j++; |
72 | + if(word[j]=='\0') | ||
73 | + Tree2->endWord = true; | ||
74 | + } | ||
75 | + printf("ok\n"); | ||
76 | +} | ||
77 | + | ||
78 | +bool is_end_caract(char letter) | ||
79 | +{ | ||
80 | + int endCaract[nb_car] = {0, 44, 45, 46, 47}; // a remplir | ||
81 | + for(int i=0; i<nb_car; i++) | ||
82 | + { | ||
83 | + if(letter==endCaract[i]) | ||
84 | + return true; | ||
85 | + } | ||
86 | + return false; | ||
87 | +} | ||
88 | + | ||
89 | +char max_index(char word[]) | ||
90 | +{ | ||
91 | + int index = 0; | ||
92 | + while(!is_end_caract(word[index])) | ||
93 | + index++; | ||
94 | + return index; | ||
95 | +} | ||
96 | + | ||
97 | +void scan_word(Node Tree, char word[], int* error) // ne gère pas les mots avec endWord = true | ||
98 | +{ | ||
99 | + int ind = 0; | ||
100 | + char letter; | ||
101 | + Node Tree2 = Tree; | ||
102 | + while(!is_end_caract(word[ind])) | ||
103 | + { | ||
104 | + letter = word[ind]; | ||
105 | + if(Tree2->next[letter-'a']!=NULL) | ||
106 | + { | ||
107 | + ind++; | ||
108 | + Tree2 = Tree2->next[letter-'a']; | ||
109 | + } | ||
110 | + else | ||
111 | + { | ||
112 | + printf("mot : %s erreur :%c %d \n", word, word[ind], word[ind]); | ||
113 | + (*error)++; | ||
114 | + ind = max_index(word); | ||
57 | } | 115 | } |
58 | - (*Tree)->endWord = true; | 116 | + } |
117 | +} | ||
118 | + | ||
119 | +void read_txt(FILE* fp, Node* Tree, int* error) | ||
120 | +{ | ||
121 | + char word[MAX]; | ||
122 | + while(1) | ||
123 | + { | ||
124 | + if(fscanf(fp, "%s", word)!=1) | ||
125 | + return; | ||
126 | + scan_word(*Tree, word, error); | ||
127 | + } | ||
59 | } | 128 | } |
60 | 129 | ||
61 | void read_lib(FILE* fp, Node* Tree) | 130 | void read_lib(FILE* fp, Node* Tree) |
@@ -63,29 +132,87 @@ void read_lib(FILE* fp, Node* Tree) | @@ -63,29 +132,87 @@ void read_lib(FILE* fp, Node* Tree) | ||
63 | char word[MAX]; | 132 | char word[MAX]; |
64 | while(1) | 133 | while(1) |
65 | { | 134 | { |
66 | - if(fscanf(fp, "%s", &word)!=1) | ||
67 | - break; | ||
68 | - add_in_tree(Tree, word); | 135 | + if(fscanf(fp, "%s", word)!=1) |
136 | + return; | ||
137 | + printf("--%s--\n", word); | ||
138 | + //fflush(stdout); | ||
139 | + add_in_tree(*Tree, word); | ||
69 | } | 140 | } |
70 | } | 141 | } |
71 | 142 | ||
72 | void print_tree(Node Tree, int index) | 143 | void print_tree(Node Tree, int index) |
73 | { | 144 | { |
74 | - Node printedTree = Tree; | ||
75 | - if(printedTree!=NULL) | ||
76 | - printf("%c", printedTree->letter); | ||
77 | - if(printedTree->next[index]==NULL) | ||
78 | - print_tree(printedTree, index+1); | 145 | + if(is_empty_tree(Tree)) |
146 | + return; | ||
147 | + Node cpTree = NULL; | ||
148 | + cpTree = Tree; | ||
149 | + if(cpTree->next[index]==NULL && index<26) | ||
150 | + { | ||
151 | + print_tree(cpTree, index+1); | ||
152 | + } | ||
153 | + else if(index == 26) | ||
154 | + { | ||
155 | + return; | ||
156 | + } | ||
79 | else | 157 | else |
80 | - print_tree(printedTree->next[index], 0); | 158 | + { |
159 | + printf("%c\n", (cpTree->next[index])->letter); | ||
160 | + if((cpTree->next[index])->endWord) | ||
161 | + { | ||
162 | + printf("fin\n"); | ||
163 | + //print_tree(cpTree, 0); | ||
164 | + return; | ||
165 | + } | ||
166 | + print_tree(cpTree->next[index], 0); | ||
167 | + } | ||
168 | +} | ||
169 | + | ||
170 | +int find_index(Node tree) | ||
171 | +{ | ||
172 | + Node cpTree = tree; | ||
173 | + int index = 0; | ||
174 | + while(cpTree->next[index]==NULL && index < NB_CARAC) | ||
175 | + index++; | ||
176 | + return index; | ||
177 | +} | ||
178 | + | ||
179 | +void print_first(Node Tree) | ||
180 | +{ | ||
181 | + Node cpTree = Tree; | ||
182 | + int index = 0; | ||
183 | + while(!is_leaf(cpTree)) | ||
184 | + { | ||
185 | + index = find_index(cpTree); | ||
186 | + printf("%c\n", (cpTree->next[index])->letter); | ||
187 | + cpTree=cpTree->next[index]; | ||
188 | + } | ||
81 | } | 189 | } |
82 | 190 | ||
83 | int main(int argc, char *argv[]) | 191 | int main(int argc, char *argv[]) |
84 | { | 192 | { |
85 | - Node Tree = NULL; | ||
86 | - FILE* fp; | ||
87 | - fp = fopen(argv[argc-1], "r"); | ||
88 | - read_lib(fp, &Tree); | ||
89 | - print_tree(Tree, 0); | 193 | + Node tree = NULL; |
194 | + int error = 0; | ||
195 | + FILE* fp_lib; | ||
196 | + FILE* fp_txt; | ||
197 | + fp_lib = fopen(argv[argc-2], "r"); | ||
198 | + fp_txt = fopen(argv[argc-1], "r"); | ||
199 | + | ||
200 | + init_tree(&tree); | ||
201 | + read_lib(fp_lib, &tree); | ||
202 | + read_txt(fp_txt, &tree, &error); | ||
203 | + | ||
204 | + printf("%p\n", tree); | ||
205 | + | ||
206 | + print_first(tree); | ||
207 | + printf("\n"); | ||
208 | + print_tree(tree, 0); | ||
209 | + | ||
210 | + printf("erreurs : %d\n", error); | ||
211 | + | ||
212 | + int endCaract[nb_car] = {0, 44, 45, 46, 47}; // a remplir | ||
213 | + for(int i=0; i<nb_car; i++) | ||
214 | + { | ||
215 | + printf("%c", endCaract[i]); | ||
216 | + } | ||
90 | return 0; | 217 | return 0; |
91 | } | 218 | } |