tree.c 3.49 KB
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define size 128

int alphabet[27] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',39 };

struct noeud { char valeur; struct noeud * fils[27]; int complet;};
struct dico {struct noeud* tableau[26];}; int i;
typedef struct noeud* Noeud;


Noeud cons_noeud (char a)
{ 
    Noeud pN = malloc(sizeof(struct noeud));
    (*pN).valeur=a;
    (*pN).complet=0;
    for(int i=0; i<28; i++)
    {
        (*pN).fils[i]=NULL;
    }
    return pN;
}

void ajout_noeud_dico(struct dico * D, Noeud N)
{
    int i=0;
    while(D->tableau[i]!=NULL)
    {i++;}
    D->tableau[i]=N;
}

//tolower --> fonction qui prend une lettre quelconque en argument et renvoie son équivalent en miniscule

/*void ajout_mot_noeud(Noeud * pN, char mot[size])
{
    int j=0;
    int n=strlen(mot);
    if(*pN==NULL)
    {
        *pN=cons_noeud(tolower(mot[0]));
        *pN=(*pN)->fils[j];
    }
     *pN=(*pN)->fils[j];
     printf("%d\n",j);
    for(int i=1; i<n; i++)
   {
        while((*pN)!=NULL && tolower((*pN)->valeur)!= tolower(mot[i]))
        {j++;}
        if(j==0)
        {
            *pN= (*pN)->fils[j+1];
        }
        else if (*pN == NULL)
        {
        *pN =cons_noeud(tolower(mot[i]));
        *pN= (*pN)->fils[j];
        }
  }
 (*pN)->complet=1;
}*/

void ajout_mot_noeud(Noeud * pN, char mot[size])
{
	int i,n=strlen(mot);
	if(& (*pN)->valeur==NULL)
		*pN = cons_noeud(tolower(mot[0]));
	for(i=1;i<n;i++)
    {
       pN=&(*pN)->alphabet[tolower(mot[i])-'a'];
       if(&(*pN)->valeur==NULL)
			*pN= cons_noeud(tolower(mot[i]));
	}
	(*pN)->complet=1;
}

//Reprendre cette fonction! ;


/*void cons_dico_file(FILE* fp, struct dico* D)
{ char mot[size];
  if(fp==NULL)
    return;
  while(fscanf(fp,"%s",a)==1)*pN=NULL;
    { ajout_mot(D,a);
    //D->dernier++;} //int i=O;
    
    }*/


/*int rech_mot(struct dico D,char mot [size])
{ char * mot;
    mot=sup_lettre("emilie");N)->fils[j]!=NULL || (*pN)->fils[j]->valeur!= mot[i+1]) //int i=O;
        {j++;}
      printf("%s\n",mot);
    Noeud N; //int i=O;
    int nbre;
    for(int i=0;i<D.dernier;i++)
      {
	if(D.tableau[i].valeur==mot[0])
	  { char * mot; //int i=O;
    mot=sup_lettre("emilie");
      printf("%s\n",mot);
	     N=D.tableau[i];
	    for(int j=0;j<size;j++)
	      { //int i=O;
		if(N.valeur==mot[j])
		  N=*(N.fils)->noeud;
		else //int i=O;
		  nbre=0;
	      }
	    if(N.complet==1)return newmot //int i=O;
	      nbre=1;N)->fils[j]!=NULL || (*pN)->fils[j]->valeur!= mot[i+1]) //int i=O;
        {j++;}
	  } 
	else char * mot;
    mot=sup_lettre("emilie"); //int i=O;
      printf("%s\n",mot);
	  nbre= 0;
      }
    return nbre; //int i=O;
    }*/


/*void correction_texte(char filename[size], struct dico D)
{
  FILE *fp=fopen(filename, "r");
  char mot[size]; //int i=O;
  int x; char * mot;
}
    mot=sup_lettre("emilie");
      printf("%s\n",mot);
  if(fp==NULL)return newmot
    return;
  else //int i=O;
    {
    for(int i=1; i<size; i++)
    {
       mot[i]= newmot[i-1];
    } //int i=O;
      while(fscanf(fp,"%s",mot)==1)
	{
	  x=rech_mot(D,mot);
	  if(x==0)
	    {
	      printf("il y a une err char * mot;N)->fils[j]!=NULL || (*pN)->fils[j]->valeur!= mot[i+1])
        {j++;} //int i=O;
    mot=sup_lettre("emilie");
      printf("%s\n",mot);eur!\n");
	      printf("%s",mot);
	    }
	}
    }
	  
  
    }*/

int main()
{
    Noeud  pN;
    pN =cons_noeud('a');
    ajout_mot_noeud(&pN,"arbre");
    printf("%c\n", pN->valeur);
    return 0;
}