This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
generated from gomu-gomu/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,51 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include "tableau_hachage.h" | ||
|
||
int main() | ||
{ | ||
printf("Travail à faire (partie n°4)"); | ||
return EXIT_SUCCESS; | ||
} | ||
char texte[1000]; | ||
TableauHashage *tableauHashage = creerTableauHashage(1000); | ||
|
||
printf("Saisir le texte (mots séparés par des espaces) :\n> "); | ||
fgets(texte, sizeof(texte), stdin); | ||
|
||
char *mots = strtok(texte, " "); | ||
while (mots != NULL) | ||
{ | ||
for (size_t i = 0; i < strlen(mots); i++) | ||
{ | ||
mots[i] = tolower(mots[i]); | ||
} | ||
|
||
if (mots[strlen(mots) - 1] == '\n') | ||
{ | ||
mots[strlen(mots) - 1] = '\0'; | ||
} | ||
|
||
insererOuModifier(tableauHashage, mots); | ||
mots = strtok(NULL, " "); | ||
} | ||
|
||
// A - Afficher les mots du texte avec leurs fréquences. | ||
printf("Fréquences des mots :\n"); | ||
afficherFrequences(tableauHashage); | ||
|
||
// B - Afficher le mot du texte le moins fréquent (c-à-d celui avec la plus petite fréquence). | ||
char *minMot = chercherFrequenceMinimum(tableauHashage); | ||
printf("Le mot ayant la fréquence minimale : %s\n", minMot); | ||
|
||
// C - Afficher la fréquence d’un mot dans le texte. Ce mot sera saisi au clavier. | ||
char motrecherche[50]; | ||
printf("Entrez un mot pour trouver sa fréquence :\n> "); | ||
scanf("%s", motrecherche); | ||
|
||
int frequence = chercherFrequenceMot(tableauHashage, motrecherche); | ||
printf("Fréquence de %s : %d.\n", motrecherche, frequence); | ||
|
||
nettoyage(tableauHashage); | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "tableau_hachage.h" | ||
|
||
int hacher(char *cle, int taille) | ||
{ | ||
int valeur_hashage = 0; | ||
|
||
while (*cle != '\0') | ||
{ | ||
valeur_hashage += *cle; | ||
cle++; | ||
} | ||
|
||
return valeur_hashage % taille; | ||
} | ||
|
||
CleValeur *creeCleValeur(char *cle) | ||
{ | ||
CleValeur *couple = (CleValeur *)malloc(sizeof(CleValeur)); | ||
couple->mot = strdup(cle); | ||
couple->frequence = 1; | ||
|
||
return couple; | ||
} | ||
|
||
Noeud *creerNoeud(CleValeur *donnee) | ||
{ | ||
Noeud *noeud = (Noeud *)malloc(sizeof(Noeud)); | ||
noeud->donnee = *donnee; | ||
|
||
noeud->suivant = NULL; | ||
return noeud; | ||
} | ||
|
||
TableauHashage *creerTableauHashage(int taille) | ||
{ | ||
TableauHashage *tableauHashage = (TableauHashage *)malloc(sizeof(TableauHashage)); | ||
tableauHashage->taille = taille; | ||
tableauHashage->tableau = (Noeud **)malloc(sizeof(Noeud *) * taille); | ||
|
||
for (int i = 0; i < taille; i++) | ||
{ | ||
tableauHashage->tableau[i] = NULL; | ||
} | ||
|
||
return tableauHashage; | ||
} | ||
|
||
void insererOuModifier(TableauHashage *tableauHashage, char *mot) | ||
{ | ||
int indice = hacher(mot, tableauHashage->taille); | ||
Noeud *noeud = tableauHashage->tableau[indice]; | ||
|
||
while (noeud != NULL) | ||
{ | ||
if (strcmp(noeud->donnee.mot, mot) == 0) | ||
{ | ||
noeud->donnee.frequence++; | ||
return; | ||
} | ||
|
||
noeud = noeud->suivant; | ||
} | ||
|
||
CleValeur *couple = creeCleValeur(mot); | ||
Noeud *noeudCree = creerNoeud(couple); | ||
|
||
noeudCree->suivant = tableauHashage->tableau[indice]; | ||
tableauHashage->tableau[indice] = noeudCree; | ||
} | ||
|
||
void afficherFrequences(TableauHashage *tableauHashage) | ||
{ | ||
for (int i = 0; i < tableauHashage->taille; i++) | ||
{ | ||
Noeud *noeud = tableauHashage->tableau[i]; | ||
|
||
while (noeud != NULL) | ||
{ | ||
printf("%s: %d\n", noeud->donnee.mot, noeud->donnee.frequence); | ||
noeud = noeud->suivant; | ||
} | ||
} | ||
} | ||
|
||
char *chercherFrequenceMinimum(TableauHashage *tableauHashage) | ||
{ | ||
char *minMot = NULL; | ||
int minFrequence = -1; | ||
|
||
for (int i = 0; i < tableauHashage->taille; i++) | ||
{ | ||
Noeud *noeud = tableauHashage->tableau[i]; | ||
|
||
while (noeud != NULL) | ||
{ | ||
if (minFrequence == -1 || noeud->donnee.frequence < minFrequence) | ||
{ | ||
minFrequence = noeud->donnee.frequence; | ||
minMot = noeud->donnee.mot; | ||
} | ||
|
||
noeud = noeud->suivant; | ||
} | ||
} | ||
|
||
return minMot; | ||
} | ||
|
||
int chercherFrequenceMot(TableauHashage *tableauHashage, char *mot) | ||
{ | ||
int indice = hacher(mot, tableauHashage->taille); | ||
Noeud *noeud = tableauHashage->tableau[indice]; | ||
|
||
while (noeud != NULL) | ||
{ | ||
if (strcmp(noeud->donnee.mot, mot) == 0) | ||
{ | ||
return noeud->donnee.frequence; | ||
} | ||
|
||
noeud = noeud->suivant; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void nettoyage(TableauHashage *tableauHashage) | ||
{ | ||
for (int i = 0; i < tableauHashage->taille; i++) | ||
{ | ||
Noeud *noeud = tableauHashage->tableau[i]; | ||
|
||
while (noeud != NULL) | ||
{ | ||
Noeud *suivant = noeud->suivant; | ||
|
||
free(noeud->donnee.mot); | ||
free(noeud); | ||
|
||
noeud = suivant; | ||
} | ||
} | ||
|
||
free(tableauHashage->tableau); | ||
free(tableauHashage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef A_TABLEAU_HACHAGE | ||
#define A_TABLEAU_HACHAGE | ||
|
||
typedef struct | ||
{ | ||
char *mot; | ||
int frequence; | ||
} CleValeur; | ||
|
||
typedef struct Noeud | ||
{ | ||
CleValeur donnee; | ||
struct Noeud *suivant; | ||
} Noeud; | ||
|
||
typedef struct | ||
{ | ||
int taille; | ||
Noeud **tableau; | ||
} TableauHashage; | ||
|
||
int hacher(char *cle, int taille); | ||
|
||
CleValeur *creeCleValeur(char *cle); | ||
|
||
Noeud *creerNoeud(CleValeur *donnee); | ||
|
||
TableauHashage *creerTableauHashage(int taille); | ||
|
||
void insererOuModifier(TableauHashage *tableauHashage, char *mot); | ||
|
||
void afficherFrequences(TableauHashage *tableauHashage); | ||
|
||
char *chercherFrequenceMinimum(TableauHashage *tableauHashage); | ||
|
||
int chercherFrequenceMot(TableauHashage *tableauHashage, char *mot); | ||
|
||
void nettoyage(TableauHashage *tableauHashage); | ||
|
||
#endif |