From: Jérôme Benoit Date: Mon, 13 Mar 2017 21:08:42 +0000 (+0100) Subject: TP 13 exo1: Implement more asked features and test them X-Git-Url: https://git.piment-noir.org/?p=TD_C.git;a=commitdiff_plain;h=cfdd46d2e85b05f77a03ae31f721e2fd4030996f TP 13 exo1: Implement more asked features and test them Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 2df5134..1217750 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -1,6 +1,7 @@ #include #include "io.h" +#include "array.h" int prompt_value(const char* msg, int* result) { puts(msg); @@ -8,10 +9,27 @@ int prompt_value(const char* msg, int* 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]); + } + if (errno == 0) { + return array; + } else { + return NULL; + } +} + void display_array(int* array, int size) { - printf("--array begin--\n"); - for (int i = 0; i < size; i++) { - printf("value in array at index[%d]=%d\n", i, array[i]); + if (array != NULL) { + printf("--array begin--\n"); + for (int i = 0; i < size; i++) { + printf("value in array at index[%d]=%d\n", i, array[i]); + } + printf("--array end--\n"); + } else { + printf("--array NULL--\n"); } - printf("--array end--\n"); } diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 8b2fb6e..21a33e0 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -2,6 +2,7 @@ #define IO_H int prompt_value(const char* msg, int* result); +int* prompt_array(int array[], unsigned* size); void display_array(int* array, int size); diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index 70a766b..f8917ea 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -10,11 +10,13 @@ bool descending(int a, int b) { } bool ascending_and_even(int a, int b) { - return (ascending(a, b) && (a % 2 == 0)); + return (((a % 2 != 0) && (b % 2 == 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ + || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); } bool ascending_and_odd(int a, int b) { - return (ascending(a, b) && (a % 2 != 0)); + return (((a % 2 == 0) && (b % 2 != 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ + || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); } static bool sort_first(int* array, unsigned length, criteria_cb criteria) { diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 41facd6..ea4abc3 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -7,17 +7,15 @@ int main() { int* tab = NULL; - unsigned tab_size = 11; - tab = create_tab(tab, tab_size); + unsigned tab_size = 0; + + tab = prompt_array(tab, &tab_size); printf("%d\n", tab_size); display_array(tab, tab_size); - tab_size = 20; - tab = resize_tab(tab, tab_size); - - printf("%d\n", tab_size); + sort_tab(tab, tab_size, ascending_and_odd); display_array(tab, tab_size);