From: Jérôme Benoit Date: Sun, 12 Mar 2017 20:13:49 +0000 (+0100) Subject: TP 13 exo1: Add more library functions X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e40016767464ae5f361e1be20a9bbc6858878e5e;p=TD_C.git TP 13 exo1: Add more library functions Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index e69de29..0c56db6 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -0,0 +1,55 @@ +#include + +#include "sort.h" + +int create_tab(int tab[], unsigned tab_size) { + tab = malloc(sizeof(unsigned) * tab_size); + if (tab == NULL) { + return -1; + } else { + return 0; + } +} + +void free_tab(int tab[]) { + free(tab); +} + +/* we suppose both tab are already created */ +static void copy_tab(int src_tab[], int dest_tab[], unsigned min_tab_size, unsigned index_offset) { + for (unsigned i = 0; i < min_tab_size; i++) { + dest_tab[i + index_offset] = src_tab[i]; + } +} + +int concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int tab_dest[]) { + int rt = create_tab(tab_dest, tab_size1 + tab_size2); + + copy_tab(tab1, tab_dest, tab_size1, 0); + copy_tab(tab2, tab_dest, tab_size2, tab_size1); + return rt; +} + +int resize_tab(int tab[], unsigned tab_size) { + tab = realloc(tab, sizeof(int) * tab_size); + if (tab == NULL) { + return -1; + } else { + return 0; + } +} + +/* number of occurences of an element in an unsorted array */ +int 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++; + } + } + return el_count; +} + +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 3326460..3bc6573 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -1,6 +1,11 @@ #ifndef ARRAY_H #define ARRAY_H - +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 tab_dest[]); +int resize_tab(int tab[], unsigned tab_size); +int count_tab_element(int tab[], unsigned tab_size, int element); +void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria); #endif /* ARRAY_H */ diff --git a/TP_13/exo1/lib/macros.h b/TP_13/exo1/lib/macros.h index ab500df..4b39cf4 100644 --- a/TP_13/exo1/lib/macros.h +++ b/TP_13/exo1/lib/macros.h @@ -19,12 +19,14 @@ #ifndef MACROS_H #define MACROS_H +#include + /* definition to expand macro then apply to pragma message */ #define VALUE_TO_STRING(x) #x #define VALUE(x) VALUE_TO_STRING(x) #define VAR_NAME_VALUE(var) #var "=" VALUE(var) -#define ARRAY_SIZE(arr) ({typeof (arr) arr ## _is_a_pointer __attribute__((unused)) = {}; \ - sizeof(arr) / sizeof(arr[0]);}) +/* FIXME: ensure we manipulate real array */ +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) #endif /* MACROS_H */ diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index 8b196ff..0fdb0be 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -9,9 +9,17 @@ bool descending(int a, int b) { return a < b; } -static bool sort_first(int* array, int length, criteria_cb criteria) { +bool ascending_and_even(int a, int b) { + return (ascending(a, b) && (a % 2 == 0)); +} + +bool ascending_and_odd(int a, int b) { + return (ascending(a, b) && (a % 2 != 0)); +} + +static bool sort_first(int* array, unsigned length, criteria_cb criteria) { bool rt = false; - for (int i = 0; i < length-1; i++) { + for (unsigned i = 0; i < length-1; i++) { if (criteria(array[i], array[i+1])) { swap_int(&array[i], &array[i+1]); if (!rt) { rt = true; }; @@ -21,7 +29,7 @@ static bool sort_first(int* array, int length, criteria_cb criteria) { } /* this function is awaited in the array.c file */ -void sort_array(int* array, int length, criteria_cb criteria) { +void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) { bool rt; do { rt = sort_first(array, length, criteria); diff --git a/TP_13/exo1/lib/sort.h b/TP_13/exo1/lib/sort.h index 57b51de..8986302 100644 --- a/TP_13/exo1/lib/sort.h +++ b/TP_13/exo1/lib/sort.h @@ -8,7 +8,9 @@ typedef bool(*criteria_cb)(int a, int b); /* sort criteria */ bool ascending(int a, int b); bool descending(int a, int b); +bool ascending_and_even(int a, int b); +bool ascending_and_odd(int a, int b); -void sort_array(int* array, int length, criteria_cb criteria); +void sort_bubble_array(int* array, unsigned length, criteria_cb criteria); #endif /* SORT_H */ diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 06f5f8d..315ed33 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -1,7 +1,8 @@ +#include #include int main() { printf("Hello world\n"); - return 0; + exit(EXIT_SUCCESS); }