Commit 2d053d0686ea2240da48653dc5d14b4b7b38e28e
1 parent
8b61d242
Projet modifié
Showing
1 changed file
with
116 additions
and
73 deletions
Show diff stats
correcteur.c
... | ... | @@ -4,120 +4,163 @@ |
4 | 4 | #include <string.h> |
5 | 5 | |
6 | 6 | #define MAX_LETTRES 30 |
7 | +#define size 26 | |
7 | 8 | |
8 | 9 | |
9 | 10 | typedef struct node { |
10 | 11 | char l; |
11 | - struct node * lettres[27]; | |
12 | + struct node * lettres[size]; | |
12 | 13 | bool fin_de_mot; |
13 | - int dernier; | |
14 | 14 | } Node; |
15 | 15 | |
16 | + | |
17 | + | |
18 | + | |
19 | + | |
20 | + | |
16 | 21 | bool is_empty(struct node *tree) |
17 | 22 | { |
18 | 23 | return tree==NULL; |
19 | 24 | } |
20 | 25 | |
26 | + | |
27 | + | |
28 | + | |
29 | + | |
30 | + | |
21 | 31 | bool is_leaf(struct node *tree) |
22 | 32 | { |
23 | - int cpt; | |
24 | - int i=0; | |
25 | - if (is_empty(tree)) return 1; | |
26 | - while (tree->lettres[i] != NULL){ | |
27 | - cpt++; | |
28 | - i++; | |
29 | - } | |
30 | - if (cpt !=0) cpt=1; | |
31 | - return(!is_empty(tree) && !cpt); | |
33 | + int cpt=1; | |
34 | + if (is_empty(tree)) return 1; | |
35 | + for (int i=0; i<size ; i++){ | |
36 | + if ( tree->lettres[i] == NULL){ | |
37 | + cpt++; | |
38 | + } | |
39 | + } | |
40 | + if (cpt == size) cpt=1; | |
41 | + if (cpt != size) cpt=0; | |
42 | + return cpt ; | |
32 | 43 | } |
33 | 44 | |
34 | 45 | |
35 | -void ajout(Node **N, char mot) | |
46 | + | |
47 | + | |
48 | + | |
49 | + | |
50 | +void ajout(Node **N, char lettre) | |
36 | 51 | { |
37 | - Node *nouveau = malloc(sizeof(struct node)); | |
38 | - (*nouveau).l=mot; | |
39 | - for (int i=0; i<27; i++) nouveau->lettres[i]=NULL; | |
40 | - *N = nouveau; | |
52 | + Node *nouveau = malloc(sizeof(Node)); | |
53 | + (*nouveau).l=lettre; | |
54 | + (*nouveau).fin_de_mot=0; | |
55 | + for (int i=0; i<size; i++) nouveau->lettres[i]=NULL; | |
56 | + *N = nouveau; | |
41 | 57 | } |
42 | 58 | |
43 | 59 | |
44 | -void ajout_alphab(Node ** pn, char * mot,int cpt,int i) | |
60 | + | |
61 | + | |
62 | + | |
63 | + | |
64 | +void ajout_alphab(Node ** pn, char * mot,int cpt) | |
45 | 65 | { |
46 | - while (mot[cpt] != '\0'){ | |
47 | - if ((*pn)==NULL){ | |
48 | - ajout(pn,mot[cpt]); | |
66 | + Node * tmp= *pn; | |
67 | + //printf("%d \n",cpt); | |
68 | + char lettre=mot[cpt]; | |
69 | + int pos = lettre - 'a'; | |
70 | + //printf("%c \n",lettre); | |
71 | + cpt++; | |
72 | + if (lettre == '\0'){ | |
73 | + (*tmp).fin_de_mot=1; | |
74 | + return; | |
49 | 75 | } |
50 | - while ((*pn)->lettres[i] != NULL){ | |
51 | - if ((*pn)->l == '?'){ | |
52 | - *pn = (*pn)->lettres[0]; | |
53 | - } | |
54 | - if ((*pn)->l != mot[cpt]){ | |
55 | - i++; | |
56 | - return ajout_alphab(&(*pn)->lettres[i],mot,cpt,i); | |
57 | - } | |
58 | - cpt++; | |
59 | - return ajout_alphab(&(*pn)->lettres[i],mot,cpt,i); | |
76 | + if (tmp->lettres[pos] == NULL) { | |
77 | + //printf("Rentré \n"); | |
78 | + ajout(&tmp->lettres[pos],lettre); | |
60 | 79 | } |
61 | - (*pn)->dernier ++ ; | |
62 | - ajout(&(*pn)->lettres[i],mot[cpt]); | |
63 | - *pn=(*pn)->lettres[i]; | |
64 | - cpt++; | |
65 | - } | |
66 | - (*pn)->fin_de_mot=1; | |
80 | + return ajout_alphab(&tmp->lettres[pos],mot,cpt); | |
81 | + | |
82 | + | |
67 | 83 | } |
68 | 84 | |
69 | 85 | |
86 | + | |
87 | + | |
88 | + | |
89 | + | |
70 | 90 | Node * charger_arbre(Node ** Arbre){ |
71 | - // FILE * dico; | |
72 | - //char mot[MAX_LETTRES]; | |
73 | - Node * debut = *Arbre; | |
74 | - //dico = fopen("test.txt","r"); | |
75 | - /* while (fscanf(dico,"%s",mot) == 1){ | |
76 | - ajout_alphab(Arbre,mot,0,0); | |
77 | - }*/ | |
78 | - ajout_alphab(Arbre,"camion",0,0); | |
79 | - *Arbre = debut; | |
80 | - ajout_alphab(Arbre,"casse",0,0); | |
81 | - *Arbre = debut; | |
82 | - //fclose(dico); | |
83 | - return *Arbre; | |
91 | + FILE * dico; | |
92 | + char mot[MAX_LETTRES]; | |
93 | + dico = fopen("text.txt","r"); | |
94 | + int i; | |
95 | + while ((i = fscanf(dico,"%s",mot)) == 1){ | |
96 | + ajout_alphab(Arbre,mot,0); | |
97 | + } | |
98 | + fclose(dico); | |
99 | + return *Arbre; | |
84 | 100 | } |
85 | 101 | |
102 | + | |
103 | + | |
104 | + | |
105 | + | |
106 | + | |
86 | 107 | void affichage_arbre(Node * Arbre) |
87 | 108 | { |
88 | - if (is_empty(Arbre)) return; | |
89 | - if (Arbre->lettres[0]== NULL) return; | |
90 | - for (int i=0; i<Arbre->dernier; i++){ | |
91 | - printf("%c -> %c",Arbre->l,Arbre->lettres[i]->l); | |
92 | - affichage_arbre(Arbre->lettres[i]); | |
93 | - if (Arbre->fin_de_mot) printf("fin de mot"); | |
94 | - } | |
95 | - | |
109 | + if (Arbre->l == '?' && is_leaf(Arbre)){ | |
110 | + printf("arbre vide \n"); | |
111 | + return; | |
112 | + } | |
113 | + for (int i=0; i<size; i++){ | |
114 | + if (Arbre->lettres[i] != NULL){ | |
115 | + printf("%c -> %c \n",Arbre->l,Arbre->lettres[i]->l); | |
116 | + affichage_arbre(Arbre->lettres[i]); | |
117 | + } | |
118 | + } | |
119 | + if (Arbre->fin_de_mot) printf("fin de mot \n"); | |
96 | 120 | } |
97 | 121 | |
98 | 122 | |
123 | + | |
124 | + | |
125 | + | |
126 | + | |
99 | 127 | void detruire_arbre(Node ** Arbre) |
100 | 128 | { |
101 | - for (int i=0; i<(*Arbre)->dernier ; i++){ | |
102 | - if (*Arbre!=NULL) detruire_arbre(&(*Arbre)->lettres[i]); | |
103 | - } | |
104 | - free(*Arbre); | |
129 | + printf("---------- \n"); | |
130 | + printf("%p \n",*Arbre); | |
131 | + if (is_empty(*Arbre)) return; | |
132 | + printf("%c \n",(*Arbre)->l); | |
133 | + for (int i=0; i<size; i++){ | |
134 | + detruire_arbre(&(*Arbre)->lettres[i]); | |
135 | + } | |
136 | + free(*Arbre); | |
137 | + printf("suppression \n"); | |
138 | + printf("%p \n",*Arbre); | |
105 | 139 | } |
106 | 140 | |
141 | + | |
142 | + | |
143 | + | |
144 | + | |
145 | + | |
107 | 146 | void initialisation(Node ** Arbre){ |
108 | - Node *nouveau = malloc(sizeof(struct node)); | |
109 | - (*nouveau).l='?'; | |
110 | - for (int i=0; i<27; i++) nouveau->lettres[i]=NULL; | |
111 | - nouveau->dernier=0; | |
112 | - *Arbre = nouveau; | |
147 | + Node *nouveau = malloc(sizeof(struct node)); | |
148 | + (*nouveau).l='?'; | |
149 | + for (int i=0; i<size; i++) nouveau->lettres[i] = NULL; | |
150 | + *Arbre = nouveau; | |
113 | 151 | } |
114 | 152 | |
153 | + | |
154 | + | |
155 | + | |
156 | + | |
115 | 157 | int main(){ |
116 | - Node * Arbre; | |
117 | - initialisation(&Arbre); | |
118 | - charger_arbre(&Arbre); | |
119 | - | |
120 | - affichage_arbre(Arbre); | |
121 | - detruire_arbre(&Arbre); | |
122 | - return 0; | |
158 | + Node * Arbre; | |
159 | + initialisation(&Arbre); | |
160 | + charger_arbre(&Arbre); | |
161 | + affichage_arbre(Arbre); | |
162 | + printf("Arbre fini \n"); | |
163 | + detruire_arbre(&Arbre); | |
164 | + affichage_arbre(Arbre); | |
165 | + return 0; | |
123 | 166 | } | ... | ... |