Pb de compilation code C table hachage
Fermé
stroumpf
-
16 juin 2008 à 19:21
stroumpf Messages postés 289 Date d'inscription mardi 17 juin 2008 Statut Membre Dernière intervention 1 mars 2009 - 23 juin 2008 à 12:40
stroumpf Messages postés 289 Date d'inscription mardi 17 juin 2008 Statut Membre Dernière intervention 1 mars 2009 - 23 juin 2008 à 12:40
A voir également:
- Pb de compilation code C table hachage
- Table ascii - Guide
- Table des matières word - Guide
- Freewifi secure code ✓ - Forum Réseau
- Code 80072efe ✓ - Forum Windows
- Code blocks avec compilateur - Télécharger - Langages
29 réponses
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 16:32
22 juin 2008 à 16:32
Re,
il faut num position de second mot soit plus grand que le 1er mot :" est clé " et non pas "clé est"
le numéro de ligne ne compte pas?!
Le fichier.
Le code
table_hash.h
posmot2.c
Le résultat
il faut num position de second mot soit plus grand que le 1er mot :" est clé " et non pas "clé est"
le numéro de ligne ne compte pas?!
Le fichier.
lami20j@debian:~/trash$ cat fichier.txt Non Lami :) ben faut parcourir la table de hachage( 2 compteurs) , tester chaque avec le mot suivantselon la num ligne qui est enregistré dans la liste chaine(tu te souviens) et puis faire la jointure(calculer la clé de hachage pour stocker dans la table de hachage) ctt ;) tu vois?
Le code
table_hash.h
/* table_hash.h */
#ifndef TABLE_HASH
#define TABLE_HASH
typedef struct c{
int pos;
int nl;
struct c *suivant;
}Coordonnees;
typedef struct L{
char mot[50];
Coordonnees *c;
struct L *suivant;
}Liste;
Liste *InsertionEnTete(Liste *L,char *mot);
void InsertionTableHash(FILE *F, Liste **TableHash);
void jointure(Liste **TableHash);
Coordonnees *InsertionEnTeteCoordonnee(Coordonnees *C,int nl,int pos);
void AfficherListe(Liste *L);
void AfficherCoordonnees(Coordonnees *C);
void AfficheMot(char *mot);
void ParcourirElementTableHash(Liste **TableHash,char *mot);
unsigned int hash_cle(char *mot);
void AfficherTableHash(Liste **TableHash);
unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot);
void PosLigne(FILE *F,Liste **TableHash);
#endif
posmot2.c
/* posmot2.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include "table_hash.h"
#define TAILLEHASH 17
int main()
{
FILE *F;
Liste **TableHash;
char mot[100];
int i;
TableHash = (Liste **) malloc (TAILLEHASH * sizeof(Liste *));
for(i=0;i<TAILLEHASH;++i)
TableHash[i] = NULL;
F=fopen("fichier.txt","r");
InsertionTableHash(F,TableHash);
fclose(F);
AfficherTableHash(TableHash);
printf("Chercher mot : ");
scanf("%s",mot);
if(ChercherMotDansTableHash(TableHash,mot))
ParcourirElementTableHash(TableHash,mot);
else
printf("%s inexistant dans la table de hash\n");
jointure(TableHash);
return 0;
}
Liste *InsertionEnTete(Liste *L,char *mot){
Liste *nouveau;
nouveau = (Liste *) malloc (sizeof(Liste));
strcpy(nouveau->mot,mot);
nouveau->suivant = L;
nouveau->c = NULL;
return nouveau;
}
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 AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
}
}
void AfficherCoordonnees(Coordonnees *C){
Coordonnees *p;
for(p=C;p!=NULL;p=p->suivant)
printf(" (%d,%d) ",p->nl,p->pos);
printf("\n");
}
void AfficheMot(char *mot){
printf("\t\t%s ",mot);
}
unsigned int hash_cle(char *mot){
unsigned int val = 0;
for(;*mot!='\0';++mot)
val = *mot + 31 * val;
return val % TAILLEHASH;
}
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");
}
}
unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot){
Liste *p;
unsigned int cle;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(p->mot,mot)==0)
return 1;
return 0;
}
void InsertionTableHash(FILE *F, Liste **TableHash){
int nl,pos,i;
unsigned int cle;
char mot[100];
Liste *p;
nl=pos=1;
while(fscanf(F,"%s",mot)==1){
cle = hash_cle(mot);
if(! ChercherMotDansTableHash(TableHash,mot))
TableHash[cle] = InsertionEnTete(TableHash[cle],mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant){
if(strcmp(p->mot,mot)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
}
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
//printf("\n");
}
}
void ParcourirElementTableHash(Liste **TableHash,char *mot){
int cle;
Liste *p;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(mot,p->mot) == 0){
printf("%s : ",mot);
AfficherCoordonnees(p->c);
}
}
void jointure(Liste **TableHash){
int i;
Liste *p,*pp;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i]!=NULL){
for(p=TableHash[i];p!=NULL;p=p->suivant)
for(pp=TableHash[i];pp!=NULL;pp=pp->suivant)
if(strcmp(p->mot,pp->mot)){
if(p->c->pos < pp->c->pos)
printf("%s %s (%d,%d) (%d,%d)\n",
p->mot,pp->mot,
p->c->nl,p->c->pos,
pp->c->nl,pp->c->pos);
else
printf("%s %s (%d,%d) (%d,%d)\n",
pp->mot,p->mot,
pp->c->nl,pp->c->pos,
p->c->nl,p->c->pos);
}
}
}
Le résultat
lami20j@debian:~/trash$ gcc posmot2.c -o posmot2
lami20j@debian:~/trash$ ./posmot2
Conteneur 0 :
;) (2,46)
clé (2,35)
et (2,29)
est (2,21)
num (2,18)
-------------------------------------------------
Conteneur 1 :
chaine(tu (2,26)
-------------------------------------------------
Conteneur 2 :
stocker (2,39)
-------------------------------------------------
Conteneur 3 :
liste (2,25)
:) (1,3)
Non (1,1)
-------------------------------------------------
Conteneur 5 :
puis (2,30)
enregistré (2,22)
de (2,43) (2,36) (2,6)
-------------------------------------------------
Conteneur 6 :
faire (2,31)
qui (2,20)
hachage( (2,7)
-------------------------------------------------
Conteneur 7 :
tu (3,1)
hachage) (2,44)
avec (2,13)
table (2,42) (2,5)
-------------------------------------------------
Conteneur 8 :
te (2,27)
suivantselon (2,16)
chaque (2,12)
parcourir (2,3)
-------------------------------------------------
Conteneur 9 :
faut (2,2)
ben (2,1)
-------------------------------------------------
Conteneur 10 :
hachage (2,37)
souviens) (2,28)
, (2,10)
compteurs) (2,9)
Lami (1,2)
-------------------------------------------------
Conteneur 11 :
la (2,41) (2,34) (2,32) (2,24) (2,17) (2,4)
-------------------------------------------------
Conteneur 12 :
jointure(calculer (2,33)
-------------------------------------------------
Conteneur 13 :
ctt (2,45)
tester (2,11)
-------------------------------------------------
Conteneur 15 :
dans (2,40) (2,23)
ligne (2,19)
le (2,14)
-------------------------------------------------
Conteneur 16 :
vois? (3,2)
pour (2,38)
mot (2,15)
2 (2,8)
-------------------------------------------------
Chercher mot : mot
mot : (2,15)
clé ;) (2,35) (2,46)
et ;) (2,29) (2,46)
est ;) (2,21) (2,46)
num ;) (2,18) (2,46)
clé ;) (2,35) (2,46)
et clé (2,29) (2,35)
est clé (2,21) (2,35)
num clé (2,18) (2,35)
et ;) (2,29) (2,46)
et clé (2,29) (2,35)
est et (2,21) (2,29)
num et (2,18) (2,29)
est ;) (2,21) (2,46)
est clé (2,21) (2,35)
est et (2,21) (2,29)
num est (2,18) (2,21)
num ;) (2,18) (2,46)
num clé (2,18) (2,35)
num et (2,18) (2,29)
num est (2,18) (2,21)
:) liste (1,3) (2,25)
Non liste (1,1) (2,25)
:) liste (1,3) (2,25)
Non :) (1,1) (1,3)
Non liste (1,1) (2,25)
Non :) (1,1) (1,3)
enregistré puis (2,22) (2,30)
puis de (2,30) (2,43)
enregistré puis (2,22) (2,30)
enregistré de (2,22) (2,43)
puis de (2,30) (2,43)
enregistré de (2,22) (2,43)
qui faire (2,20) (2,31)
hachage( faire (2,7) (2,31)
qui faire (2,20) (2,31)
hachage( qui (2,7) (2,20)
hachage( faire (2,7) (2,31)
hachage( qui (2,7) (2,20)
tu hachage) (3,1) (2,44)
tu avec (3,1) (2,13)
tu table (3,1) (2,42)
tu hachage) (3,1) (2,44)
avec hachage) (2,13) (2,44)
table hachage) (2,42) (2,44)
tu avec (3,1) (2,13)
avec hachage) (2,13) (2,44)
avec table (2,13) (2,42)
tu table (3,1) (2,42)
table hachage) (2,42) (2,44)
avec table (2,13) (2,42)
suivantselon te (2,16) (2,27)
chaque te (2,12) (2,27)
parcourir te (2,3) (2,27)
suivantselon te (2,16) (2,27)
chaque suivantselon (2,12) (2,16)
parcourir suivantselon (2,3) (2,16)
chaque te (2,12) (2,27)
chaque suivantselon (2,12) (2,16)
parcourir chaque (2,3) (2,12)
parcourir te (2,3) (2,27)
parcourir suivantselon (2,3) (2,16)
parcourir chaque (2,3) (2,12)
ben faut (2,1) (2,2)
ben faut (2,1) (2,2)
souviens) hachage (2,28) (2,37)
, hachage (2,10) (2,37)
compteurs) hachage (2,9) (2,37)
Lami hachage (1,2) (2,37)
souviens) hachage (2,28) (2,37)
, souviens) (2,10) (2,28)
compteurs) souviens) (2,9) (2,28)
Lami souviens) (1,2) (2,28)
, hachage (2,10) (2,37)
, souviens) (2,10) (2,28)
compteurs) , (2,9) (2,10)
Lami , (1,2) (2,10)
compteurs) hachage (2,9) (2,37)
compteurs) souviens) (2,9) (2,28)
compteurs) , (2,9) (2,10)
Lami compteurs) (1,2) (2,9)
Lami hachage (1,2) (2,37)
Lami souviens) (1,2) (2,28)
Lami , (1,2) (2,10)
Lami compteurs) (1,2) (2,9)
tester ctt (2,11) (2,45)
tester ctt (2,11) (2,45)
ligne dans (2,19) (2,40)
le dans (2,14) (2,40)
ligne dans (2,19) (2,40)
le ligne (2,14) (2,19)
le dans (2,14) (2,40)
le ligne (2,14) (2,19)
vois? pour (3,2) (2,38)
vois? mot (3,2) (2,15)
vois? 2 (3,2) (2,8)
vois? pour (3,2) (2,38)
mot pour (2,15) (2,38)
2 pour (2,8) (2,38)
vois? mot (3,2) (2,15)
mot pour (2,15) (2,38)
2 mot (2,8) (2,15)
vois? 2 (3,2) (2,8)
2 pour (2,8) (2,38)
2 mot (2,8) (2,15)
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 16:42
22 juin 2008 à 16:42
Re,
au lieu de
if(strcmp(p->mot,pp->mot)){
mets
au lieu de
if(strcmp(p->mot,pp->mot)){
mets
if(strcmp(p->mot,pp->mot) < 0){
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 22:05
22 juin 2008 à 22:05
Meerci LAMI, mais juste je veux verifier un truc, quand vous avez testé ce code sur ton texte, est ce que les info sur juste de chaque mot(numligne eu position)?
merci
merci
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 22:31
22 juin 2008 à 22:31
Là il maffiche des resultats incorrects
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 23:14
22 juin 2008 à 23:14
Salut,
Je t'ai déjà demande
le numéro de ligne ne compte pas?!
En fait tu peux avoir pour la même clé (donc des collision) des mot qui proviennent du plusieurs lignes, d'où ma question.
est ce que les info sur juste de chaque mot(numligne eu position)?
je ne comprends pas
Là il maffiche des resultats incorrects
Montre moi, où ;-)
Je t'ai déjà demande
le numéro de ligne ne compte pas?!
En fait tu peux avoir pour la même clé (donc des collision) des mot qui proviennent du plusieurs lignes, d'où ma question.
est ce que les info sur juste de chaque mot(numligne eu position)?
je ne comprends pas
Là il maffiche des resultats incorrects
Montre moi, où ;-)
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 23:36
22 juin 2008 à 23:36
Bonsoir Lami; et merci je m'avoir repondu,
laà quand il maffiche la table de hacahge il m'affiche les mot , mais leurs infos(position et num ligne ) sont incorrecte.
je comprends pas pourkoi en fait.
Chez toi c nikel , mais chez moi il ya une chose qui cloche. :(
laà quand il maffiche la table de hacahge il m'affiche les mot , mais leurs infos(position et num ligne ) sont incorrecte.
je comprends pas pourkoi en fait.
Chez toi c nikel , mais chez moi il ya une chose qui cloche. :(
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 23:37
22 juin 2008 à 23:37
Ben, tu n'as qu'à m'afficher tes tests comme je l'ai fait ;-)
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 23:38
22 juin 2008 à 23:38
comment tu fais lami pour afficher, moi les resultata s'affichesnt sur l'ecran noir :)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 23:41
22 juin 2008 à 23:41
Je travaille sous linux si tu l'as remarqué ;-)
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 23:44
22 juin 2008 à 23:44
ouii , c pas le cas pour moi
conteneur 0 : piu' (5,10)
conteneur 19 : troppa (4,9)
..etc
tous les resultata sont incorrects
conteneur 0 : piu' (5,10)
conteneur 19 : troppa (4,9)
..etc
tous les resultata sont incorrects
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 23:46
22 juin 2008 à 23:46
Lasciatemi cantare
con la chitarra in mano
Lasciatemi cantare
sono un italiano
Buongiorno Italia gli spaghetti al dente
e un partigiano come Presidente
con l'autoradio sempre nella mano destra
e un canarino sopra la finestra
Buongiorno Italia con i tuoi artisti
con troppa America sui manifesti
con le canzoni con amore
con il cuore
con piu' donne sempre meno suore
Buongiorno Italia
Buongiorno Maria
con gli occhi pieni di malinconia
buongiorno Dio
lo sai che ci sono anch'io
//////////////////////////////
Normallement le resultat soient :
piu'(13,2)
con la chitarra in mano
Lasciatemi cantare
sono un italiano
Buongiorno Italia gli spaghetti al dente
e un partigiano come Presidente
con l'autoradio sempre nella mano destra
e un canarino sopra la finestra
Buongiorno Italia con i tuoi artisti
con troppa America sui manifesti
con le canzoni con amore
con il cuore
con piu' donne sempre meno suore
Buongiorno Italia
Buongiorno Maria
con gli occhi pieni di malinconia
buongiorno Dio
lo sai che ci sono anch'io
//////////////////////////////
Normallement le resultat soient :
piu'(13,2)
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
22 juin 2008 à 23:50
22 juin 2008 à 23:50
chez moi j'ai bien le résultat demandé (voir en gras)
lami20j@debian:~/trash$ ./posmot2
Conteneur 0 :
che (18,3)
Dio (17,2)
troppa (10,2)
-------------------------------------------------
Conteneur 1 :
manifesti (10,5)
-------------------------------------------------
Conteneur 2 :
pieni (16,4)
l'autoradio (7,2)
Presidente (6,5)
-------------------------------------------------
Conteneur 3 :
malinconia (16,6)
i (9,4)
finestra (8,6)
destra (7,6)
italiano (4,3)
-------------------------------------------------
Conteneur 4 :
anch'io (18,6)
come (6,4)
al (5,5)
sono (18,5) (4,1)
-------------------------------------------------
Conteneur 5 :
tuoi (9,5)
con (16,1) (13,1) (12,1) (11,4) (11,1) (10,1) (9,3) (7,1) (2,1)
-------------------------------------------------
Conteneur 6 :
Maria (15,2)
piu' (13,2)
mano (7,5) (2,5)
-------------------------------------------------
Conteneur 7 :
sui (10,4)
artisti (9,6)
sempre (13,4) (7,3)
-------------------------------------------------
Conteneur 8 :
lo (18,1)
meno (13,5)
cuore (12,3)
canzoni (11,3)
America (10,3)
-------------------------------------------------
Conteneur 9 :
di (16,5)
sopra (8,4)
canarino (8,3)
nella (7,4)
Italia (14,2) (9,2) (5,2)
-------------------------------------------------
Conteneur 10 :
spaghetti (5,4)
-------------------------------------------------
Conteneur 11 :
amore (11,5)
gli (16,2) (5,3)
la (8,5) (2,2)
cantare (3,2) (1,2)
-------------------------------------------------
Conteneur 12 :
ci (18,4)
buongiorno (17,1)
suore (13,6)
Buongiorno (15,1) (14,1) (9,1) (5,1)
-------------------------------------------------
Conteneur 13 :
dente (5,6)
Lasciatemi (3,1)
-------------------------------------------------
Conteneur 14 :
il (12,2)
un (8,2) (6,2) (4,2)
chitarra (2,3)
-------------------------------------------------
Conteneur 15 :
occhi (16,3)
le (11,2)
partigiano (6,3)
sciatemi (1,1)
-------------------------------------------------
Conteneur 16 :
sai (18,2)
donne (13,3)
e (8,1) (6,1)
in (2,4)
-------------------------------------------------
Chercher mot : piu'
piu' : (13,2)
troppa che (10,2) (18,3)
Dio che (17,2) (18,3)
troppa Dio (10,2) (17,2)
l'autoradio pieni (7,2) (16,4)
pieni Presidente (16,4) (6,5)
l'autoradio Presidente (7,2) (6,5)
i malinconia (9,4) (16,6)
italiano i (4,3) (9,4)
malinconia finestra (16,6) (8,6)
i finestra (9,4) (8,6)
italiano finestra (4,3) (8,6)
malinconia destra (16,6) (7,6)
i destra (9,4) (7,6)
finestra destra (8,6) (7,6)
italiano destra (4,3) (7,6)
italiano malinconia (4,3) (16,6)
come anch'io (6,4) (18,6)
sono anch'io (18,5) (18,6)
come sono (6,4) (18,5)
al anch'io (5,5) (18,6)
come al (6,4) (5,5)
sono al (18,5) (5,5)
con tuoi (16,1) (9,5)
piu' Maria (13,2) (15,2)
Maria mano (15,2) (7,5)
piu' mano (13,2) (7,5)
sui artisti (10,4) (9,6)
sempre artisti (13,4) (9,6)
sui sempre (10,4) (13,4)
lo meno (18,1) (13,5)
lo cuore (18,1) (12,3)
cuore meno (12,3) (13,5)
lo canzoni (18,1) (11,3)
canzoni meno (11,3) (13,5)
cuore canzoni (12,3) (11,3)
lo America (18,1) (10,3)
America meno (10,3) (13,5)
cuore America (12,3) (10,3)
canzoni America (11,3) (10,3)
sopra di (8,4) (16,5)
nella di (7,4) (16,5)
canarino di (8,3) (16,5)
canarino sopra (8,3) (8,4)
canarino nella (8,3) (7,4)
sopra nella (8,4) (7,4)
Italia di (14,2) (16,5)
Italia sopra (14,2) (8,4)
Italia canarino (14,2) (8,3)
Italia nella (14,2) (7,4)
gli amore (16,2) (11,5)
la amore (8,5) (11,5)
cantare amore (3,2) (11,5)
gli la (16,2) (8,5)
gli cantare (16,2) (3,2)
cantare la (3,2) (8,5)
ci suore (18,4) (13,6)
buongiorno ci (17,1) (18,4)
buongiorno suore (17,1) (13,6)
Buongiorno ci (15,1) (18,4)
buongiorno Buongiorno (17,1) (15,1)
Buongiorno suore (15,1) (13,6)
Lasciatemi dente (3,1) (5,6)
un il (8,2) (12,2)
il chitarra (12,2) (2,3)
un chitarra (8,2) (2,3)
partigiano occhi (6,3) (16,3)
sciatemi occhi (1,1) (16,3)
le occhi (11,2) (16,3)
le partigiano (11,2) (6,3)
sciatemi le (1,1) (11,2)
sciatemi partigiano (1,1) (6,3)
sai donne (18,2) (13,3)
e donne (8,1) (13,3)
donne in (13,3) (2,4)
e sai (8,1) (18,2)
e in (8,1) (2,4)
sai in (18,2) (2,4)
lami20j@debian:~/trash$
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
22 juin 2008 à 23:52
22 juin 2008 à 23:52
Bizarre chez moi non, il maffiche d'autres infos :(
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
23 juin 2008 à 00:00
23 juin 2008 à 00:00
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "table_hash.h"
#define TAILLEHASH 307
int main()
{
FILE *F;
char mot[100];
int i;
unsigned int cle;
int nl, pos;
nl=pos=1;
char c;
int seuil;
Liste **L;
Liste **TableHash;
TableHash = (Liste **) malloc (TAILLEHASH * sizeof(Liste *));
for(i=0;i<TAILLEHASH;++i)
TableHash[i] = NULL;
F=fopen("C:\\Users\\cyrine\\Documents\\text.txt","r");
InsertionTableHash(F,TableHash);
fclose(F);
printf(" la liste filtré");
printf("\n");
printf("\n");
AfficherTableHash(TableHash);
printf("voila les deux séquences");
jointure(TableHash);
printf("Chercher mot : ");
scanf("%s",mot);
if(cle = ChercherMotDansTableHash(TableHash,mot))
printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot));
else
printf("%s inexistant dans la table de hash\n");
scanf("%c",c);
return 0;
}
void ParcourirElementTableHash(Liste **TableHash,char *mot){
int cle;
Liste *p;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(mot,p->mot) == 0){
printf("%s : ",mot);
AfficherCoordonnees(p->c);
}
}
void jointure(Liste **TableHash){
int i;
Liste *p,*pp;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i]!=NULL){
for(p=TableHash[i];p!=NULL;p=p->suivant)
for(pp=TableHash[i];pp!=NULL;pp=pp->suivant)
if(strcmp(p->mot,pp->mot) < 0){
if(p->c->pos < pp->c->pos)
printf("%s %s (%d,%d) (%d,%d)\n",
p->mot,pp->mot,
p->c->nl,p->c->pos,
pp->c->nl,pp->c->pos);
else
printf("%s %s (%d,%d) (%d,%d)\n",
pp->mot,p->mot,
pp->c->nl,pp->c->pos,
p->c->nl,p->c->pos);
}
}
}
Liste *InsertionEnTete(Liste *L,char *mot){
Liste *nouveau;
nouveau = (Liste *) malloc (sizeof(Liste));
strcpy(nouveau->mot,mot);
nouveau->suivant = L;
nouveau->c = NULL;
return nouveau;
}
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 AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
printf("\n");
}
}
void AfficherCoordonnees(Coordonnees *C){
Coordonnees *p;
for(p=C;p!=NULL;p=p->suivant)
printf(" (%d,%d) ",p->nl,p->pos);
printf("\n");
}
void AfficheMot(char *mot){
printf("%s ",mot);
}
unsigned int hash_cle(char *mot){
unsigned int val = 0;
for(;*mot!='\0';++mot)
val = *mot + 31 * val;
return val % TAILLEHASH;
}
void InsertionTableHash(FILE *F, Liste **TableHash){
int nl,pos,i;
unsigned int cle;
char mot[100];
Liste *p;
nl=pos=1;
while(fscanf(F,"%s",mot)==1){
cle = hash_cle(mot);
if(! ChercherMotDansTableHash(TableHash,mot))
TableHash[cle] = InsertionEnTete(TableHash[cle],mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant){
if(strcmp(p->mot,mot)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
}
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
}
}
void AfficherTableHash(Liste **TableHash){
int i;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL){
printf("Conteneur %d : ",i);
AfficherListe(TableHash[i]);
printf("-------------------------------------------------\n");
}
}
unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot){
Liste *p;
unsigned int cle;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(p->mot,mot)==0)
return 1;
return 0;
}
void PosLigne(FILE *F,Liste **TableHash){
char s[50];
int nl,pos,i;
Liste *p;
nl=pos=1;
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)
if(strcmp(p->mot,s)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
}
printf("\n");
}
stp Lami essai avec ce code la et montre moi les resultats
#include<stdlib.h>
#include<string.h>
#include "table_hash.h"
#define TAILLEHASH 307
int main()
{
FILE *F;
char mot[100];
int i;
unsigned int cle;
int nl, pos;
nl=pos=1;
char c;
int seuil;
Liste **L;
Liste **TableHash;
TableHash = (Liste **) malloc (TAILLEHASH * sizeof(Liste *));
for(i=0;i<TAILLEHASH;++i)
TableHash[i] = NULL;
F=fopen("C:\\Users\\cyrine\\Documents\\text.txt","r");
InsertionTableHash(F,TableHash);
fclose(F);
printf(" la liste filtré");
printf("\n");
printf("\n");
AfficherTableHash(TableHash);
printf("voila les deux séquences");
jointure(TableHash);
printf("Chercher mot : ");
scanf("%s",mot);
if(cle = ChercherMotDansTableHash(TableHash,mot))
printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot));
else
printf("%s inexistant dans la table de hash\n");
scanf("%c",c);
return 0;
}
void ParcourirElementTableHash(Liste **TableHash,char *mot){
int cle;
Liste *p;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(mot,p->mot) == 0){
printf("%s : ",mot);
AfficherCoordonnees(p->c);
}
}
void jointure(Liste **TableHash){
int i;
Liste *p,*pp;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i]!=NULL){
for(p=TableHash[i];p!=NULL;p=p->suivant)
for(pp=TableHash[i];pp!=NULL;pp=pp->suivant)
if(strcmp(p->mot,pp->mot) < 0){
if(p->c->pos < pp->c->pos)
printf("%s %s (%d,%d) (%d,%d)\n",
p->mot,pp->mot,
p->c->nl,p->c->pos,
pp->c->nl,pp->c->pos);
else
printf("%s %s (%d,%d) (%d,%d)\n",
pp->mot,p->mot,
pp->c->nl,pp->c->pos,
p->c->nl,p->c->pos);
}
}
}
Liste *InsertionEnTete(Liste *L,char *mot){
Liste *nouveau;
nouveau = (Liste *) malloc (sizeof(Liste));
strcpy(nouveau->mot,mot);
nouveau->suivant = L;
nouveau->c = NULL;
return nouveau;
}
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 AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
printf("\n");
}
}
void AfficherCoordonnees(Coordonnees *C){
Coordonnees *p;
for(p=C;p!=NULL;p=p->suivant)
printf(" (%d,%d) ",p->nl,p->pos);
printf("\n");
}
void AfficheMot(char *mot){
printf("%s ",mot);
}
unsigned int hash_cle(char *mot){
unsigned int val = 0;
for(;*mot!='\0';++mot)
val = *mot + 31 * val;
return val % TAILLEHASH;
}
void InsertionTableHash(FILE *F, Liste **TableHash){
int nl,pos,i;
unsigned int cle;
char mot[100];
Liste *p;
nl=pos=1;
while(fscanf(F,"%s",mot)==1){
cle = hash_cle(mot);
if(! ChercherMotDansTableHash(TableHash,mot))
TableHash[cle] = InsertionEnTete(TableHash[cle],mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant){
if(strcmp(p->mot,mot)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
}
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
}
}
void AfficherTableHash(Liste **TableHash){
int i;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL){
printf("Conteneur %d : ",i);
AfficherListe(TableHash[i]);
printf("-------------------------------------------------\n");
}
}
unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot){
Liste *p;
unsigned int cle;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(p->mot,mot)==0)
return 1;
return 0;
}
void PosLigne(FILE *F,Liste **TableHash){
char s[50];
int nl,pos,i;
Liste *p;
nl=pos=1;
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)
if(strcmp(p->mot,s)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
}
printf("\n");
}
stp Lami essai avec ce code la et montre moi les resultats
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 567
23 juin 2008 à 00:40
23 juin 2008 à 00:40
Ben, pourquoi n'utilises-tu mon code?
chez toi je vois
if(ChercherMotDansTableHash(TableHash,mot))
printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot));
else
printf("%s inexistant dans la table de hash\n");
et chez moi c'est
chez toi je vois
if(ChercherMotDansTableHash(TableHash,mot))
printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot));
else
printf("%s inexistant dans la table de hash\n");
et chez moi c'est
if(ChercherMotDansTableHash(TableHash,mot))
ParcourirElementTableHash(TableHash,mot);
else
printf("%s inexistant dans la table de hash\n");
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
23 juin 2008 à 11:03
23 juin 2008 à 11:03
Bonjour LAMI,
c'est le meme problelme meme si jai changé l'instruction comme tu ma dit.
t'a une idée svp.
merci
c'est le meme problelme meme si jai changé l'instruction comme tu ma dit.
t'a une idée svp.
merci
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
23 juin 2008 à 11:31
23 juin 2008 à 11:31
Ah, c'est bon :)
je l'ai trouvé: yavait un probleme au niveau du fichier texte ( au niveau des point finaux) ;)
mais bref, je voudrais savoir Lami, si c la meme chose por toi pour quoi il affiche la liste des infos de chaque element dans un ordre décroissant
Buongiorno (16,1), (14,1), (13,8), (9,1), (5,1)
?
merci
je l'ai trouvé: yavait un probleme au niveau du fichier texte ( au niveau des point finaux) ;)
mais bref, je voudrais savoir Lami, si c la meme chose por toi pour quoi il affiche la liste des infos de chaque element dans un ordre décroissant
Buongiorno (16,1), (14,1), (13,8), (9,1), (5,1)
?
merci
stroumpf
Messages postés
289
Date d'inscription
mardi 17 juin 2008
Statut
Membre
Dernière intervention
1 mars 2009
2
23 juin 2008 à 12:40
23 juin 2008 à 12:40
Bonjour Lami, est qu'on peut améliorer plus cette fonction de hachage de sorte que 2 mots n'aappartiennent au meme element de la table de hachage.
merci
J'attends vos réponses
:)
merci
J'attends vos réponses
:)
22 juin 2008 à 16:38
22 juin 2008 à 16:41
merci
22 juin 2008 à 16:43
je te conseille de créer une autre table de hash pour ça ;-)
22 juin 2008 à 16:45
22 juin 2008 à 16:46