Bonjour,
voilà je dois programmer un dictionnaire sous forme d'arbre binaire, qui lit un texte et le stocke dans un arbre.
je nai pas d'erreur de compilation, mais quand je le teste j'obtient erreur de segmentation
pouvez vous me dire ou est l'erreur. la fonction d'affichage n'est pas definitive, là je voudrais juste voir si ma fonction de stockage fonctionne. merci d'avance
/*compte mot, version4 ouverture fichier, affichage ecran un mot par ligne, stockage arbre binaire */
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define taille 10000
struct wordtree
{ /* structure d'un noeud de l'arbre*/
char *wordvalue; /* la valeur du mot*/
int wordcount; /* le compteur associé*/
struct wordtree * left; /*lien vers le fils gauche*/
struct wordtree * right ; /* lien vers le fils droit*/
};
void stocker (struct wordtree *wtree, char *mot)
{
int trouve=0;
struct wordtree * root;
root=(struct wordtree *)malloc(sizeof(struct wordtree));
if (root==NULL) {strcpy(root->wordvalue, mot);
root->wordcount=1;
root->left=NULL;
root->right=NULL;
}
else {
root=wtree;
while (root!=NULL && trouve==0)
{
if(strcmp(root->wordvalue,mot)<0)
{
root=root->left;}
if (strcmp(root->wordvalue,mot)>0)
{root=root->right;}
if (strcmp(root->wordvalue,mot)==0)
{
(root->wordcount)++;
trouve=1;
}
}
strcpy(root->wordvalue, mot);
(root->wordcount)=1;
}
}
void affichage (struct wordtree *wtree)
{
while (wtree!=NULL)
{
printf("%s ", wtree->wordvalue);
printf("%d\n",wtree->wordcount);
wtree=wtree->left;
}
}
int main ( int argc , char ** argv )
{
setlocale(LC_ALL, ""); /*pour inclure les caractère daccent etc*/
FILE * df;
char car;
char mot[taille];
struct wordtree *wtree=NULL;
wtree=(struct wordtree *)malloc(sizeof(struct wordtree));
int i=0;
if (argc!=2) {printf ("utilisation : %s fichier \n",argv[0]);
exit(-1);
}
if ((df=fopen (argv[1],"r"))==NULL) {
printf ("fichier %s non accessible \n",argv[1]);
exit(-2);
}
while ((car=fgetc(df))!=EOF)
{
if (isalpha(car)) {mot[i]=car;
i=i+1 ;
}
else {if (i>0) { /* cas des separateurs multiples*/
mot[i]='\0';/* terminaison de chaine*/
stocker (wtree,mot); /*traiter le mot*/
i=0;/*remise à zero pour mot suivant*/
}}
}
affichage(wtree);
fclose (df);
return 0;
}
