Bonjour,
bon là je refais le travail depuis le debut
je vais faire une focntion qui va retourner un pointeur sur une structure table de hachage qui va contenir les 2 table de hacha ge remplie depuis le fichier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 int TraiterFichier(char const *langue, int *pNbLignes) { int iSeq; int bEnregistrementOK = 1; int nbLignes = 0; TableHachageMot *DblTableHash; TableHachageMot *DblTableHash2; struct Chainon_fbtAllocator *pAlloc2seq = Chainon_CreateFbtAllocator(1000000); struct Coord_fbtAllocator *pAllocCoord2seq = Coord_CreateFbtAllocator(1000000); AfficherCarac(stdout, 79, '-'); printf("\nTraitement fichier pour langue : %s\n", langue); if(pNbLignes != NULL) *pNbLignes = 0; *pMaxSequence = 0; DblTableHash = CreerDoubleTableHachage(); DblTableHash2 = CreerDoubleTableHachage(); SetAllocateurChainon(Chainon_FbtAllocToIface(pAlloc2seq)); SetAllocateurCoord(Coord_FbtAllocToIface(pAllocCoord2seq)); if(LireFichierEntree(langue, DblTableHash, DblTableHash2 ,&nbLignes)<0) return EXIT_FAILURE; { size_t *pStats = NULL; size_t nStats = 0; if(statistiques_double_table(DblTableHash, &pStats, &nStats)>=0) { #if 0 size_t iStat; #endif printf("--- Statistiques de la table de hachage ---\n"); while(nStats>0 && pStats[nStats-1]==0) nStats--; printf("Nb. de cases vides : %llu\n", (long long)pStats[0]); printf("Nb. maximum de collisions : %llu\n", (long long)nStats); #if 0 for(iStat=0 ; iStat<nStats ; iStat++) { if(pStats[iStat]!=0) printf("%llu : %llu\n", (long long)iStat, (long long)(pStats[iStat])); } #endif putchar('\n'); } free(pStats), pStats=NULL; } if(pNbLignes != NULL) *pNbLignes = nbLignes; printf("Calcul des nombres de lignes...\n"); CalculerNombreLignesDoubleTable(DblTableHash); CalculerNombreLignesDoubleTable(DblTableHash2); return (bEnregistrementOK ? EXIT_SUCCESS : EXIT_FAILURE);
BOnsoir,
là je me demande encore, je fais appel de la fonction TraiterFichier deux fois dans le main: une fois pour la langue francaise pour construire les 2 table de hachage, et une autre fois pour la langue anglais.
Là je me demade si lors du 2eme appele les 2 ancienne table de hachage s'ecrasent . donc on aura à la fin 2 tables de hachage en anglais?
Des reponses à ma question SVP?
merci
Le problème majeur, c'est que les tables de hachage sont perdues à la fin de la fonction TraiterFichier().
De plus, tu passes deux fois la même table à la fonction LireFichierEntree(), au lieu de passer 1 et 2...
Dans ton cas, il n'y a pas reellement le choix.
Il faut declarer les pointeur non pas dans la fonction appelé mais dans la fonction appelante.
Ainsi, tu fais un passage d'argument par adresse (un pointeur en gros), ce qui te permet de conserver tes variables.
Donc :
Devient ceci (ou quelque chose de tres proche)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 int main () { ... TraiterFichier("francais", 50); } int TraiterFichier(char const *langue, int *pNbLignes) { ..... TableHachageMot *DblTableHash; TableHachageMot *DblTableHash2; ..... }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 int main () { ... TableHachageMot *DblTableHash = NULL; TableHachageMot *DblTableHash2 = NULL; TraiterFichier(DblTableHash, DblTableHash2, "francais", 50); } int TraiterFichier(TableHachageMot *DblTableHash, TableHachageMot *DblTableHash2, char const *langue, int *pNbLignes) { ..... }
Un conseil, n'oublie pas d'initialiser tout le temps tes pointeurs ! A la bonne adresse si tu la connais, sinon a NULL.
Merci SoftEavans,
Comme d'habitude avec tes reponses efficaces
Amitiés
Cyrine
Merci cyrine, mais je n'ai fait qu'exprimer ce que Médinoc disait.
NOTE: Plutôt que les mettre à NULL dans le main, appeler la fonction de création dedans (au lieu de le faire dans TraiterFichier()).
Effectivement, on peut faire l'allocation dynamique directement, je n'avais pas vu la ligne.Un conseil, n'oublie pas d'initialiser tout le temps tes pointeurs ! A la bonne adresse si tu la connais, sinon a NULL.
C'est bien meilleur que de mettre NULL, merci Médinoc.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 int main () { ... TableHachageMot *DblTableHash = CreerDoubleTableHachage(); TableHachageMot *DblTableHash2 = CreerDoubleTableHachage(); TraiterFichier(DblTableHash, DblTableHash2, "francais", 50); } int TraiterFichier(TableHachageMot *DblTableHash, TableHachageMot *DblTableHash2, char const *langue, int *pNbLignes) { ..... }
Cyrine << Tu declare deux table de hachage, mais tu n'en voulait qu'une a la fin ? Si la deuxieme table est delarer juste temporairement afin de remplir ou faire des operation sur la premiere, une maniere plus propre et plus logique serait de la declarer dans TraiterFichier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 int main () { ... /* On ne veut conserver QUE la premiere table */ TableHachageMot *DblTableHash = CreerDoubleTableHachage(); TraiterFichier(DblTableHash, "francais", 50); } int TraiterFichier(TableHachageMot *DblTableHash, char const *langue, int *pNbLignes) { /* La table sera perdu a la fin */ TableHachageMot *DblTableHash2 = CreerDoubleTableHachage(); ..... /* Ne pas oublier de liberer la memoire pour DblTableHash2 */ }
Merci SoftEavans et Médinoc, en fait j' veux les deux table de hachage: pour stoker les mots et l'autre pour stocker les couple de mots.
Là j'ai un probleme, apres le remplissage des table de hachage, j'ai voulu parcourir chacun pour afficher le contenu, mais là il n'affiche rien comme si la table est vide
une idée svp?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 int main(int argc, char *argv[]) { int i,j; TableHachageMot *DblTableHash1en = CreerDoubleTableHachage(); TableHachageMot *DblTableHash2en = CreerDoubleTableHachage(); TableHachageMot *DblTableHash1fr = CreerDoubleTableHachage(); TableHachageMot *DblTableHash2fr = CreerDoubleTableHachage(); int nbLignes1=0; #ifdef _MSC_VER { int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF; _CrtSetDbgFlag( tmpFlag ); } #endif if(TraiterFichier(DblTableHash1fr, DblTableHash2fr, "fr", &nbLignes1)!=EXIT_SUCCESS) return EXIT_FAILURE; printf("génération des triggers inter-langues"); for(i=0;i<TAILLEHASH;i++) { printf("%s",GetMot(DblTableHash2fr[i])); } return EXIT_SUCCESS; }
merci
Il compile sans warning, cet appel à GetMot()?
Tu as une double table de hachage, c'est donc un tableau de tableux de listes chaînées. Là, tu ne fais qu'une seule boucle...
Comme l'a dit Médinoc, il nous faudrait GetMot(), mais ca semble deja louche a la base.
Il faut que tu configure le compilo C avec au minimum [-W] et [-Wall].
EDIT
Avec DblTableHash2fr[i], c'est une ADRESSE que tu envoie, plus precisement l'adresse qui pointe sur la premiere case d'un tableau. Il ne faut pas faire "%s" la dessus !.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 for(i=0;i<TAILLEHASH;i++) { printf("%s",GetMot(DblTableHash2fr[i])); }
Note pour ceux qui ne savent pas: La table de hachage est double car elle hache les mots selon deux clés différentes; vu qu'il n'y a pas de comptage de références, il était plus facile de les mettre bout-à-bout que de faire deux tables de hachage semi-indépendantes. De plus, la clé de la première table est incluse dans celle de la seconde.
(sur demande, je peux ajouter un schéma)
il m'affiche en plus une erreur là :/home/cyrine/Bureau/projet Final/projet version 11 Juin/main.c|983|erreur: incompatible type for argument 1 of ‘GetMot’|
Code : Sélectionner tout - Visualiser dans une fenêtre à part printf("%s",GetMot(DblTableHash2fr[i][j]));
voilà la fonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 char const * GetMot(ChainonMot const *pc) { assert(pc!=NULL); return pc->mot; }
Pourrait-on avoir le code reproduit l'erreur (la boucle je pense) ainsi que le code de GetMot() s'il te plait ?
Car tel quel, le compilo nous informe qu'il y a incompatibilité entre les types passer, et on peut pas t'eclairer tant qu'on ne voit pas ce que tu as coder.
EDIT : désolé, je n'avais pas vu (tu n'aurai pas editer par hasard ?)
Desolé, je ne retrouve pas la definition de la structure "TableHachageMot" et celle de "ChainonMot". Pourrai tu les mettre s'il te plait ?
ok , merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 typedef struct st_chainonMot ChainonMot; typedef struct st_liste { ChainonMot *pPremier; } ListeMot; typedef ListeMot TableHachageMot[TAILLEHASH]; struct st_chainonMot { struct st_chainonMot *suivant; int nbLignes; size_t cchTailleMot; ListeCoord coords; char mot[1]; };
Oula !
C'est un peu confu dans ma tete.
Voici ta structure ChainonMot
Le truc qui m'as sauté aux yeux, c'est : char mot[1];
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 typedef struct st_chainonMot ChainonMot; struct st_chainonMot { struct st_chainonMot *suivant; int nbLignes; size_t cchTailleMot; ListeCoord coords; char mot[1]; };
Tu declare un tableau statique de char, tableau qui contient ... UNE case ???
Pourquoi ?
Pourquoi ne pas mettre le plus simplement du monde char mot; ???
De plus, cela me semble mal nommé.
J'ai l'impression que tu as fait une liste simplement chainée
Donc, si on parcours la liste du debut jusqu'a la fin, on obtient quoi ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 struct st_chainonMot { struct st_chainonMot *suivant; .... }
Un seul mot ? Ou chaque structure contient un mot d'une lettre ??
En tout les cas, l'erreur provient d'ici :
Si tu regarde mieux ta structure, on a :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 char const * GetMot(ChainonMot const *pc) { assert(pc!=NULL); return pc->mot; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 struct st_chainonMot { struct st_chainonMot *suivant; int nbLignes; size_t cchTailleMot; ListeCoord coords; char mot[1]; };
Le type de mot est le type du return ne sont pas les meme (char[] vs char).
Il y a plusieurs moyen d'y remedier, mais je t'encourage et repenser fortmement a ta structure de base, car si celle-ci n'est pas absolu compris, clair et defendable, ben le reste ca va etre coton ....
Essaie un code de ce style:
Code C : Sélectionner tout - Visualiser dans une fenêtre à part printf("%s", GetMot(GetPremierC(&DblTableHash2fr[i][j])) );
Ou, en plus développé, affichant toute la liste:
Code C : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 ListeMot const * pListe = &DblTableHash2fr[i][j]; ChainonMot const * pcMot; for(pcMot = GetPremierC(pListe) ; pcMot!=NULL ; pcMot = GetSuivantC(pcMot)) printf("%s", GetMot(pcMot));
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager