diff --git a/correcteur.c b/correcteur.c index d39e28c..94e38c1 100644 --- a/correcteur.c +++ b/correcteur.c @@ -4,120 +4,163 @@ #include #define MAX_LETTRES 30 +#define size 26 typedef struct node { char l; - struct node * lettres[27]; + struct node * lettres[size]; 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); + int cpt=1; + if (is_empty(tree)) return 1; + for (int i=0; ilettres[i] == NULL){ + cpt++; + } + } + if (cpt == size) cpt=1; + if (cpt != size) cpt=0; + return cpt ; } -void ajout(Node **N, char mot) + + + + +void ajout(Node **N, char lettre) { - Node *nouveau = malloc(sizeof(struct node)); - (*nouveau).l=mot; - for (int i=0; i<27; i++) nouveau->lettres[i]=NULL; - *N = nouveau; + Node *nouveau = malloc(sizeof(Node)); + (*nouveau).l=lettre; + (*nouveau).fin_de_mot=0; + for (int i=0; ilettres[i]=NULL; + *N = nouveau; } -void ajout_alphab(Node ** pn, char * mot,int cpt,int i) + + + + +void ajout_alphab(Node ** pn, char * mot,int cpt) { - while (mot[cpt] != '\0'){ - if ((*pn)==NULL){ - ajout(pn,mot[cpt]); + Node * tmp= *pn; + //printf("%d \n",cpt); + char lettre=mot[cpt]; + int pos = lettre - 'a'; + //printf("%c \n",lettre); + cpt++; + if (lettre == '\0'){ + (*tmp).fin_de_mot=1; + return; } - 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); + if (tmp->lettres[pos] == NULL) { + //printf("Rentré \n"); + ajout(&tmp->lettres[pos],lettre); } - (*pn)->dernier ++ ; - ajout(&(*pn)->lettres[i],mot[cpt]); - *pn=(*pn)->lettres[i]; - cpt++; - } - (*pn)->fin_de_mot=1; + return ajout_alphab(&tmp->lettres[pos],mot,cpt); + + } + + + + 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; + FILE * dico; + char mot[MAX_LETTRES]; + dico = fopen("text.txt","r"); + int i; + while ((i = fscanf(dico,"%s",mot)) == 1){ + ajout_alphab(Arbre,mot,0); + } + fclose(dico); + return *Arbre; } + + + + + void affichage_arbre(Node * Arbre) { - if (is_empty(Arbre)) return; - if (Arbre->lettres[0]== NULL) return; - for (int i=0; idernier; i++){ - printf("%c -> %c",Arbre->l,Arbre->lettres[i]->l); - affichage_arbre(Arbre->lettres[i]); - if (Arbre->fin_de_mot) printf("fin de mot"); - } - + if (Arbre->l == '?' && is_leaf(Arbre)){ + printf("arbre vide \n"); + return; + } + for (int i=0; ilettres[i] != NULL){ + printf("%c -> %c \n",Arbre->l,Arbre->lettres[i]->l); + affichage_arbre(Arbre->lettres[i]); + } + } + if (Arbre->fin_de_mot) printf("fin de mot \n"); } + + + + void detruire_arbre(Node ** Arbre) { - for (int i=0; i<(*Arbre)->dernier ; i++){ - if (*Arbre!=NULL) detruire_arbre(&(*Arbre)->lettres[i]); - } - free(*Arbre); + printf("---------- \n"); + printf("%p \n",*Arbre); + if (is_empty(*Arbre)) return; + printf("%c \n",(*Arbre)->l); + for (int i=0; ilettres[i]); + } + free(*Arbre); + printf("suppression \n"); + printf("%p \n",*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; + Node *nouveau = malloc(sizeof(struct node)); + (*nouveau).l='?'; + for (int i=0; ilettres[i] = NULL; + *Arbre = nouveau; } + + + + int main(){ - Node * Arbre; - initialisation(&Arbre); - charger_arbre(&Arbre); - - affichage_arbre(Arbre); - detruire_arbre(&Arbre); - return 0; + Node * Arbre; + initialisation(&Arbre); + charger_arbre(&Arbre); + affichage_arbre(Arbre); + printf("Arbre fini \n"); + detruire_arbre(&Arbre); + affichage_arbre(Arbre); + return 0; } -- libgit2 0.21.2