forked from menault/2026_TD1_DEV51_Qualite_Algo
Optimisation des performances en mémorisant la longueur des mots et en supprimant les parcours redondants
This commit is contained in:
+20
-18
@@ -46,14 +46,6 @@ unsigned long alea(void)
|
||||
/* Fonctions utilitaires sur les chaînes */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int longueur(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n] != '\0')
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
void copier(char *dest, const char *src)
|
||||
{
|
||||
int i = 0;
|
||||
@@ -83,9 +75,14 @@ int comparer(const char *a, const char *b)
|
||||
return (unsigned char)a[i] - (unsigned char)b[i];
|
||||
}
|
||||
|
||||
void ajouter(char *dest, const char *src)
|
||||
/* position indique la fin de dest et avance avec les caractères ajoutés. */
|
||||
void ajouter(char *dest, const char *src, size_t *position)
|
||||
{
|
||||
copier(dest + longueur(dest), src);
|
||||
size_t fin = *position;
|
||||
for (size_t i = 0; src[i] != '\0'; i++)
|
||||
dest[fin++] = src[i];
|
||||
dest[fin] = '\0';
|
||||
*position = fin;
|
||||
}
|
||||
|
||||
char en_minuscule(char c)
|
||||
@@ -128,9 +125,10 @@ char **generer_vocabulaire(void)
|
||||
for (int i = 0; i < NB_MOTS_VOCAB; i++) {
|
||||
vocab[i] = malloc(TAILLE_MOT);
|
||||
vocab[i][0] = '\0';
|
||||
size_t position = 0;
|
||||
int nb_syllabes = 2 + alea() % 3;
|
||||
for (int j = 0; j < nb_syllabes; j++)
|
||||
ajouter(vocab[i], SYLLABES[alea() % NB_SYLLABES]);
|
||||
ajouter(vocab[i], SYLLABES[alea() % NB_SYLLABES], &position);
|
||||
}
|
||||
return vocab;
|
||||
}
|
||||
@@ -211,7 +209,7 @@ char **decouper_lignes(const char *texte, int *nb_lignes)
|
||||
|
||||
void normaliser_ligne(char *ligne)
|
||||
{
|
||||
for (int i = 0; i < longueur(ligne); i++) {
|
||||
for (int i = 0; ligne[i] != '\0'; i++) {
|
||||
if (est_lettre(ligne[i]))
|
||||
ligne[i] = en_minuscule(ligne[i]);
|
||||
else
|
||||
@@ -226,6 +224,7 @@ void normaliser_ligne(char *ligne)
|
||||
typedef struct {
|
||||
char mot[TAILLE_MOT];
|
||||
int occurrences;
|
||||
int taille;
|
||||
} Entree;
|
||||
|
||||
int est_mot_vide(const char *mot)
|
||||
@@ -263,7 +262,8 @@ int chercher_mot(const Entree *table, const int *index_mots, const char *mot)
|
||||
return -(position + 1);
|
||||
}
|
||||
|
||||
void compter_mot(Entree *table, int *nb_entrees, int *index_mots, const char *mot)
|
||||
void compter_mot(Entree *table, int *nb_entrees, int *index_mots,
|
||||
const char *mot, int taille)
|
||||
{
|
||||
int indice = chercher_mot(table, index_mots, mot);
|
||||
if (indice >= 0) {
|
||||
@@ -278,6 +278,7 @@ void compter_mot(Entree *table, int *nb_entrees, int *index_mots, const char *mo
|
||||
indice = *nb_entrees;
|
||||
copier(table[indice].mot, mot);
|
||||
table[indice].occurrences = 1;
|
||||
table[indice].taille = taille;
|
||||
index_mots[position] = indice + 1;
|
||||
(*nb_entrees)++;
|
||||
}
|
||||
@@ -302,7 +303,7 @@ void compter_mots_ligne(Entree *table, int *nb_entrees, int *index_mots,
|
||||
if (k > 0) {
|
||||
(*nb_mots_total)++;
|
||||
if (!est_mot_vide(mot))
|
||||
compter_mot(table, nb_entrees, index_mots, mot);
|
||||
compter_mot(table, nb_entrees, index_mots, mot, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -339,7 +340,7 @@ double longueur_moyenne(const Entree *table, int n)
|
||||
{
|
||||
long total = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
total += longueur(table[i].mot);
|
||||
total += table[i].taille;
|
||||
return (double)total / n;
|
||||
}
|
||||
|
||||
@@ -348,7 +349,7 @@ double ecart_type_longueur(const Entree *table, int n, double moyenne)
|
||||
{
|
||||
double somme = 0.0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
double ecart = longueur(table[i].mot) - moyenne;
|
||||
double ecart = table[i].taille - moyenne;
|
||||
somme += ecart * ecart;
|
||||
}
|
||||
return somme / n; /* variance ; la racine est prise dans main */
|
||||
@@ -399,12 +400,13 @@ char *construire_rapport(const Entree *table, int n)
|
||||
char *rapport = malloc((size_t)n * 64 + 256);
|
||||
char ligne[128];
|
||||
rapport[0] = '\0';
|
||||
size_t position = 0;
|
||||
|
||||
ajouter(rapport, "rang;mot;occurrences\n");
|
||||
ajouter(rapport, "rang;mot;occurrences\n", &position);
|
||||
for (int i = 0; i < n; i++) {
|
||||
snprintf(ligne, sizeof(ligne), "%d;%s;%d\n",
|
||||
i + 1, table[i].mot, table[i].occurrences);
|
||||
ajouter(rapport, ligne);
|
||||
ajouter(rapport, ligne, &position);
|
||||
}
|
||||
return rapport;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user