forked from menault/2026_TD3_DEV51_Qualite_Algo
optimisation de F2 en utilisant l'id comme index d'un tableau de booleens
This commit is contained in:
@@ -118,24 +118,95 @@ void trier_par_montant(Transaction *t, int n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* F2 : nombre de clients ayant au moins une transaction */
|
/* F2 : nombre de clients ayant au moins une transaction */
|
||||||
int nb_clients_distincts(const Transaction *t, int n)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int nb_clients_distincts(const Transaction *t, int n) // on utilise l'id come index
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (n <= 0) return 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// on trouve l'id/index max pr la taille du tableau
|
||||||
|
|
||||||
|
int max_id = t[0].client;
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 1; i < n; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (t[i].client > max_id)
|
||||||
|
{
|
||||||
|
|
||||||
|
max_id = t[i].client;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// creation du tableau
|
||||||
|
|
||||||
|
bool *presence = (bool *)calloc(max_id + 1, sizeof(bool));
|
||||||
|
|
||||||
|
|
||||||
|
if (presence == NULL) // si le tableau est vide (ne devrait pas arriver sauf si j'ai mal fais les choses)
|
||||||
|
{
|
||||||
|
|
||||||
|
return -1; // on sort direct de la fonction
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// on compte les clients
|
||||||
|
|
||||||
int nb = 0;
|
int nb = 0;
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
int deja_vu = 0;
|
|
||||||
for (int j = 0; j < i; j++) {
|
for (int i = 0; i < n; i++)
|
||||||
if (t[j].client == t[i].client) {
|
{
|
||||||
deja_vu = 1;
|
|
||||||
break;
|
int id_client = t[i].client;
|
||||||
}
|
|
||||||
}
|
if (!presence[id_client]) // si client pas vu
|
||||||
if (!deja_vu)
|
{
|
||||||
|
|
||||||
|
presence[id_client] = true; // marquer comme vu
|
||||||
nb++;
|
nb++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
free(presence); // j'ai du mal avc la memoire dcp honnetement c gemini qui m'a dit de faire ca (cleo)
|
||||||
|
|
||||||
return nb;
|
return nb;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* F3 : solde total de chaque client */
|
/* F3 : solde total de chaque client */
|
||||||
void total_par_client(const Transaction *t, int n, long *totaux)
|
void total_par_client(const Transaction *t, int n, long *totaux)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user