X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo1%2Flib%2Fio.c;h=b0a66a0fb1b14f4cc821a475c0a8558e9c90a44d;hb=ac5c12a4131a461182da110ec6c50be4cbd4270e;hp=7639410116edf56925481f8501b24fc0ff29c963;hpb=889d586254feccc26f294af86ce7e698ea006dfb;p=TD_C.git diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 7639410..b0a66a0 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); } - /* error might have occured */ - handle_prompt_error(errno); return array; } @@ -30,7 +30,8 @@ void display_choice_menu() { 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) Quitter.\n"); + printf("7) Redimensionner le tableau.\n"); + printf("8) Quitter.\n"); } int* do_concat(int array[], unsigned* size) { @@ -49,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"); @@ -67,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) { @@ -95,6 +96,31 @@ void do_count(int array[], unsigned size) { 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); + 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); + } + } + *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");