X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo1%2Flib%2Fio.c;h=69b13d768f2b68e4cd0a01e01f2e23ab30a8368a;hb=04b0afb5d5c81f1d98e98b9a6e532b1d3c868cc4;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hpb=eddc018ded5c49cd7301031d7c6c80022fb91f9d;p=TD_C.git diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index e69de29..69b13d7 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -0,0 +1,129 @@ +#include +#include +#include + +#include "io.h" +#include "array.h" + +int prompt_value(const char* msg, int* result) { + puts(msg); + int retVal = scanf("%d", result); + return (retVal == 1) ? 0 : 1; +} + +int* prompt_array(int array[], unsigned* size) { + int errno = prompt_value("Taille du tableau?", (int*)size); + array = create_tab(array, *size); + for (unsigned i = 0; i < *size; i++) { + errno += prompt_value("Valeur?", &array[i]); + } + /* error might have occured */ + handle_prompt_error(errno); + return array; +} + +void display_choice_menu() { + printf("\n=== Menu ===\n\n"); + printf("1) Saisir puis concatener un autre tableau.\n"); + printf("2) Trier le tableau.\n"); + printf("3) Afficher le tableau.\n"); + printf("4) Compter le nombre d'occurence d'un entier dans le tableau.\n"); + printf("5) Compter le nombre d'entiers pairs dans le tableau.\n"); + printf("6) Compter le nombre d'entiers impairs dans le tableau.\n"); + printf("7) Redimensionner le tableau.\n"); + printf("8) Quitter.\n"); +} + +int* do_concat(int array[], unsigned* size) { + int* tab_to_concat = NULL; + unsigned tab_to_concat_size = 0; + printf("\n=== Saisie d'un tableau ===\n\n"); + tab_to_concat = prompt_array(tab_to_concat, &tab_to_concat_size); + int* tab_concat = concat_tab(array, *size, tab_to_concat, tab_to_concat_size); + *size += tab_to_concat_size; + free_tab(array); + free_tab(tab_to_concat); + return tab_concat; +} + +void do_sort(int array[], unsigned size) { + int errno = 0; + int choice = 0; + bool done = false; + s_criteria_cb sort_criteria; + + printf("\n=== Menu de tri ===\n\n"); + printf("1) Croissant.\n"); + printf("2) Decroissant.\n"); + printf("3) Croissant pairs en premier.\n"); + printf("4) Croissant impairs en premier.\n"); + do { + errno = prompt_value("Choix?", &choice); + handle_prompt_error(errno); + done = true; + if (1 > choice || 4 < choice) { + printf("\nFaire un choix compris entre 1 et 4\n"); + done = false; + } + } while (!done); + switch (choice) { + case 1: + sort_criteria = ascending; + break; + case 2: + sort_criteria = descending; + break; + case 3: + sort_criteria = ascending_and_even; + break; + case 4: + sort_criteria = ascending_and_odd; + break; + default: + /* sort ascending by default, unused code path */ + sort_criteria = ascending; + break; + } + sort_tab(array, size, sort_criteria); +} + +void do_count(int array[], unsigned size) { + int errno = 0; + int search_value = 0; + + errno = prompt_value("\nValeur a chercher?", &search_value); + handle_prompt_error(errno); + printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); +} + +void do_resize(int array[], unsigned* old_size) { + int errno = 0; + unsigned new_size = 0; + + errno = prompt_value("\nNouvelle taille?", (int*)&new_size); + handle_prompt_error(errno); + /* FIXME: one should able the set the array new content if new_size > *old_size + * for now, new values are zeroed */ + array = resize_tab(array, *old_size, new_size); + *old_size = new_size; +} + +void handle_prompt_error(int errno) { + if (errno != 0) { + printf("\nMerci de saisir un nombre entier, exiting\n"); + /* it's somewhat violent but better than looping forever */ + exit(EXIT_FAILURE); + } +} + +void display_array(int array[], unsigned size) { + if (array != NULL) { + printf("\n--array begin--\n"); + for (unsigned i = 0; i < size; i++) { + printf("value in array at index[%d]=%d\n", i, array[i]); + } + printf("--array end--\n"); + } else { + printf("\n--array NULL--\n"); + } +}