TD01 avec FRAMERY Karl

This commit is contained in:
abraham
2026-10-07 12:09:12 +02:00
parent 6165b37b51
commit b2a9d097aa
10 changed files with 6619 additions and 12 deletions
+42 -12
View File
@@ -26,6 +26,7 @@
#define NB_MOTS_VOCAB 7000
#define TAILLE_MOT 32
#define MAX_MOTS_UNIQUES 20000
#define TAILLE_HACHAGE 65536
#define GRAINE 20261006UL
/* ------------------------------------------------------------------------ */
@@ -235,29 +236,53 @@ int est_mot_vide(const char *mot)
return 0;
}
int chercher_mot(Entree *table, int nb_entrees, const char *mot)
unsigned long hacher_mot(const char *mot)
{
for (int i = 0; i < nb_entrees; i++) {
if (egales(table[i].mot, mot))
return i;
unsigned long h = 2166136261UL;
for (int i = 0; mot[i] != '\0'; i++) {
h ^= (unsigned char)mot[i];
h *= 16777619UL;
}
return h % TAILLE_HACHAGE;
}
int chercher_mot(const Entree *table, const int *index_mots, const char *mot,
int *position)
{
unsigned long case_hachage = hacher_mot(mot);
for (int i = 0; i < TAILLE_HACHAGE; i++) {
int indice = index_mots[case_hachage] - 1;
if (indice < 0) {
*position = (int)case_hachage;
return -1;
}
if (egales(table[indice].mot, mot)) {
*position = (int)case_hachage;
return indice;
}
case_hachage = (case_hachage + 1) % TAILLE_HACHAGE;
}
*position = -1;
return -1;
}
void compter_mot(Entree *table, int *nb_entrees, const char *mot)
void compter_mot(Entree *table, int *nb_entrees, int *index_mots,
const char *mot)
{
int indice = chercher_mot(table, *nb_entrees, mot);
int position;
int indice = chercher_mot(table, index_mots, mot, &position);
if (indice >= 0) {
table[indice].occurrences++;
} else {
} else if (position >= 0 && *nb_entrees < MAX_MOTS_UNIQUES) {
copier(table[*nb_entrees].mot, mot);
table[*nb_entrees].occurrences = 1;
index_mots[position] = *nb_entrees + 1;
(*nb_entrees)++;
}
}
void compter_mots_ligne(Entree *table, int *nb_entrees, const char *ligne,
long *nb_mots_total)
void compter_mots_ligne(Entree *table, int *nb_entrees, int *index_mots,
const char *ligne, long *nb_mots_total)
{
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, index_mots, mot);
}
}
}
@@ -318,9 +343,11 @@ double longueur_moyenne(const Entree *table, int n)
double ecart_type_longueur(const Entree *table, int n)
{
double moyenne = longueur_moyenne(table, n);
double somme = 0.0;
for (int i = 0; i < n; i++) {
double ecart = longueur(table[i].mot) - longueur_moyenne(table, n);
/* 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 +439,12 @@ int main(void)
normaliser_ligne(lignes[i]);
Entree *table = malloc(MAX_MOTS_UNIQUES * sizeof(Entree));
int *index_mots = calloc(TAILLE_HACHAGE, sizeof(int));
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, index_mots, lignes[i],
&nb_mots_total);
trier_entrees(table, nb_entrees);
@@ -445,6 +474,7 @@ int main(void)
printf("\nEmpreinte du rapport : %016lx\n", empreinte(rapport));
free(rapport);
free(index_mots);
free(table);
for (int i = 0; i < nb_lignes; i++)
free(lignes[i]);