#include "compare.h" #include "genprint.h" #define xstr(s) str(s) // permet l'expansion d'une macro #define str(s) #s // Ex: scanf("%"xstr(DMAX)"s",buffer); // Tableau des tailles des types (ne pas modifier) int psize[] = { 2, 2, 1, 5, MAXSTR, 9, 9, }; // Tableau des fonctions de comparaison (ne pas modifier) fcmp cmp[] = { fcmp_int, fcmp_reverse_int, fcmp_char, fcmp_double, fcmp_string, fcmp_string_hiera, fcmp_pointx, fcmp_pointy, }; // Tableau des types pour l'aide (ne pas modifier) // clang-format off char *type[] = { "int", "Integers in [0,100[ (sort: in usual order).", "int2", "Integers in [0,100[ (sort: in reverse order).", "char", "Capital letters in ['A','Z'].", "double", "Real numbers in ]-1.00,+1.00[.", "string", "Strings of length ≤ " xstr(MAXSTR) " (sort: lexicographic).", "string2", "Strings of length ≤ " xstr(MAXSTR) " (sort by length, lexico if same length).", "pointx", "Real points of [0,9.9]×[0,9.9] with 'x'-like fcmp().", "pointy", "Real points of [0,9.9]×[0,9.9] with 'y'-like fcmp().", }; // clang-format on // Tableau des tailles des types (ne pas modifier) int size[] = { sizeof(int), sizeof(int), sizeof(char), sizeof(double), sizeof(string), sizeof(string), sizeof(point), sizeof(point), }; int main(int argc, char *argv[]) { type_t t; // nb_values = nombre d'éléments à générer. int nb_values = (argc >= 2) ? atoi(argv[1]) : -1; const char *s = (argc >= 3) ? argv[2] : type[INT]; for (t = 0; t < NUMTYPES; t++) if (!strcmp(s, type[2 * t])) break; // type trouvé ! if (nb_values < 0 || t == NUMTYPES) { // nombre d'éléments ou type incorrect fprintf(stderr, "\n Usage: %s nb_values [t]", argv[0]); fprintf(stderr, "\n Ex.: %s 42 int\n\n", argv[0]); fprintf(stderr, " nb_values = number of random elements\n"); fprintf(stderr, " t = type of elements & fcmp():\n\n"); for (t = 0; t < NUMTYPES; t++) { fprintf(stderr, "%8s ", type[2 * t]); fprintf(stderr, " %s\n", type[2 * t + 1]); } fprintf(stderr, "\n"); exit(EXIT_FAILURE); } unsigned seed = time(NULL) % 1000; srandom(seed); printf("\nSeed: %u\n\n", seed); // pour rejouer la même chose au cas où // Tableau initial void *T = malloc(nb_values * size[t]); printf("Array of %i elements of type '%s'\n", nb_values, type[2 * t]); printf("%s\n", type[2 * t + 1]); // Affichage et création de T[] printf("\nInput array:\n\n"); //////////////////////////////////////////////////////////////// // COMPLÉTER À PARTIR D'ICI. // 1. Initialiser chaque élément du tableau en utilisant init(). // 2. Afficher le tableau en utilisant print_array(). // 3. Trier ici le tableau. // qsort(...); // ou autre tri, comme mergesort ou heapsort. printf("Array sorted:\n\n"); // 4. Afficher à nouveau le tableau (trié) en utilisant print_array(). print_array(T, t, nb_values); printf("\n\n"); //////////////////////////////////////////////////////////////// // À partir d'ici, tests de lecture des tableaux depuis des fichiers. printf("Array of integers from array_int.txt\n\n"); // 5. Ouvrir le fichier array_int.txt en lecture. // 6. Lire le tableau d'entiers depuis le flux obtenu avec read_array(). // Par exemple : T = read_array(...); // 7. Afficher T. // 8. Trier T. // 9. Afficher T. // 10. Libérer la mémoire allouée pour T // 11. Fermer le flux f. printf("\n\n"); // Faire de même pour les autres types. printf("Array of characters from array_char.txt\n\n"); printf("\n\n"); printf("Array of doubles from array_double.txt\n\n"); printf("\n\n"); // Attention aux types chaînes de caractères, plus délicats à gérer. // En effet, il faut allouer de la mémoire pour chaque chaîne. printf("Array of strings from array_string.txt\n\n"); printf("\n\n"); printf("Array of strings from array_string2.txt\n\n"); printf("\n\n"); printf("Array of points from array_pointx.txt\n\n"); printf("\n\n"); printf("Array of points from array_pointy.txt\n\n"); printf("\n\n"); // Vous pouvez ajouter du code pour trier successivement les tableaux de // points suivant les deux critères x et y. Testez plusieurs fonctions de // tri. Que remarquez-vous ? return EXIT_SUCCESS; }