arbre.c 4.83 KB
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define TAILLE 27

//#include "arbre.h"


typedef struct arbre{
    char val;
    struct arbre *suite[TAILLE];
    bool finmot; //1 si fin de mot
}Arbre;
 
typedef struct dico {
    Arbre *alpha[TAILLE];
}Dico;



int calculcase(char c)
{
    if(c==39){
        return 26;
    }
    else if(c<97){
        return c-'A';
    }
    else {
    return c-'a';
    }
}

bool est_vide(struct arbre *arbre)
{
	return arbre==NULL;
}

bool mot_existe(struct arbre *monarbre,char *mot,int i){
    if (monarbre==NULL){
//         printf("false\n");
        return false;
    }
//     printf("%c \n",(monarbre->val));
    if (mot[i+1]=='\0'){
//         printf("finmot\n");
        return monarbre->finmot;
    }
    else {
        i++;
        return mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
        
    }
}

void ini_dico(struct dico *pt_dico)
{
    printf("ini_dico\n");
    for(int i=0;i<TAILLE;i++){
        pt_dico->alpha[i]=NULL;        
    }
}
void creation_arbre(Arbre **ppt_arbre,char c)
{
//     printf("creation arbre\n");
    Arbre *monarbre=malloc(sizeof(struct arbre));
    monarbre->val=c;
    monarbre->finmot=false;
    for(int j=0;j<TAILLE;j++){
        monarbre->suite[j]=NULL;     
    }
    *ppt_arbre=monarbre;
}
    

void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
{

//     printf("%s:length:%zu \n",mot,strlen(mot));
    
    if(mot[i]!='\0'){
//         printf("\nlettre:%c; i=%d\n",mot[i],i);
        if (*arbrecourant==NULL){
            creation_arbre(arbrecourant,mot[i]);
        }
        i++;
        if(mot[i+1]=='\0'){
//             printf("mot:%s  %d\n\n\n",mot,i);
            (*arbrecourant)->finmot=true;
        }
        else ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
    }

}

void charger_dico(FILE *fp, struct dico **ppt_dico)
{   char mot[20];
    ini_dico(*ppt_dico);
    while (fscanf(fp, "%s", mot)==1){
//         printf("mot:%s\n\n\n",mot);
//         printf("\nlettre:%c\n",mot[0]);
        ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
    }
    return ;
}


void free_arbre(struct arbre *pt_arbre)
{
   if (pt_arbre==NULL){
        return ;
    }
    for(int i=0;i<TAILLE;i++){
        free_arbre((pt_arbre->suite[i]));
    }
    free(pt_arbre);
}

void free_dico(struct dico *pt_dico)
{
   if (pt_dico==NULL){
        return ;
    }
  
    for(int i=0;i<TAILLE;i++){
        free_arbre((pt_dico->alpha[i]));
    }

    free(pt_dico);
}

void affiche_arbre(struct arbre *arbre)
{
    if(arbre==NULL){
        return ;
    }
    printf("%c:",arbre->val);
    for(int i=0;i<TAILLE;i++){
        affiche_arbre(arbre->suite[i]);
    }
}

void affiche_dico(struct dico *dico)
{
    if(dico==NULL){
        return ;
    }
    printf("---------------------------------------\n");
    for(int i=0;i<TAILLE;i++){
        printf("%d:",i);
        affiche_arbre(dico->alpha[i]);
        printf("\n");
    }
   
}


void analyse_fichier(FILE *fp,Dico *pt_dico, int *res[2]){
    int nbmot_t=0;
    int nbmot_f=0;
    char mot[20];
    while (fscanf(fp, "%s", mot)==1){
//         printf("mot:%s\n\n\n",mot);
//         printf("\nlettre:%c\n",mot[0]);
        if(mot_existe(pt_dico->alpha[calculcase(mot[0])],mot,0)){
            nbmot_t++;
        }
        else nbmot_f++;
    }
    *res[0]=nbmot_t;
    *res[1]=nbmot_f;    
}

int main (int argc,char *argv[]){
    FILE* dico = NULL;
    if(argc >1){
        printf("chargement de votre dictionnaire \n");
        dico=fopen(argv[1],"r"); 
    }
//     else dico=fopen("words-no-accents","r");
    
  	if(dico == NULL){
//         return EXIT_FAILURE;
        printf("chargement du dictionnaire par default");
        dico=fopen("words-no-accents","r");
    } //File is not readable
  	
    struct dico *mondico=malloc(sizeof(struct dico));
    charger_dico(dico,&mondico);
    affiche_dico(mondico);
    fclose(dico); 
    char mot[]="zombie";
    char mot2[]="zombbbiiee";
//     mot_existe(mondico->alpha[calculcase(mot[0])],mot,0);
//     mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0);
    
    printf("%s:%d \n",mot,mot_existe(mondico->alpha[calculcase(mot[0])],mot,0));
    printf("%s:%d \n",mot2,mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0));
    
    FILE* fichier_utilisateur = NULL;
    if(argc >2){
        printf("Analyse de votre fichier \n");
        fichier_utilisateur=fopen(argv[2],"r"); 
        printf("Fin Analyse de votre fichier \n");
    }
    
//     if (fichier_utilisateur!=NULL){
//         printf("analyse en cours");
//         int *res[2];
//         analyse_fichier(fichier_utilisateur,mondico,res);
//         printf("juste:%d \n",*res[0]);
//         printf("faux:%d \n",*res[1]);
//     }
    
    printf("fin programme \n");
    
    
    free_dico(mondico);
    if (fichier_utilisateur!=NULL){
        fclose(fichier_utilisateur);
    }
           
    return 0;
    }