Commit 804ad89aeada0c81c66de5ff74f2dfc0619af1bb

Authored by Emilie10
1 parent 35fa2c2f

projet

Showing 1 changed file with 154 additions and 0 deletions   Show diff stats
projetfinal/projet.c 0 → 100644
... ... @@ -0,0 +1,154 @@
  1 +
  2 +#include "projet.h"
  3 +
  4 +
  5 +void init_dictionnaire(struct dictionnaire* D){
  6 + D->tetes = NULL ;
  7 + D->nbelem = 0 ;
  8 +}
  9 +
  10 +void cons_dictionnaire(struct dictionnaire* D, char* fichier){
  11 + char word[100] ;
  12 + FILE * f = fopen(fichier, "r");
  13 + if(f == NULL)
  14 + exit(1) ;
  15 +
  16 + while(fscanf(f, "%s", word) != EOF){
  17 + ajout_mot(D,word) ;
  18 + }
  19 +
  20 +}
  21 +
  22 +
  23 +void ajout_mot(struct dictionnaire* ptrD, char* MOT){
  24 + int tailleMot = strlen(MOT) ;
  25 + struct noeud* tmp = ptrD->tetes ;
  26 + int i = 0, j = 0, position = 0;
  27 + bool sortie = false;
  28 + if(tmp != NULL){
  29 + while(position < ptrD->nbelem && tmp[position].valeur != MOT[0]){
  30 + position++ ;
  31 + }
  32 + if(position < ptrD->nbelem){
  33 + tmp = &(tmp[position]) ;
  34 + while(!sortie){
  35 + if(tmp->valeur == MOT[i]){
  36 + i++ ;
  37 + if(i < tailleMot){
  38 + j = 0 ;
  39 + struct noeud* liste = tmp->liste_noeud ;
  40 + while(j< tmp->nbfils && liste[j].valeur != MOT[i])
  41 + j++ ;
  42 + if(j < tmp->nbfils){
  43 + tmp = &(liste[j]) ;
  44 + }
  45 + else
  46 + sortie = true ;
  47 + }
  48 + else{
  49 + tmp->complet = true ;
  50 + sortie = true ;
  51 + }
  52 + }
  53 + else
  54 + sortie = true ;
  55 + }
  56 + }
  57 + }
  58 + for(j = i ; j < tailleMot ; j++){
  59 + if(j == 0 && position >= ptrD->nbelem){
  60 + ptrD->nbelem +=1 ;
  61 + ptrD->tetes = realloc(ptrD->tetes , ptrD->nbelem*sizeof(struct noeud)) ;
  62 + ptrD->tetes [ptrD->nbelem-1].valeur = MOT[j] ;
  63 + ptrD->tetes [ptrD->nbelem-1].nbfils = 0 ;
  64 + ptrD->tetes [ptrD->nbelem-1].liste_noeud = NULL ;
  65 + if(j == tailleMot-1)
  66 + ptrD->tetes[ptrD->nbelem-1].complet= true ;
  67 + else
  68 + ptrD->tetes[ptrD->nbelem-1].complet = false ;
  69 + tmp = &ptrD->tetes[ptrD->nbelem-1];
  70 + }
  71 + else{
  72 + tmp->nbfils++ ;
  73 + tmp->liste_noeud = realloc(tmp->liste_noeud, (tmp->nbfils)*sizeof(struct noeud)) ;
  74 + tmp->liste_noeud[tmp->nbfils-1].valeur = MOT[j] ;
  75 + tmp->liste_noeud[tmp->nbfils-1].nbfils = 0 ;
  76 + tmp->liste_noeud[tmp->nbfils-1].liste_noeud = NULL ;
  77 + if(j == tailleMot-1)
  78 + tmp->liste_noeud[tmp->nbfils-1].complet = true;
  79 + else
  80 + tmp->liste_noeud[tmp->nbfils-1].complet = false ;
  81 + tmp = &tmp->liste_noeud[tmp->nbfils-1] ;
  82 + }
  83 + }
  84 +
  85 +
  86 +}
  87 +
  88 +bool appartient_mot(struct dictionnaire *D, char * MOT){
  89 + if(D->tetes==NULL) return false ;
  90 + int length = strlen(MOT) ;
  91 + int i,j;
  92 +
  93 + bool exit = false , found = false ;
  94 + struct noeud N ;
  95 +
  96 + i = 0;
  97 + while(i < D->nbelem && D->tetes[i].valeur != MOT[0] )
  98 + i++ ;
  99 + if(i==D->nbelem) return false ;
  100 + if(length == 1 && D->tetes[i].complet )
  101 + found = true;
  102 + N = D->tetes[i] ;
  103 + i = 1 ;
  104 +
  105 + while(!exit && !found){
  106 + j = 0 ;
  107 + while(j < N.nbfils && N.liste_noeud[j].valeur != MOT[i]){
  108 + j++ ;
  109 + }
  110 + if(j < N.nbfils){
  111 + i++ ;
  112 + if(i>=length && N.liste_noeud[j].complet){
  113 + found = true ;
  114 + }
  115 + else{
  116 + if(i>=length)
  117 + exit = true ;
  118 + else
  119 + N = N.liste_noeud[j] ;
  120 + }
  121 + }
  122 + else{
  123 + exit = true ;
  124 + }
  125 +
  126 +
  127 + }
  128 +
  129 + return found ;
  130 +}
  131 +void correction(struct dictionnaire *D, char *phrase){
  132 + FILE *f = fopen(phrase, "r");
  133 + if(f!=NULL){
  134 + int nbError = 0;
  135 + char mot[25];
  136 + while(fscanf(f,"%s",mot)!=EOF){
  137 + if(!appartient_mot(D,mot)){
  138 + printf("il y a une erreur\n");
  139 + printf("------------------------------------------------------------------\n");
  140 + printf("%s\n",mot);
  141 + printf("------------------------------------------------------------------\n");
  142 + nbError++;
  143 + }
  144 +
  145 + }
  146 + printf("La phrase contenait : %d erreurs\n", nbError);
  147 + }
  148 +
  149 +}
  150 +
  151 +
  152 +
  153 +
  154 +
... ...