Téléverser les fichiers vers "/"

This commit is contained in:
2026-10-07 12:03:44 +02:00
parent 6165b37b51
commit 103ae34f6a
2 changed files with 117 additions and 10 deletions
+45 -10
View File
@@ -217,6 +217,16 @@ void normaliser_ligne(char *ligne)
}
}
#define TAILLE_HACHAGE 65536 // Puissance de 2, bien plus grand que MAX_MOTS_UNIQUES
unsigned int hacher(const char *mot) {
unsigned int h = 5381;
for (int i = 0; mot[i] != '\0'; i++) {
h = ((h << 5) + h) + (unsigned char)mot[i]; /* équivalent à h * 33 + c */
}
return h;
}
/* ------------------------------------------------------------------------ */
/* Comptage des mots */
/* ------------------------------------------------------------------------ */
@@ -235,29 +245,44 @@ int est_mot_vide(const char *mot)
return 0;
}
int chercher_mot(Entree *table, int nb_entrees, const char *mot)
int chercher_mot(Entree *table, int *table_hachage, const char *mot, int *pos_libre)
{
for (int i = 0; i < nb_entrees; i++) {
if (egales(table[i].mot, mot))
return i;
unsigned int h = hacher(mot) & (TAILLE_HACHAGE - 1);
while (table_hachage[h] != -1) {
int indice = table_hachage[h];
if (egales(table[indice].mot, mot)) {
return indice; // Le mot a été trouvé
}
h = (h + 1) & (TAILLE_HACHAGE - 1); // Résolution de collision (sondage linéaire)
}
// Si on arrive ici, le mot n'est pas dans la table
if (pos_libre != NULL) {
*pos_libre = h; // On sauvegarde l'emplacement libre pour l'insertion
}
return -1;
}
void compter_mot(Entree *table, int *nb_entrees, const char *mot)
void compter_mot(Entree *table, int *nb_entrees, const char *mot, int *table_hachage)
{
int indice = chercher_mot(table, *nb_entrees, mot);
int pos_libre;
int indice = chercher_mot(table, table_hachage, mot, &pos_libre);
if (indice >= 0) {
table[indice].occurrences++;
} else {
// Insertion du nouveau mot à la suite de la table classique
copier(table[*nb_entrees].mot, mot);
table[*nb_entrees].occurrences = 1;
// On mémorise son index dans la table de hachage
table_hachage[pos_libre] = *nb_entrees;
(*nb_entrees)++;
}
}
void compter_mots_ligne(Entree *table, int *nb_entrees, const char *ligne,
long *nb_mots_total)
long *nb_mots_total, int *table_hachage)
{
char mot[TAILLE_MOT];
int i = 0;
@@ -275,7 +300,7 @@ void compter_mots_ligne(Entree *table, int *nb_entrees, const char *ligne,
if (k > 0) {
(*nb_mots_total)++;
if (!est_mot_vide(mot))
compter_mot(table, nb_entrees, mot);
compter_mot(table, nb_entrees, mot, table_hachage);
}
}
}
@@ -319,8 +344,11 @@ double longueur_moyenne(const Entree *table, int n)
double ecart_type_longueur(const Entree *table, int n)
{
double somme = 0.0;
// On calcule la moyenne une seule fois avant la boucle
double moyenne = longueur_moyenne(table, n);
for (int i = 0; i < n; i++) {
double ecart = longueur(table[i].mot) - longueur_moyenne(table, n);
double ecart = longueur(table[i].mot) - moyenne;
somme += ecart * ecart;
}
return somme / n; /* variance ; la racine est prise dans main */
@@ -412,10 +440,16 @@ int main(void)
normaliser_ligne(lignes[i]);
Entree *table = malloc(MAX_MOTS_UNIQUES * sizeof(Entree));
int *table_hachage = malloc(TAILLE_HACHAGE * sizeof(int));
for (int i = 0; i < TAILLE_HACHAGE; i++) {
table_hachage[i] = -1; // -1 indique une case vide
}
int nb_entrees = 0;
long nb_mots_total = 0;
for (int i = 0; i < nb_lignes; i++)
compter_mots_ligne(table, &nb_entrees, lignes[i], &nb_mots_total);
compter_mots_ligne(table, &nb_entrees, lignes[i], &nb_mots_total, table_hachage);
trier_entrees(table, nb_entrees);
@@ -446,6 +480,7 @@ int main(void)
free(rapport);
free(table);
free(table_hachage);
for (int i = 0; i < nb_lignes; i++)
free(lignes[i]);
free(lignes);