correcteur.c
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_LETTRES 30
typedef struct node {
char l;
struct node * lettres[27];
bool fin_de_mot;
int dernier;
} Node;
bool is_empty(struct node *tree)
{
return tree==NULL;
}
bool is_leaf(struct node *tree)
{
int cpt;
int i=0;
if (is_empty(tree)) return 1;
while (tree->lettres[i] != NULL){
cpt++;
i++;
}
if (cpt !=0) cpt=1;
return(!is_empty(tree) && !cpt);
}
void ajout(Node **N, char mot)
{
Node *nouveau = malloc(sizeof(struct node));
(*nouveau).l=mot;
for (int i=0; i<27; i++) nouveau->lettres[i]=NULL;
*N = nouveau;
}
void ajout_alphab(Node ** pn, char * mot,int cpt,int i)
{
while (mot[cpt] != '\0'){
if ((*pn)==NULL){
ajout(pn,mot[cpt]);
}
while ((*pn)->lettres[i] != NULL){
if ((*pn)->l == '?'){
*pn = (*pn)->lettres[0];
}
if ((*pn)->l != mot[cpt]){
i++;
return ajout_alphab(&(*pn)->lettres[i],mot,cpt,i);
}
cpt++;
return ajout_alphab(&(*pn)->lettres[i],mot,cpt,i);
}
(*pn)->dernier ++ ;
ajout(&(*pn)->lettres[i],mot[cpt]);
*pn=(*pn)->lettres[i];
cpt++;
}
(*pn)->fin_de_mot=1;
}
Node * charger_arbre(Node ** Arbre){
// FILE * dico;
//char mot[MAX_LETTRES];
Node * debut = *Arbre;
//dico = fopen("test.txt","r");
/* while (fscanf(dico,"%s",mot) == 1){
ajout_alphab(Arbre,mot,0,0);
}*/
ajout_alphab(Arbre,"camion",0,0);
*Arbre = debut;
ajout_alphab(Arbre,"casse",0,0);
*Arbre = debut;
//fclose(dico);
return *Arbre;
}
void affichage_arbre(Node * Arbre)
{
if (is_empty(Arbre)) return;
if (Arbre->lettres[0]== NULL) return;
for (int i=0; i<Arbre->dernier; i++){
printf("%c -> %c",Arbre->l,Arbre->lettres[i]->l);
affichage_arbre(Arbre->lettres[i]);
if (Arbre->fin_de_mot) printf("fin de mot");
}
}
void detruire_arbre(Node ** Arbre)
{
for (int i=0; i<(*Arbre)->dernier ; i++){
if (*Arbre!=NULL) detruire_arbre(&(*Arbre)->lettres[i]);
}
free(*Arbre);
}
void initialisation(Node ** Arbre){
Node *nouveau = malloc(sizeof(struct node));
(*nouveau).l='?';
for (int i=0; i<27; i++) nouveau->lettres[i]=NULL;
nouveau->dernier=0;
*Arbre = nouveau;
}
int main(){
Node * Arbre;
initialisation(&Arbre);
charger_arbre(&Arbre);
affichage_arbre(Arbre);
detruire_arbre(&Arbre);
return 0;
}