1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct fiche
{
char nom[11];
char prenom[11];
int age;
};
/* affichage du tableau */
static void aff (struct fiche const *a, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
/* pointeur intermediaire pour alleger l'ecriture */
struct fiche const *p = a + i;
printf ("%-10s %-10s %d ans\n", p->nom, p->prenom, p->age);
}
printf ("\n");
}
/* fonction utilisateur de comparaison fournie a qsort() */
static int compare_prenom (void const *a, void const *b)
{
/* definir des pointeurs type's et initialise's
avec les parametres */
struct fiche const *pa = a;
struct fiche const *pb = b;
/* evaluer et retourner l'etat de l'evaluation (tri croissant) */
return strcmp (pa->prenom, pb->prenom);
}
/* fonction utilisateur de comparaison fournie a qsort() */
static int compare_age (void const *a, void const *b)
{
struct fiche const *pa = a;
struct fiche const *pb = b;
return pa->age - pb->age;
}
/* fonction utilisateur de comparaison fournie a qsort() */
static int compare_age_dec (void const *a, void const *b)
{
struct fiche const *pa = a;
struct fiche const *pb = b;
return pb->age - pa->age;
}
int main (void)
{
/* tableau a trier (tableau de pointeurs sur char const) */
struct fiche tab[] = {
{"Simpson", "Homer", 36},
{"Bouvier", "Marge", 34},
{"Simpson", "Bart", 10},
{"Simpson", "Lisa", 8},
{"Simpson", "Maggie", 2},
};
/* affichage du tableau dans l'etat */
aff (tab, sizeof tab / sizeof *tab);
qsort (tab, sizeof tab / sizeof *tab, sizeof *tab, compare_prenom);
/* affichage du tableau apres le tri */
aff (tab, sizeof tab / sizeof *tab);
qsort (tab, sizeof tab / sizeof *tab, sizeof *tab, compare_age);
/* affichage du tableau apres le tri */
aff (tab, sizeof tab / sizeof *tab);
qsort (tab, sizeof tab / sizeof *tab, sizeof *tab, compare_age_dec);
/* affichage du tableau apres le tri */
aff (tab, sizeof tab / sizeof *tab);
return 0;
} |
Partager