From: Jérôme Benoit Date: Wed, 15 Mar 2017 18:42:03 +0000 (+0100) Subject: TP 13 exo1: Give more explicits name to callbacks X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=210f7f057cbb8acbc783ffd1ac7333d6cf613ce4;p=TD_C.git TP 13 exo1: Give more explicits name to callbacks Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index 57c15de..db8ab7e 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -63,7 +63,7 @@ unsigned count_tab_element(int tab[], unsigned tab_size, int element) { return el_count; } -unsigned count_tab_criteria(int tab[], unsigned tab_size, count_criteria_cb c_criteria) { +unsigned count_tab_criteria(int tab[], unsigned tab_size, c_criteria_cb c_criteria) { unsigned cr_count = 0; for (unsigned i = 0; i < tab_size; i++) { @@ -83,6 +83,6 @@ bool is_odd(int a) { } -void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) { - sort_bubble_array(tab, tab_size, criteria); +void sort_tab(int tab[], unsigned tab_size, s_criteria_cb sort_criteria) { + sort_bubble_array(tab, tab_size, sort_criteria); } diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index 216fa0a..4e58754 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -5,7 +5,7 @@ #include "sort.h" -typedef bool(*count_criteria_cb)(int a); +typedef bool(*c_criteria_cb)(int a); bool is_even(int a); bool is_odd(int a); @@ -15,7 +15,7 @@ void free_tab(int tab[]); int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2); int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size); unsigned count_tab_element(int tab[], unsigned tab_size, int element); -unsigned count_tab_criteria(int tab[], unsigned tab_size, count_criteria_cb c_criteria); -void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria); +unsigned count_tab_criteria(int tab[], unsigned tab_size, c_criteria_cb c_criteria); +void sort_tab(int tab[], unsigned tab_size, s_criteria_cb sort_criteria); #endif /* ARRAY_H */ diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 884d4a0..e5a3000 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -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"); @@ -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) { @@ -102,6 +102,8 @@ void do_resize(int array[], unsigned* old_size) { 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; } diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index 01a742f..78625b6 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -19,10 +19,10 @@ bool ascending_and_odd(int a, int b) { || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); } -static bool sort_first(int* array, unsigned length, criteria_cb criteria) { +static bool sort_first(int* array, unsigned length, s_criteria_cb sort_criteria) { bool rt = false; for (unsigned i = 0; i < length-1; i++) { - if (criteria(array[i], array[i+1])) { + if (sort_criteria(array[i], array[i+1])) { swap_int(&array[i], &array[i+1]); rt = true; } @@ -31,9 +31,9 @@ static bool sort_first(int* array, unsigned length, criteria_cb criteria) { } /* the feature of this function is awaited in the array.c file */ -void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) { +void sort_bubble_array(int* array, unsigned length, s_criteria_cb sort_criteria) { bool rt; do { - rt = sort_first(array, length, criteria); + rt = sort_first(array, length, sort_criteria); } while (rt); } diff --git a/TP_13/exo1/lib/sort.h b/TP_13/exo1/lib/sort.h index 8986302..3c499f7 100644 --- a/TP_13/exo1/lib/sort.h +++ b/TP_13/exo1/lib/sort.h @@ -3,7 +3,7 @@ #include -typedef bool(*criteria_cb)(int a, int b); +typedef bool(*s_criteria_cb)(int a, int b); /* sort criteria */ bool ascending(int a, int b); @@ -11,6 +11,6 @@ bool descending(int a, int b); bool ascending_and_even(int a, int b); bool ascending_and_odd(int a, int b); -void sort_bubble_array(int* array, unsigned length, criteria_cb criteria); +void sort_bubble_array(int* array, unsigned length, s_criteria_cb sort_criteria); #endif /* SORT_H */