Bonjour je suis nouveau sur le forum est ceci est ma première question donc j'espère que je ne me suis pas trompé d'endroit pour poster.
Voilà j'ai un projet à faire en C sur les tables de hachage. Je crois avoir bien compris le principe malheureusement je n'arrive pas à créer un dictionnaire à partir d'un fichier texte.
Il semble que j'ai un problème dans ma fonction Ajouter ou bien dans Lecture. Il semble que seul le dernier mot du fichier que je lis soit ajouter dans ma table. Pourtant à l'aide de printf je vois que l'ajout de tout les mots c'est bien effectuer. Si quelqu'un pouvait m'aider cela serait fort aimable. Je joint mon source:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#define B 256
#define N 10000
#define LIBRE 2 /* a été libre par suppression */
#define OQP 1
#define VIDE 0 /* n'a jamais été occupée */
#define MAX 10
typedef struct {
char etat; /* occupée ou libre ou vide */
char * mot;
int occurrence;
int position[MAX]; /* position du mot dans la phrase */
}Case;
typedef Case Dictionnaire[N];
/* Hachage linéaire */
/* Fonction non utilisée */
int h(char * mot)
{
int h = 0;
int i;
for (i = 0; mot[i] != '\0'; i++)
{
h = h*256 + (unsigned int) mot[i];
if (h >= B)
h %= B;
}
return h;
}
/* Fonction de hachage très simple sur la premiere lettre du mot */
int hbis(char * mot)
{
return (mot[0]-'a');
}
void ViderDictionnaire(Dictionnaire a)
{
int i;
for(i=0;i<=B-1;i++)
{
a[i].etat=VIDE;
a[i].mot='\0';
}
}
int Position(char * mot,Dictionnaire a)
{
int i=0;
while(i<B && a[h(mot)].mot!=mot && a[h(mot)].etat!=VIDE &&
a[h(mot)].etat==OQP)
i=i+1;
return i;
}
int Positionne(char * mot,Dictionnaire a)
{
int hi,dernier;
hi=hbis(mot);
dernier= (hi + B-1) % B ;
while(hi!=dernier && a[hi].mot!=mot && a[hi].etat!=VIDE &&
a[hi].etat!=LIBRE)
hi = (hi + 1) % B ;
return hi;
}
int Positiona(char * mot,Dictionnaire a)
{
int hi,dernier;
hi=hbis(mot);
dernier= (hi + B-1) % B ;
while(hi!=dernier && a[hi].etat==OQP /* (a[hi].etat!=VIDE ||
a[hi].etat!=LIBRE) */ )
hi = (hi + 1) % B ;
return hi;
}
/* Renvois 1 si le mot est deja dans le dico */
int Element(char * mot,Dictionnaire a)
{
if(a[Positionne(mot,a)].mot==mot)
return 1;
else return 0;
}
void Enlever(char * mot,Dictionnaire a)
{
int i;
i=Positionne(mot,a);
if(a[i].mot==mot)
{
a[i].etat=LIBRE;
a[i].mot='\0';
printf("on a mis libre pour a[%d]=%s\n",i,a[i].mot);
}
}
void Ajouter(char * mot,Dictionnaire a)
{
int i;
int position_du_mot;
if(Element(mot,a)==0)
{
i=Positiona(mot,a);
if(a[i].etat!=OQP /* a[i].etat==VIDE || a[i].etat==LIBRE) */ )
{
a[i].mot=mot; printf("'%s' ajouté a la position %d\t",mot,i);
a[i].etat=OQP;
a[i].occurrence=1;
}
else if(a[i].mot!=mot)
printf("erreur table pleine\n");
printf("a[%d]=%s\n",i,a[i].mot);
}
else /* Le mot est déjà dans la table on augmente juste le nbr
d'occurrences */
{
position_du_mot=Positionne(mot,a);
printf("'%s' est déjà présent %d fois à la position
%d\n",mot,a[position_du_mot].occurrence,position_du_mot);
a[position_du_mot].occurrence++;
}
}
int estAlpha (int x)
{
if ( isalpha(x) || x == 'é' || x == 'è' || x == 'ê' || x == 'ç' ||
x == 'à'
|| x == 'â' || x == 'ï' || x == 'ô' || x == 'ù' || x == 'û' )
return 1;
else return 0;
}
void AfficheDictionnaire(Dictionnaire a)
{
int i;
printf(" ---------------------\n");
printf("|Affichage de la table|\n
---------------------\nIndice\tMot\t\tOccurrences\n");
for(i=0;i<=B;i++)
{
if(a[i].etat==OQP)
printf("i=%d\tmot=%s\toccurrences=%d\n",i,a[i].mot,a[i].occurrence);
}
}
int Lecture (FILE * file,Dictionnaire a)
{
char buf[50];
int c;
int i, cour=0;
int tmp;
rewind(file);printf("->Début de la lecture\n");
while(feof(file) == 0)
{
i=0;
c=fgetc(file);
while(c=='\r' || c=='\n'|| c=='\t' || estAlpha(c) == 0)
{
c=fgetc(file);
cour++;
} ungetc(c, file); cour--;
c= fgetc(file); cour++;
while( estAlpha(c) == 1)
{
c = tolower(c);
buf[i] = (char)c;
i++;
c= fgetc(file);
cour++;
}
buf[i] = '\0';
tmp = cour - (strlen(buf)-1);
Ajouter(buf,a);
ungetc(c, file); cour--;
printf("%c", ungetc(c, file));
}
printf("->Fin de la fonction lecture\n\n");
AfficheDictionnaire(a);
fclose(file);
return 1;
}
void usage(const char *nom)
{
fprintf(stderr,"utilisation:\n");
fprintf(stderr,"%s,nomdufichiertexte\n",nom);
fprintf(stderr,"lit le texte contenu dans le fichier %s\n",nom);
}
int Prelecture(int argc, char * argv[],FILE * file,Dictionnaire a)
{
if(argc!=2)
{
usage(argv[0]);
printf("sort de usage\n");
return EXIT_FAILURE;
}
else
{
fprintf(stderr,"Ouverture du fichier '%s' en
cours...\n",argv[1]);
file=fopen(argv[1],"r");
printf("fichier ouvert\n\n--------------------------\n");
if(file==NULL)
{
fprintf(stderr,"Erreur d'ouverture du fichier %s\n",argv[1]);
perror(argv[1]);
return EXIT_FAILURE;
}
int c, nl, nm, nc, etat,dehors,dedans;
etat = dehors;
nl = nm = nc = 0;
while(!feof(file))
{
c = fgetc(file);
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
etat = dehors;
else if(etat == dehors)
{
etat = dedans;
++nm;
}
}
printf("\nNombre de ligne: %d\nNombre de mot: %d\nNombre de
caracteres: %d\n", nl, nm, nc);
printf("----------------------------------------\n");
Lecture(file,a);
}
return EXIT_SUCCESS;
}
int main (int argc, char * argv[])
{
FILE * file;
Dictionnaire a;
char * mot1="bonjour";
char * mot2="ceci";
char * mot3="babar";
char * mot4="bonjour";
ViderDictionnaire(a);
printf("\n\n");
/* printf("Quelques tests\n");
Ajouter(mot1,a);
Ajouter(mot2,a);
Ajouter(mot3,a);
Ajouter(mot4,a);
printf("Fin des tests\n\n");*/
Prelecture(argc,argv,file,a);
printf("\n");
return 0;
}