From: Jérôme Benoit Date: Tue, 14 Mar 2017 22:11:23 +0000 (+0100) Subject: TP 13 exo1: implement odd and even elements counting with callbacks X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=889d586254feccc26f294af86ce7e698ea006dfb;p=TD_C.git TP 13 exo1: implement odd and even elements counting with 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 ec612c1..7743ca5 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -47,8 +47,9 @@ int* resize_tab(int tab[], unsigned new_tab_size) { } /* number of occurences of an element in an unsorted array */ -int count_tab_element(int tab[], unsigned tab_size, int element) { +unsigned count_tab_element(int tab[], unsigned tab_size, int element) { unsigned el_count = 0; + for (unsigned i = 0; i < tab_size; i++) { if (tab[i] == element) { el_count++; @@ -57,6 +58,26 @@ int 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 cr_count = 0; + + for (unsigned i = 0; i < tab_size; i++) { + if (c_criteria(tab[i])) { + cr_count++; + } + } + return cr_count; +} + +bool is_even(int a) { + return (a % 2 == 0); +} + +bool is_odd(int a) { + return (a % 2 != 0); + +} + void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) { sort_bubble_array(tab, tab_size, criteria); } diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index efdb488..ebf92c5 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -1,13 +1,21 @@ #ifndef ARRAY_H #define ARRAY_H +#include + #include "sort.h" +typedef bool(*count_criteria_cb)(int a); + +bool is_even(int a); +bool is_odd(int a); + int* create_tab(int tab[], unsigned tab_size); void free_tab(int tab[]); int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2); int* resize_tab(int tab[], unsigned tab_size); -int count_tab_element(int tab[], unsigned tab_size, int element); +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); #endif /* ARRAY_H */ diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 989e746..7639410 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -28,7 +28,9 @@ void display_choice_menu() { 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) Quitter.\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"); } int* do_concat(int array[], unsigned* size) { @@ -77,6 +79,7 @@ void do_sort(int array[], unsigned size) { criteria = ascending_and_odd; break; default: + /* sort ascending by default, unused code path */ criteria = ascending; break; } diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index e747218..2c81a63 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -18,8 +18,8 @@ int main() { display_choice_menu(); errno = prompt_value("Choix?", &choice); handle_prompt_error(errno); - if (1 > choice || 5 < choice) { - printf("\nFaire un choix compris entre 1 et 5\n"); + if (1 > choice || 7 < choice) { + printf("\nFaire un choix compris entre 1 et 7\n"); continue; } switch (choice) { @@ -35,11 +35,17 @@ int main() { case 4: do_count(tab, tab_size); break; + case 5: + printf("\nLe nombre d'entiers pairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_even)); + break; + case 6: + printf("\nLe nombre d'entiers impairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_odd)); + break; default: /* do nothing, unused code path */ break; } - } while (choice != 5); + } while (choice != 7); free_tab(tab); exit(EXIT_SUCCESS);