#include #include #include #include #define MAX_LETTRES 30 #define size 255 typedef struct node { char l; struct node * lettres[size]; bool fin_de_mot; } Node; bool is_empty(struct node *tree) { return tree==NULL; } bool is_leaf(struct node *tree) { 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 lettre) { 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) { Node * tmp= *pn; char lettre=mot[cpt]; int pos; pos=lettre; if (lettre>='A' && lettre <= 'Z') pos =lettre -'A'+'a'; if (lettre == '\0' || pos == 44 || pos == 46){ (*tmp).fin_de_mot=1; return; } if (tmp->lettres[pos] == NULL) { ajout(&tmp->lettres[pos],lettre); } cpt++; return ajout_alphab(&tmp->lettres[pos],mot,cpt); } Node * charger_arbre(Node ** Arbre){ FILE * dico; char mot[MAX_LETTRES]; dico = fopen("words.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)){ 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) { if (is_empty(*Arbre)) return; for (int i=0; ilettres[i]); } free(*Arbre); *Arbre=NULL; } void initialisation(Node ** Arbre){ Node *nouveau = malloc(sizeof(struct node)); (*nouveau).l='?'; (*nouveau).fin_de_mot=0; for (int i=0; ilettres[i] = NULL; *Arbre = nouveau;} int comparaison(Node ** pn, char * mot,int cpt,int * erreur) { char lettre=mot[cpt]; int pos = lettre ; if (lettre>='A' && lettre <= 'Z') pos =lettre -'A'+'a'; cpt++; if (lettre == '\0' || pos == 44 || pos == 46){ if((*pn)->fin_de_mot==0){ (*erreur)++; } return (*erreur); } if (lettre == '?' || lettre == '!' || lettre == ':' || lettre == ';') return 0; if ((*pn)->lettres[pos] == NULL) { (*erreur)++; return (*erreur); } return comparaison(&(*pn)->lettres[pos],mot,cpt,erreur); } void test_erreur(Node * Dico){ FILE * texte; int * erreur=malloc(sizeof(int)); (*erreur)=0; char mot[MAX_LETTRES]; texte = fopen("test.txt","r"); int i; while ((i = fscanf(texte,"%s",mot)) == 1){ (*erreur)=comparaison(&Dico,mot,0,erreur); } if ((*erreur)==0 || (*erreur)==1 ) printf("%d erreur",(*erreur)); else printf("%d erreurs",(*erreur)); fclose(texte); } int main(){ Node * Arbre; initialisation(&Arbre); charger_arbre(&Arbre); //affichage_arbre(Arbre); printf("Arbre fini \n"); test_erreur(Arbre); detruire_arbre(&Arbre); //affichage_arbre(Arbre); return 0; }