Commit c3dc6df14bb84b59f84ea7ec0f1279b2cac30c5d

Authored by tvieuble
1 parent 3cc6463a

ajout projetfinalaccent2.c

Showing 1 changed file with 214 additions and 0 deletions   Show diff stats
projetfinalaccent2.c 0 → 100644
... ... @@ -0,0 +1,214 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <wchar.h>
  4 +#include <locale.h>
  5 +
  6 +#include "fonctions.h"
  7 +
  8 +#define A 256
  9 +
  10 +void ajout_tete(wchar_t elem, struct cell** pL) {
  11 + struct cell* p;
  12 + p = malloc(sizeof(struct cell));
  13 + p->arbre = malloc(sizeof(struct node));
  14 + p->arbre->listeFils = NULL;
  15 + p->arbre->lettre = elem;
  16 + p->arbreSuivant = *pL;
  17 + *pL = p;
  18 +}
  19 +
  20 +struct cell ** insertion(wchar_t elem, struct cell** pL) {
  21 + if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
  22 + ajout_tete(elem, pL);
  23 + return &(*pL)->arbre->listeFils;
  24 + }
  25 + else if((*pL)->arbre->lettre == elem) {
  26 + return &(*pL)->arbre->listeFils;
  27 + }
  28 + else {
  29 + return insertion(elem, &(*pL)->arbreSuivant);
  30 + }
  31 +}
  32 +
  33 +void initialisation_tab_arbre(struct node tab[]) {
  34 + int i = 0;
  35 + for(wchar_t u = 'a'; u < A; u++) {
  36 + tab[i].lettre = u; //ajout lettres minuscules
  37 + tab[i].listeFils = NULL;
  38 + i++;
  39 + }
  40 + tab[i].lettre = '?';
  41 + tab[i+1].lettre = '!';
  42 + tab[i+2].lettre = '\'';
  43 + tab[i+3].lettre = '.'; //Ajout des caractères de ponctuation par défau
  44 + tab[i+4].lettre = ':';
  45 + tab[i+5].lettre = ';';
  46 + for(int j = 0; j <= 5; j++) {
  47 + tab[i+j].listeFils = NULL;
  48 + insertion('\0', &(tab[i+j].listeFils));
  49 + }
  50 +}
  51 +
  52 +int indice_lettre(struct node tab_arbre_prcp[], wchar_t lettre) {
  53 + int i = 0;
  54 + while(i >= 0 && i < A){
  55 + if(lettre == tab_arbre_prcp[i].lettre ){
  56 + return i;
  57 + }
  58 + i++;
  59 + }
  60 +return -1;
  61 +}
  62 +
  63 +void remplir_dico(FILE* fd, struct node tab_arbre_prcp[]) {
  64 +
  65 + struct cell** localisationArbre = NULL;
  66 + int cptmot = 0, indice = 0;
  67 + wchar_t motLu[50];
  68 + while(fwscanf(fd, L"%ls", motLu)==1) {
  69 + int estUneLettre = 1;
  70 + int i = 0;
  71 + cptmot += 1;
  72 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  73 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  74 + }
  75 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  76 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  77 + }
  78 + else if((motLu[0] < 'A') || (motLu[0] > 'z')) {
  79 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  80 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  81 + if(indice == -1) {
  82 + wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");
  83 + wprintf(L"Mot : '%ls' incorrect\n", motLu);
  84 + estUneLettre = 0;
  85 + }
  86 + }
  87 + else {
  88 + wprintf(L"Erreur remplissage dico : L'un des caracteres n'est pas une lettre\n");
  89 + wprintf(L"Mot : '%ls' incorrect\n", motLu);
  90 + estUneLettre = 0;
  91 + }
  92 + while((motLu[i] != '\0') && (estUneLettre == 1)) {
  93 + i += 1;
  94 + localisationArbre = insertion(motLu[i], localisationArbre);
  95 + }
  96 + }
  97 + wprintf(L"\n");
  98 + fclose(fd);
  99 + wprintf(L"%d mots inseres dans le dictionnaire.\n", cptmot);
  100 +}
  101 +
  102 +struct cell ** test_mot(wchar_t mot, struct cell** localisation, int* verif) {
  103 +
  104 + if((*localisation == NULL) || (*localisation)->arbre->lettre > mot) {
  105 + *verif = 0;
  106 + return NULL;
  107 + }
  108 + if((*localisation)->arbre->lettre == mot) {
  109 + return &(*localisation)->arbre->listeFils;
  110 + }
  111 + else {
  112 + return test_mot(mot, &(*localisation)->arbreSuivant, verif);
  113 + }
  114 +}
  115 +
  116 +void correction_txt(FILE* fd, struct node tab_arbre_prcp[]) {
  117 + struct cell** localisationArbre = NULL;
  118 + int verif;
  119 + int indice = 0, cptfaute = 0;
  120 + wchar_t motLu[50];
  121 + wprintf(L"\nCorrection du fichier texte : \n\n");
  122 + while(fwscanf(fd, L"%ls", motLu)==1) {
  123 + verif = 1;
  124 + int i = 0;
  125 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  126 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  127 + }
  128 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  129 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  130 + }
  131 + else if((motLu[0] < 'A') || (motLu[0] > 'z')) {
  132 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  133 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  134 + if(indice == -1) {
  135 + wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");
  136 + verif = 0;
  137 + }
  138 + }
  139 + else {
  140 + wprintf(L"Erreur correction txt : L'un des caracteres n'est pas une lettre\n");
  141 + verif = 0;
  142 + }
  143 + while((verif == 1) && (motLu[i] != '\0')) {
  144 + i += 1;
  145 + if((motLu[i] == '.') || (motLu[i] == ',') || (motLu[i] == ';') || (motLu[i] == ':') || (motLu[i] == '!') || (motLu[i] == '?')) motLu[i] = '\0'; //prendre en compte les fins de phrase
  146 + localisationArbre = test_mot(motLu[i], localisationArbre, &verif);
  147 + }
  148 + if(verif == 0) {
  149 + wprintf(L"Mot '%ls' non present dans le dicitonnaire.\n", motLu);
  150 + cptfaute += 1;
  151 + }
  152 + }
  153 + wprintf(L"\nLe texte comporte %d faute(s)\n", cptfaute);
  154 + fclose(fd);
  155 +}
  156 +
  157 +void correction_mot(struct node tab_arbre_prcp[]) {
  158 + struct cell** localisationArbre = NULL;
  159 + int verif;
  160 + int indice = 0;
  161 + wchar_t motLu[50];
  162 + wprintf(L"\nEntrez un mot ou une phrase a corriger : \n(0 pour quitter)\n\n");
  163 + while(wscanf(L"%ls", motLu)==1) {
  164 + if(motLu[0]=='0') return;
  165 + verif = 1;
  166 + int i = 0;
  167 + if((motLu[0] >= 'A') && (motLu[0] <= 'Z')) {
  168 + localisationArbre = &tab_arbre_prcp[motLu[0]-65].listeFils;
  169 + }
  170 + else if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  171 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  172 + }
  173 + else if((motLu[0] < 'A') || (motLu[0] > 'z')) {
  174 + indice = indice_lettre(tab_arbre_prcp, motLu[0]);
  175 + localisationArbre = &tab_arbre_prcp[indice].listeFils;
  176 + if(indice == -1) {
  177 + wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");
  178 + verif = 0;
  179 + }
  180 + }
  181 + else {
  182 + wprintf(L"Erreur correction mot : L'un des caracteres n'est pas une lettre\n");
  183 + verif = 0;
  184 + }
  185 + while((verif == 1) && (motLu[i] != '\0')) {
  186 + i += 1;
  187 + if((motLu[i] == '.') || (motLu[i] == ',') || (motLu[i] == ';') || (motLu[i] == ':') || (motLu[i] == '!') || (motLu[i] == '?')) motLu[i] = '\0';
  188 + localisationArbre = test_mot(motLu[i], localisationArbre, &verif);
  189 + }
  190 + if(verif == 0) {
  191 + wprintf(L"Mot : '%ls' non present dans le dicitonnaire.\n", motLu);
  192 + }
  193 + else wprintf(L"Mot : '%ls' correct\n", motLu);
  194 + }
  195 +}
  196 +
  197 +void desallocationArbre(struct cell** pL) {
  198 +
  199 + if ((*pL) == NULL) return;
  200 +
  201 + desallocationArbre(&(*pL)-> arbreSuivant);
  202 + desallocationArbre(&(*pL)-> arbre -> listeFils);
  203 + free((*pL) -> arbre);
  204 + free(*pL);
  205 +}
  206 +
  207 +void desallocationTableauArbre(struct node tab_arbre[]) {
  208 + int i = 0;
  209 + for(wchar_t u = 'a'; u <= A+5; u++) { //A+5 car ajout de 5 caractères de ponctuation en plus des lettres
  210 + desallocationArbre(&tab_arbre[i].listeFils);
  211 + i++;
  212 + }
  213 +}
  214 +
... ...