1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include "table_hash.h"
#define TAILLEHASH 307
#define NBRSEQ 10
#define SEUIL 3
#define SEUIL 3
int main (void)
{ unsigned int pt;
char c;
Liste **TableHash = malloc (TAILLEHASH * sizeof *TableHash);
if (TableHash != NULL)
{
pt = HashCode (".");
{
int i;
for (i = 0; i < TAILLEHASH; ++i)
TableHash[i] = NULL;
}
printf ("debut du programme \n" "----------------------------------\n");
{
FILE *F = fopen ("C:\\essai.txt", "r");
if (F != NULL)
{
char mot[100];
printf
("Ouverture du Fichier \n"
"----------------------------------\n");
while (fscanf (F, "%s", mot) == 1)
{
unsigned int cle = HashCode (mot);
printf ("%u-", cle);
assert (mot != NULL);
assert (TableHash != NULL);
if ((cle != pt)&& (!ChercherMotDansTableHash (TableHash, mot)))
{
TableHash[cle] = InsertionEnTete(TableHash[cle],mot);
}
printf ("\n");
}
fclose (F);
F=fopen("C:\\essai.txt","r");
PosLigne(F,TableHash);
AfficherTableHash(TableHash);
}
}
}
scanf("%s",c);
return 0;
}
unsigned int HashCode (char *ligne)
{
unsigned int Code = 0;
size_t const len = strlen (ligne);
size_t i;
// printf ("ligne = '%s'\n", ligne);
for (i = 0; i < len; i++)
{
Code = ligne[i] + 31 * Code;
}
return Code % 101;
}
unsigned int ChercherMotDansTableHash (Liste ** TableHash,char *mot)
{
int found = 0;
unsigned int cle = HashCode (mot);
Liste *p;
assert (cle < TAILLEHASH);
for (p = TableHash[cle]; !found && p != NULL; p = p->suivant)
{
// printf ("p=%p\n", (void *) p);
assert (p != NULL);
assert (p->m != NULL);
assert (p->m->mot != NULL);
found = strcmp (p->m->mot, mot) == 0;
}
return found;
}
mots *InsertionEnTeteMot(mots *m,char mot[50]){
mots *nouveau;
nouveau = (mots *) malloc (sizeof(mots));
strcpy(nouveau->mot,mot);
nouveau->suivant = m;
return nouveau;
}
Liste *InsertionEnTete(Liste *L,char *mot){
Liste *nouveau;
nouveau = (Liste *) malloc (sizeof(Liste));
nouveau->freq=0;
nouveau->m=NULL;
nouveau->m=InsertionEnTeteMot(nouveau->m,mot);
nouveau->m = InsertionEnQueueMot(nouveau->m,mot);
nouveau->suivant = L;
nouveau->c = NULL;
return nouveau;
}
mots *InsertionEnQueueMot(mots *m,char mot[50]){
mots *nouveau;
nouveau = (mots *) malloc (sizeof(mots));
strcpy(nouveau->mot,mot);
nouveau->suivant = NULL;
if(m==NULL)
return nouveau;
else
{
m->suivant = nouveau;
return m;
}
}
void PosLigne(FILE *F,Liste **TableHash)
{
char s[50];
int nl,pos,i;
Liste *p;
Coordonnees *c;
int trouve =0;
nl=pos=1;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i]!=NULL)
for(p=TableHash[i];p!=NULL;p=p->suivant)
p->freq=0;
while(fscanf(F,"%s",s)==1)
{
for(i=0;i<TAILLEHASH;++i)
{
if(TableHash[i]!=NULL)
{
for(p=TableHash[i];p!=NULL;p=p->suivant)
{
Coordonnees *x;//ou Coordonnees x; sans le *
if(strcmp(p->m->mot,s)==0)
{
/*Avant l'insertion*/
trouve=0;
for(x=p->c;x!=NULL;x=x->suivant)
if(x->nl == nl) //même ligne
trouve=1;
if(trouve==0)
p->freq=p->freq+1;
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
}
}
}
}
/*ici :p*/
if(fgetc(F)=='\n')
{
++nl; // ou nl++; ça change rien
pos=0;
}
++pos;//pareil ou pos++; ça change rien non plus
}
}
Coordonnees *InsertionEnTeteCoordonnee(Coordonnees *C,int nl ,int pos){
Coordonnees *nouveau;
nouveau = (Coordonnees *) malloc (sizeof(Coordonnees));
nouveau->pos = pos;
nouveau->nl = nl;
nouveau->suivant = C;
return nouveau;
}
void AfficherTableHash(Liste **TableHash){
int i;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL){
printf("Conteneur %d \n",i);
AfficherListe(TableHash[i]);
printf("----------------------------------\n");
}
}
void AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
AfficheMot(p->m);
printf("%d : ",p->freq);
AfficherCoordonnees(p->c);
}
}
void AfficheMot(mots *m){
mots *p;
for(p=m;p!=NULL;p=p->suivant)
printf("%s ",p->mot);
printf("\n");
/*if(m!=NULL)
{
AfficheMot(m->suivant);
printf("%s ",m->mot);
}*/
}
void AfficherCoordonnees(Coordonnees *C){
Coordonnees *p;
for(p=C;p!=NULL;p=p->suivant)
printf(" (%d,%d) ",p->nl,p->pos);
printf("\n");
} |
Partager