projetfinalaccent2.c 6.54 KB
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>

#include "fonctions.h"

#define A 256

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, int* lettreAjoute) {
	if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
		ajout_tete(elem, pL);
		if(lettreAjoute != NULL) *lettreAjoute = 1;
		return &(*pL)->arbre->listeFils;
	}
	else if((*pL)->arbre->lettre == elem) {
		return &(*pL)->arbre->listeFils;
	}
	else {
	  return insertion(elem, &(*pL)->arbreSuivant, lettreAjoute);
	}
}

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++;
  }
  tab[i].lettre = '?';
  tab[i+1].lettre = '!';
  tab[i+2].lettre = '\'';
  tab[i+3].lettre = '.';	//Ajout des caractères de ponctuation par défau
  tab[i+4].lettre = ':';
  tab[i+5].lettre = ';';
  for(int j = 0; j <= 5; j++) { 
    tab[i+j].listeFils = NULL;
    insertion('\0', &(tab[i+j].listeFils), NULL);
  }
}

int indice_lettre(struct node tab_arbre_prcp[], wchar_t lettre) {
  int i = 0;
  while(i >= 0 && i < A){  
   if(lettre == tab_arbre_prcp[i].lettre ){
     return i;
   }  
   i++;
  }
return -1;
}

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) {
    int estUneLettre = 1, lettreAjoute = 0;
	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] < 'A') || (motLu[0] > 'z')) {
       indice = indice_lettre(tab_arbre_prcp, motLu[0]);
       localisationArbre = &tab_arbre_prcp[indice].listeFils;
       if(indice == -1) {
	 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;
	localisationArbre = insertion(motLu[i], localisationArbre, &lettreAjoute);
     }
     if(lettreAjoute == 1) cptmot += 1;
   }
	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, cptfaute = 0;
  wchar_t motLu[50];
  wprintf(L"\nCorrection du fichier texte : \n\n");
  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] < 'A') || (motLu[0] > 'z')) {
       indice = indice_lettre(tab_arbre_prcp, motLu[0]);
       localisationArbre = &tab_arbre_prcp[indice].listeFils;
       if(indice == -1) {
		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;
        if((motLu[i] == '.') || (motLu[i] == ',') || (motLu[i] == ';') || (motLu[i] == ':') || (motLu[i] == '!') || (motLu[i] == '?')) motLu[i] = '\0';		//prendre en compte les fins de phrase
        localisationArbre = test_mot(motLu[i], localisationArbre, &verif);
      }
      if(verif == 0) {
      	wprintf(L"Mot '%ls' non present dans le dicitonnaire.\n", motLu);
      	cptfaute += 1;
      }
    }
    wprintf(L"\nLe texte comporte %d faute(s)\n", cptfaute);
    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"\nEntrez un mot ou une phrase a corriger : \n(0 pour quitter)\n\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] < 'A') || (motLu[0] > 'z')) {
       indice = indice_lettre(tab_arbre_prcp, motLu[0]);
       localisationArbre = &tab_arbre_prcp[indice].listeFils;
       if(indice == -1) {
		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;
        if((motLu[i] == '.') || (motLu[i] == ',') || (motLu[i] == ';') || (motLu[i] == ':') || (motLu[i] == '!') || (motLu[i] == '?')) motLu[i] = '\0';
        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 desallocationArbre(struct cell** pL) {
  
  if ((*pL) == NULL) return;

  desallocationArbre(&(*pL)-> arbreSuivant);
  desallocationArbre(&(*pL)-> arbre -> listeFils);
  free((*pL) -> arbre);
  free(*pL);  
}

void desallocationTableauArbre(struct node tab_arbre[]) {
  int i = 0;
  for(wchar_t u = 'a'; u <= A+5; u++) { //A+5 car ajout de 5 caractères de ponctuation en plus des lettres
    desallocationArbre(&tab_arbre[i].listeFils);
    i++;
  }
}