Blame view

projetfinalaccent2.c 6.83 KB
d75f444a   pvernier   ajout de la desal...
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  #include <stdio.h>

  #include <stdlib.h>

  #include <wchar.h>

  #include <locale.h>

  

  #define A 256

  

  struct node {

    wchar_t lettre;

    struct cell* listeFils;

  };

  

  struct cell {

    struct node* arbre;

    struct cell* arbreSuivant;

  };

  

  void lien_listeFils(struct cell** pL) {

    struct cell* p;

    p = malloc(sizeof(struct cell));

   

    (*pL)->arbre->listeFils = p;

  }

  

  void initialisation_tab_arbre(struct node tab[]) {

    int i = 0;

    for(wchar_t u = 'a'; u < A; u++) {

      tab[i].lettre = u; //ajout lettres minuscules

      tab[i].listeFils = NULL;

      i++;

    }

  }

  

  void ajout_tete(wchar_t elem, struct cell** pL) {

    struct cell* p;

    p = malloc(sizeof(struct cell));

    p->arbre = malloc(sizeof(struct node));

    p->arbre->listeFils = NULL;

    p->arbre->lettre = elem;

    p->arbreSuivant = *pL;

    *pL = p;

  }

  

  struct cell ** insertion(wchar_t elem, struct cell** pL) {

  	if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {

  		ajout_tete(elem, pL);

  		return &(*pL)->arbre->listeFils;

  	}

  	else if((*pL)->arbre->lettre == elem) {

  		return &(*pL)->arbre->listeFils;

  	}

  	else {

  		return insertion(elem, &(*pL)->arbreSuivant);

  	}

  }

  

  int indice_lettre(struct node tab_arbre_prcp[], wchar_t lettre)

  {

    int i = 1;

    while(i > 0 && i < A){  

     if(lettre == tab_arbre_prcp[i].lettre ){

       return i;

     }  

     i++;

    }

  return 0;

  }

  

  void remplir_dico(FILE* fd, struct node tab_arbre_prcp[]) {

  

    struct cell** localisationArbre = NULL; 

    int cptmot = 0, indice = 0;

    wchar_t motLu[50];

    while(fwscanf(fd, L"%ls", motLu)==1) {

    	//wprintf(L"mot lu : %ls\n", motLu);

    	int estUneLettre = 1;

  	int i = 0;

      cptmot += 1;

       if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {

         localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;

       }

       else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {

         localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;

       }

       else if(motLu[0] > 'z') {

         indice = indice_lettre(tab_arbre_prcp, motLu[0]);

         localisationArbre = &tab_arbre_prcp[indice].listeFils;

         if(indice == 0) {

  	 wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");

  	 wprintf(L"Mot : %ls incorrect\n", motLu);

  	 estUneLettre = 0;

         }

       }

       else {

         wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");

         wprintf(L"Mot : %ls incorrect\n", motLu);

         estUneLettre = 0;

       }

       while((motLu[i] != '\0') && (estUneLettre == 1)) {

    	i += 1;

    	//wprintf(L"lettre lue %d\n", motLu[i]);

  	localisationArbre = insertion(motLu[i], localisationArbre);

       }

     }

  	wprintf(L"\n");

  	fclose(fd);

    	wprintf(L"%d mots inseres dans le dictionnaire.\n", cptmot);

  }

  

  struct cell** test_mot(wchar_t mot, struct cell** localisation, int* verif) {

  

    if((*localisation == NULL) || (*localisation)->arbre->lettre > mot) {

      *verif = 0;

      return NULL;

    }

    if((*localisation)->arbre->lettre == mot) {

      return &(*localisation)->arbre->listeFils;

    }

    else {

      return test_mot(mot, &(*localisation)->arbreSuivant, verif);

    }

  }

  

  void correction_txt(FILE* fd, struct node tab_arbre_prcp[]) {

    struct cell** localisationArbre = NULL;

    int verif;

    int indice = 0;

    wchar_t motLu[50];

    while(fwscanf(fd, L"%ls", motLu)==1) {

        verif = 1;

        int i = 0;

        if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {

          localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;

        }

        else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {

          localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;

        }

        else if(motLu[0] > 'z') {

         indice = indice_lettre(tab_arbre_prcp, motLu[0]);

         localisationArbre = &tab_arbre_prcp[indice].listeFils;

         if(indice == 0) {

  	wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");

          verif = 0;

         }

        }

        else {

          wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");

          verif = 0;

        }

        while((verif == 1) && (motLu[i] != '\0')) {

          i += 1;

          localisationArbre = test_mot(motLu[i], localisationArbre, &verif);

        }

        if(verif == 0) wprintf(L"Mot %ls non present dans le dicitonnaire.\n", motLu);

      }

      fclose(fd);

  } 

  

  void correction_mot(struct node tab_arbre_prcp[]) {

    struct cell** localisationArbre = NULL;

    int verif;

    int indice = 0;

    wchar_t motLu[50];

    wprintf(L"Entrez un mot ou une phrase a corriger : \n(0 pour quitter)\n");

    while(wscanf(L"%ls", motLu)==1) {

        if(motLu[0]=='0') return;

        verif = 1;

        int i = 0;

        if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {

          localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;

        }

        else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {

          localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;

        }

        else if(motLu[0] > 'z') {

         indice = indice_lettre(tab_arbre_prcp, motLu[0]);

         localisationArbre = &tab_arbre_prcp[indice].listeFils;

         if(indice == 0) {

  	wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");

          verif = 0;

         }

        }

        else {

          wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");

          verif = 0;

        }

        while((verif == 1) && (motLu[i] != '\0')) {

          i += 1;

          wprintf(L"lettre lue %d\n", motLu[i]);

          localisationArbre = test_mot(motLu[i], localisationArbre, &verif);

        }

        if(verif == 0) wprintf(L"Mot : %ls non present dans le dicitonnaire.\n", motLu);

        else wprintf(L"Mot : %ls correct\n", motLu);

      }

  } 

  

  /*void supp_tete(struct cell** pL) {

    struct cell* p;

    p = *pL;

    *pL = (*pL) -> arbreSuivant;

    free(p);

  }*/

  

  void desalocationArbre(struct cell** pL) {

    

    if ((*pL) == NULL) return;  

    

    desalocationArbre(&(*pL)-> arbreSuivant);

    desalocationArbre(&(*pL)-> arbre -> listeFils);

    

    free((*pL) -> arbre);

    free(*pL);  

  }

  

  void desalocationTableauArbre(struct node tab_arbre[]){

    int i = 0;

    for(wchar_t u = 'a'; u < A; u++) {

  

        desalocationArbre(&tab_arbre[i].listeFils);

  

      i++;

    }

  }

  

  int main(int argc, char* argv[]) {

  

   

    setlocale(LC_ALL, "");

    FILE* dico = NULL;

    FILE* txt = NULL;

    struct node tab_arbre[A];

    //struct node * a;

    

    if(argc>2) {

      dico = fopen(argv[1], "r");

      txt = fopen(argv[2], "r");

    }

    else {

      dico = NULL;

      txt = NULL;

    }

    if ((dico == NULL) || (txt == NULL)) {

          wprintf(L"Erreur : il manque un ou plusieurs fichiers !\n");

          return 1;

      }

    initialisation_tab_arbre(tab_arbre);

    remplir_dico(dico, tab_arbre); //on suppose qu'il n'y a pas d'accents dans le dictionnaire

    correction_txt(txt, tab_arbre);

    correction_mot(tab_arbre);

    desalocationTableauArbre(tab_arbre); 

  

    wprintf(L"fini\n");

    

  

    return 0;

  }