X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo1%2Flib%2Fio.c;h=b6cc52c21780b5e8b614431566786e294a256543;hb=b2129d81b38a22c9102a8fb3b66d317fc77c5c07;hp=884d4a0b730f1d6e266628f326866a4e829112a2;hpb=59941dc1cf415077fbfbde8b313e52e8f3fc6fe5;p=TD_C.git diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 884d4a0..b6cc52c 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -1,9 +1,9 @@ +#include #include #include #include "io.h" #include "array.h" -#include "utils.h" int prompt_value(const char* msg, int* result) { puts(msg); @@ -15,10 +15,10 @@ 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]); + errno += prompt_value("Valeur?", &array[i]); + /* error might have occured */ + handle_prompt_error(errno, array); } - /* error might have occured */ - handle_prompt_error(errno); return array; } @@ -50,7 +50,7 @@ void do_sort(int array[], unsigned size) { int errno = 0; int choice = 0; bool done = false; - criteria_cb criteria; + s_criteria_cb sort_criteria; printf("\n=== Menu de tri ===\n\n"); printf("1) Croissant.\n"); @@ -59,7 +59,7 @@ void do_sort(int array[], unsigned size) { printf("4) Croissant impairs en premier.\n"); do { errno = prompt_value("Choix?", &choice); - handle_prompt_error(errno); + handle_prompt_error(errno, array); done = true; if (1 > choice || 4 < choice) { printf("\nFaire un choix compris entre 1 et 4\n"); @@ -68,23 +68,23 @@ void do_sort(int array[], unsigned size) { } while (!done); switch (choice) { case 1: - criteria = ascending; + sort_criteria = ascending; break; case 2: - criteria = descending; + sort_criteria = descending; break; case 3: - criteria = ascending_and_even; + sort_criteria = ascending_and_even; break; case 4: - criteria = ascending_and_odd; + sort_criteria = ascending_and_odd; break; default: /* sort ascending by default, unused code path */ - criteria = ascending; + sort_criteria = ascending; break; } - sort_tab(array, size, criteria); + sort_tab(array, size, sort_criteria); } void do_count(int array[], unsigned size) { @@ -92,7 +92,7 @@ void do_count(int array[], unsigned size) { int search_value = 0; errno = prompt_value("\nValeur a chercher?", &search_value); - handle_prompt_error(errno); + handle_prompt_error(errno, array); printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); } @@ -101,11 +101,27 @@ void do_resize(int array[], unsigned* old_size) { unsigned new_size = 0; errno = prompt_value("\nNouvelle taille?", (int*)&new_size); - handle_prompt_error(errno); + handle_prompt_error(errno, array); array = resize_tab(array, *old_size, new_size); + if (new_size > *old_size) { + printf("Saisie des valeurs supplementaires du tableau\n"); + for (unsigned i = *old_size; i < new_size; i++) { + errno += prompt_value("Valeur?", &array[i]); + handle_prompt_error(errno, array); + } + } *old_size = new_size; } +void handle_prompt_error(int errno, int* tab) { + if (errno != 0) { + printf("\nMerci de saisir un nombre entier, exiting\n"); + /* it's somewhat violent but better than looping forever */ + free_tab(tab); + exit(EXIT_FAILURE); + } +} + void display_array(int array[], unsigned size) { if (array != NULL) { printf("\n--array begin--\n");