projet.c 2.51 KB
#include "projet.h"

bool is_empty(ptcell pt)
{
  return(pt==NULL);
}

bool feuille(ptcell pt)
{
  for(int i=0; i<NB; i++)
    if(pt->arbre[i] != NULL)
      return false;
  return true;
}

void init_arbre(ptcell *pt)
{
  if(is_empty(*pt))
    {
      *pt = malloc(sizeof(cell));
      (*pt)->lettre = '?';
      (*pt)->fin = false;
      for(int i=0; i<NB; i++)
        (*pt)->arbre[i] = NULL;
    }
} 

int indice_lettre(char lettre)
{
  if(lettre>=97 && lettre<=122) 
    return lettre-'a';
  if(lettre>=65 && lettre<=90) 
    return lettre-'A';
  if(lettre == 39) 
    return lettre-13;
  else return -1;
}

void ajout_arbre(ptcell pt, char mot[])
{
  int index=0;
  int indice;
  ptcell pt1 = pt;
  while(mot[index]!='\0')
  {
    indice = indice_lettre(mot[index]);
    if(pt1->arbre[indice]!=NULL)
    {
      pt1=pt1->arbre[indice];
      if(mot[index+1]=='\0')
        pt1->fin = true;
    }
    else
    {
      ptcell pt2 = NULL;
      pt2 = malloc(sizeof(cell));
      pt2->lettre = mot[index];
      for(int i=0; i<NB; i++)
        pt2->arbre[i]=NULL;
      pt2->fin = false;
      if(mot[index+1]=='\0')
        pt2->fin = true;
      pt1->arbre[indice] = pt2;
      pt1=pt1->arbre[indice];
    }
    index++;
  }
}

bool caract_fin(char lettre)
{
  if(lettre==0) return true;
  if((lettre>=32 && lettre<=38)||(lettre>=40 && lettre<=47)||(lettre>=58 && lettre<=64)||(lettre>=123 && lettre<=126)) return true;
  return false;
}

void verifie_mot(ptcell pt, char mot[], int* compteur)
{
  bool fin = false;
  int index = 0;
  int indice;
  ptcell pt1 = pt;
  while(!caract_fin(mot[index]))
  {
    indice = indice_lettre(mot[index]);
    if(pt1 != NULL && pt1->arbre[indice]!=NULL)
    {
      index++;
      pt1=pt1->arbre[indice];
      fin=pt1->fin;
    }
    else
    {
      fin = true;
      printf("Le mot %s n'est pas reconnu.\n", mot);
      (*compteur)++;
      break;
    }
  }
  if(!fin)
  {
    (*compteur)++;
    printf("Le mot %s n'est pas reconnu.\n", mot);
  }
}

// Lecture du fichier à analyser
void lecture_texte(FILE* fp, ptcell* arbre, int* compteur)
{
  char mot[MAX];
  while(1)
  {
    if(fscanf(fp, "%s", mot)!=1)
      break;
    verifie_mot(*arbre, mot, compteur);
  }
}

// Lecture du dictionnaire
void lecture_dico(FILE* fp, ptcell* arbre)
{
  char mot[MAX];
  while(1)
    {
      if(fscanf(fp, "%s", mot)!=1)
        break;
      ajout_arbre(*arbre, mot);
    } 
}

void free_arbre(ptcell* arbre)
{
  if(*arbre!=NULL)
  {
    for(int i=0; i<NB; i++)
    {
      free_arbre(&(*arbre)->arbre[i]);
    }
    free(*arbre);
  }
}