Commit 3b142eb18bebd70c7805ede5efd76267ad481a5f
1 parent
5a0b8312
Projet modifié
Showing
1 changed file
with
58 additions
and
1 deletions
Show diff stats
correcteur.c
@@ -13,6 +13,24 @@ typedef struct node { | @@ -13,6 +13,24 @@ typedef struct node { | ||
13 | int dernier; | 13 | int dernier; |
14 | } Node; | 14 | } Node; |
15 | 15 | ||
16 | +bool is_empty(struct node *tree) | ||
17 | +{ | ||
18 | + return tree==NULL; | ||
19 | +} | ||
20 | + | ||
21 | +bool is_leaf(struct node *tree) | ||
22 | +{ | ||
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); | ||
32 | +} | ||
33 | + | ||
16 | 34 | ||
17 | void ajout(Node **N, char mot) | 35 | void ajout(Node **N, char mot) |
18 | { | 36 | { |
@@ -27,6 +45,10 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) | @@ -27,6 +45,10 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) | ||
27 | { | 45 | { |
28 | int i = 0; | 46 | int i = 0; |
29 | while (mot[cpt] != '\0'){ | 47 | while (mot[cpt] != '\0'){ |
48 | + if ((*pn)==NULL){ | ||
49 | + ajout(pn,mot[cpt]); | ||
50 | + return; | ||
51 | + } | ||
30 | while ((*pn)->lettres[i] != NULL){ | 52 | while ((*pn)->lettres[i] != NULL){ |
31 | if (strcmp(&(*pn)->l,mot) != 0){ | 53 | if (strcmp(&(*pn)->l,mot) != 0){ |
32 | i++; | 54 | i++; |
@@ -34,17 +56,19 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) | @@ -34,17 +56,19 @@ void ajout_alphab(Node ** pn, char * mot,int cpt) | ||
34 | *pn=(*pn)->lettres[i]; | 56 | *pn=(*pn)->lettres[i]; |
35 | return ajout_alphab(pn,mot,cpt++); | 57 | return ajout_alphab(pn,mot,cpt++); |
36 | } | 58 | } |
59 | + (*pn)->dernier++; | ||
37 | ajout(&(*pn)->lettres[i],mot[cpt]); | 60 | ajout(&(*pn)->lettres[i],mot[cpt]); |
38 | *pn=(*pn)->lettres[i]; | 61 | *pn=(*pn)->lettres[i]; |
39 | cpt++; | 62 | cpt++; |
40 | } | 63 | } |
64 | + (*pn)->fin_de_mot=1; | ||
41 | } | 65 | } |
42 | 66 | ||
43 | 67 | ||
44 | Node * charger_arbre(Node ** Arbre){ | 68 | Node * charger_arbre(Node ** Arbre){ |
45 | FILE * dico; | 69 | FILE * dico; |
46 | char mot[MAX_LETTRES]; | 70 | char mot[MAX_LETTRES]; |
47 | - dico = fopen("words.txt","r"); | 71 | + dico = fopen("test.txt","r"); |
48 | while (fscanf(dico,"%s",mot) == 1){ | 72 | while (fscanf(dico,"%s",mot) == 1){ |
49 | ajout_alphab(Arbre,mot,0); | 73 | ajout_alphab(Arbre,mot,0); |
50 | } | 74 | } |
@@ -52,7 +76,40 @@ Node * charger_arbre(Node ** Arbre){ | @@ -52,7 +76,40 @@ Node * charger_arbre(Node ** Arbre){ | ||
52 | return *Arbre; | 76 | return *Arbre; |
53 | } | 77 | } |
54 | 78 | ||
79 | +void affichage_arbre(Node * Arbre) | ||
80 | +{ | ||
81 | + if (is_empty(Arbre)) return; | ||
82 | + if (Arbre->lettres[0]== NULL) return; | ||
83 | + for (int i=0; i<Arbre->dernier; i++){ | ||
84 | + printf("%c -> %c",Arbre->l,Arbre->lettres[i]->l); | ||
85 | + affichage_arbre(Arbre->lettres[i]); | ||
86 | + if (Arbre->fin_de_mot) printf("fin de mot"); | ||
87 | + } | ||
88 | + | ||
89 | +} | ||
90 | + | ||
91 | + | ||
92 | +void detruire_arbre(Node ** Arbre) | ||
93 | +{ | ||
94 | + for (int i=0; i<(*Arbre)->dernier ; i++){ | ||
95 | + if (*Arbre!=NULL) detruire_arbre(&(*Arbre)->lettres[i]); | ||
96 | + } | ||
97 | + free(*Arbre); | ||
98 | +} | ||
99 | + | ||
100 | +void initialisation(Node ** Arbre){ | ||
101 | + Node *nouveau = malloc(sizeof(struct node)); | ||
102 | + (*nouveau).l='?'; | ||
103 | + for (int i=0; i<27; i++) nouveau->lettres[i]=NULL; | ||
104 | + nouveau->dernier=0; | ||
105 | + *Arbre = nouveau; | ||
106 | +} | ||
55 | 107 | ||
56 | int main(){ | 108 | int main(){ |
109 | + Node * Arbre; | ||
110 | + initialisation(&Arbre); | ||
111 | + charger_arbre(&Arbre); | ||
112 | + affichage_arbre(Arbre); | ||
113 | + detruire_arbre(&Arbre); | ||
57 | return 0; | 114 | return 0; |
58 | } | 115 | } |