Commit d75f444a580c66e9ff66319d149c59e81a47295b

Authored by pvernier
1 parent 43039ae2

ajout de la desalocation

Showing 1 changed file with 256 additions and 0 deletions   Show diff stats
projetfinalaccent2.c 0 → 100644
... ... @@ -0,0 +1,256 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <wchar.h>
  4 +#include <locale.h>
  5 +
  6 +#define A 256
  7 +
  8 +struct node {
  9 + wchar_t lettre;
  10 + struct cell* listeFils;
  11 +};
  12 +
  13 +struct cell {
  14 + struct node* arbre;
  15 + struct cell* arbreSuivant;
  16 +};
  17 +
  18 +void lien_listeFils(struct cell** pL) {
  19 + struct cell* p;
  20 + p = malloc(sizeof(struct cell));
  21 +
  22 + (*pL)->arbre->listeFils = p;
  23 +}
  24 +
  25 +void initialisation_tab_arbre(struct node tab[]) {
  26 + int i = 0;
  27 + for(wchar_t u = 'a'; u < A; u++) {
  28 + tab[i].lettre = u; //ajout lettres minuscules
  29 + tab[i].listeFils = NULL;
  30 + i++;
  31 + }
  32 +}
  33 +
  34 +void ajout_tete(wchar_t elem, struct cell** pL) {
  35 + struct cell* p;
  36 + p = malloc(sizeof(struct cell));
  37 + p->arbre = malloc(sizeof(struct node));
  38 + p->arbre->listeFils = NULL;
  39 + p->arbre->lettre = elem;
  40 + p->arbreSuivant = *pL;
  41 + *pL = p;
  42 +}
  43 +
  44 +struct cell ** insertion(wchar_t elem, struct cell** pL) {
  45 + if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
  46 + ajout_tete(elem, pL);
  47 + return &(*pL)->arbre->listeFils;
  48 + }
  49 + else if((*pL)->arbre->lettre == elem) {
  50 + return &(*pL)->arbre->listeFils;
  51 + }
  52 + else {
  53 + return insertion(elem, &(*pL)->arbreSuivant);
  54 + }
  55 +}
  56 +
  57 +int indice_lettre(struct node tab_arbre_prcp[], wchar_t lettre)
  58 +{
  59 + int i = 1;
  60 + while(i > 0 && i < A){
  61 + if(lettre == tab_arbre_prcp[i].lettre ){
  62 + return i;
  63 + }
  64 + i++;
  65 + }
  66 +return 0;
  67 +}
  68 +
  69 +void remplir_dico(FILE* fd, struct node tab_arbre_prcp[]) {
  70 +
  71 + struct cell** localisationArbre = NULL;
  72 + int cptmot = 0, indice = 0;
  73 + wchar_t motLu[50];
  74 + while(fwscanf(fd, L"%ls", motLu)==1) {
  75 + //wprintf(L"mot lu : %ls\n", motLu);
  76 + int estUneLettre = 1;
  77 + int i = 0;
  78 + cptmot += 1;
  79 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  80 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  81 + }
  82 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  83 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  84 + }
  85 + else if(motLu[0] > 'z') {
  86 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  87 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  88 + if(indice == 0) {
  89 + wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");
  90 + wprintf(L"Mot : %ls incorrect\n", motLu);
  91 + estUneLettre = 0;
  92 + }
  93 + }
  94 + else {
  95 + wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");
  96 + wprintf(L"Mot : %ls incorrect\n", motLu);
  97 + estUneLettre = 0;
  98 + }
  99 + while((motLu[i] != '\0') && (estUneLettre == 1)) {
  100 + i += 1;
  101 + //wprintf(L"lettre lue %d\n", motLu[i]);
  102 + localisationArbre = insertion(motLu[i], localisationArbre);
  103 + }
  104 + }
  105 + wprintf(L"\n");
  106 + fclose(fd);
  107 + wprintf(L"%d mots inseres dans le dictionnaire.\n", cptmot);
  108 +}
  109 +
  110 +struct cell** test_mot(wchar_t mot, struct cell** localisation, int* verif) {
  111 +
  112 + if((*localisation == NULL) || (*localisation)->arbre->lettre > mot) {
  113 + *verif = 0;
  114 + return NULL;
  115 + }
  116 + if((*localisation)->arbre->lettre == mot) {
  117 + return &(*localisation)->arbre->listeFils;
  118 + }
  119 + else {
  120 + return test_mot(mot, &(*localisation)->arbreSuivant, verif);
  121 + }
  122 +}
  123 +
  124 +void correction_txt(FILE* fd, struct node tab_arbre_prcp[]) {
  125 + struct cell** localisationArbre = NULL;
  126 + int verif;
  127 + int indice = 0;
  128 + wchar_t motLu[50];
  129 + while(fwscanf(fd, L"%ls", motLu)==1) {
  130 + verif = 1;
  131 + int i = 0;
  132 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  133 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  134 + }
  135 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  136 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  137 + }
  138 + else if(motLu[0] > 'z') {
  139 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  140 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  141 + if(indice == 0) {
  142 + wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");
  143 + verif = 0;
  144 + }
  145 + }
  146 + else {
  147 + wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");
  148 + verif = 0;
  149 + }
  150 + while((verif == 1) && (motLu[i] != '\0')) {
  151 + i += 1;
  152 + localisationArbre = test_mot(motLu[i], localisationArbre, &verif);
  153 + }
  154 + if(verif == 0) wprintf(L"Mot %ls non present dans le dicitonnaire.\n", motLu);
  155 + }
  156 + fclose(fd);
  157 +}
  158 +
  159 +void correction_mot(struct node tab_arbre_prcp[]) {
  160 + struct cell** localisationArbre = NULL;
  161 + int verif;
  162 + int indice = 0;
  163 + wchar_t motLu[50];
  164 + wprintf(L"Entrez un mot ou une phrase a corriger : \n(0 pour quitter)\n");
  165 + while(wscanf(L"%ls", motLu)==1) {
  166 + if(motLu[0]=='0') return;
  167 + verif = 1;
  168 + int i = 0;
  169 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  170 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  171 + }
  172 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  173 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  174 + }
  175 + else if(motLu[0] > 'z') {
  176 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  177 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  178 + if(indice == 0) {
  179 + wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");
  180 + verif = 0;
  181 + }
  182 + }
  183 + else {
  184 + wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");
  185 + verif = 0;
  186 + }
  187 + while((verif == 1) && (motLu[i] != '\0')) {
  188 + i += 1;
  189 + wprintf(L"lettre lue %d\n", motLu[i]);
  190 + localisationArbre = test_mot(motLu[i], localisationArbre, &verif);
  191 + }
  192 + if(verif == 0) wprintf(L"Mot : %ls non present dans le dicitonnaire.\n", motLu);
  193 + else wprintf(L"Mot : %ls correct\n", motLu);
  194 + }
  195 +}
  196 +
  197 +/*void supp_tete(struct cell** pL) {
  198 + struct cell* p;
  199 + p = *pL;
  200 + *pL = (*pL) -> arbreSuivant;
  201 + free(p);
  202 +}*/
  203 +
  204 +void desalocationArbre(struct cell** pL) {
  205 +
  206 + if ((*pL) == NULL) return;
  207 +
  208 + desalocationArbre(&(*pL)-> arbreSuivant);
  209 + desalocationArbre(&(*pL)-> arbre -> listeFils);
  210 +
  211 + free((*pL) -> arbre);
  212 + free(*pL);
  213 +}
  214 +
  215 +void desalocationTableauArbre(struct node tab_arbre[]){
  216 + int i = 0;
  217 + for(wchar_t u = 'a'; u < A; u++) {
  218 +
  219 + desalocationArbre(&tab_arbre[i].listeFils);
  220 +
  221 + i++;
  222 + }
  223 +}
  224 +
  225 +int main(int argc, char* argv[]) {
  226 +
  227 +
  228 + setlocale(LC_ALL, "");
  229 + FILE* dico = NULL;
  230 + FILE* txt = NULL;
  231 + struct node tab_arbre[A];
  232 + //struct node * a;
  233 +
  234 + if(argc>2) {
  235 + dico = fopen(argv[1], "r");
  236 + txt = fopen(argv[2], "r");
  237 + }
  238 + else {
  239 + dico = NULL;
  240 + txt = NULL;
  241 + }
  242 + if ((dico == NULL) || (txt == NULL)) {
  243 + wprintf(L"Erreur : il manque un ou plusieurs fichiers !\n");
  244 + return 1;
  245 + }
  246 + initialisation_tab_arbre(tab_arbre);
  247 + remplir_dico(dico, tab_arbre); //on suppose qu'il n'y a pas d'accents dans le dictionnaire
  248 + correction_txt(txt, tab_arbre);
  249 + correction_mot(tab_arbre);
  250 + desalocationTableauArbre(tab_arbre);
  251 +
  252 + wprintf(L"fini\n");
  253 +
  254 +
  255 + return 0;
  256 +}
... ...