From 34f864c6720b5e480db66430a218e2fd54743ce7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 14:45:14 +0100 Subject: [PATCH 01/16] TP 13 exo1: return the dynamic array created or modified when necessary MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 33 +++++++++++++++++++-------------- TP_13/exo1/lib/array.h | 6 +++--- TP_13/exo1/lib/io.c | 2 ++ TP_13/exo1/src/main.c | 15 +++++++++++++-- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index c9a8246..f7e324f 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -2,12 +2,16 @@ #include "array.h" -int create_tab(int tab[], unsigned tab_size) { - tab = malloc(sizeof(unsigned) * tab_size); +int* create_tab(int tab[], unsigned tab_size) { + tab = malloc(sizeof(int) * tab_size); if (tab == NULL) { - return -1; + return NULL; } else { - return 0; + /* initialize to zero the integer array */ + for (unsigned i = 0; i < tab_size; i++) { + tab[i] = 0; + } + return tab; } } @@ -15,28 +19,29 @@ 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) { - /* FIXME: I think it's worth doing some sanity check on the array size */ - for (unsigned i = 0; i < min_tab_size; i++) { +/* we suppose both tabs are already created */ +static void copy_tab(int src_tab[], int dest_tab[], unsigned src_tab_size, unsigned index_offset) { + /* FIXME: I think it's worth doing some sanity checks on the array size: + * dest_tab_size >= src_tab_size */ + for (unsigned i = 0; i < src_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); +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); +int* resize_tab(int tab[], unsigned new_tab_size) { + tab = realloc(tab, sizeof(int) * new_tab_size); if (tab == NULL) { - return -1; + return NULL; } else { - return 0; + return tab; } } diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index 9c06bc8..18596b2 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -3,10 +3,10 @@ #include "sort.h" -int create_tab(int tab[], unsigned tab_size); +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* 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); diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 4af0b66..2df5134 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -9,7 +9,9 @@ int prompt_value(const char* msg, int* result) { } 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]); } + printf("--array end--\n"); } diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 1b93898..658930e 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -3,11 +3,22 @@ #include "array.h" #include "utils.h" +#include "io.h" int main() { int* tab = NULL; - create_tab(tab, 11); - const unsigned tab_size = ARRAY_SIZE(tab); + const unsigned tab_size = 11; + tab = create_tab(tab, tab_size); + printf("%d\n", tab_size); + + display_array(tab, tab_size); + + const unsigned tab_new_size = 20; + tab = resize_tab(tab, tab_new_size); + + display_array(tab, tab_new_size); + + free_tab(tab); exit(EXIT_SUCCESS); } -- 2.34.1 From 34dd19e9551e120279afde8d75a1cb51181d7794 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 15:00:07 +0100 Subject: [PATCH 02/16] TP 11 exo1: simplify the logic in the array creation and resizing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 12 +++--------- TP_13/exo1/src/main.c | 2 ++ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index f7e324f..d2e6182 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -4,15 +4,13 @@ int* create_tab(int tab[], unsigned tab_size) { tab = malloc(sizeof(int) * tab_size); - if (tab == NULL) { - return NULL; - } else { + if (tab != NULL) { /* initialize to zero the integer array */ for (unsigned i = 0; i < tab_size; i++) { tab[i] = 0; } - return tab; } + return tab; } void free_tab(int tab[]) { @@ -38,11 +36,7 @@ int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int* resize_tab(int tab[], unsigned new_tab_size) { tab = realloc(tab, sizeof(int) * new_tab_size); - if (tab == NULL) { - return NULL; - } else { - return tab; - } + return tab; } /* number of occurences of an element in an unsorted array */ diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 658930e..9f23749 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -17,6 +17,8 @@ int main() { const unsigned tab_new_size = 20; tab = resize_tab(tab, tab_new_size); + printf("%d\n", tab_new_size); + display_array(tab, tab_new_size); free_tab(tab); -- 2.34.1 From c944d83b743f75264d017b47bc388962c51f2d3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 15:02:15 +0100 Subject: [PATCH 03/16] TP 11 exo1: affecting a value cost less than branching MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index 0fdb0be..70a766b 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -22,7 +22,7 @@ static bool sort_first(int* array, unsigned length, criteria_cb criteria) { 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; }; + rt = true; } } return rt; -- 2.34.1 From ba8488e5c6979a346c7a364affd31512142bac3d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 15:06:35 +0100 Subject: [PATCH 04/16] TP 11 exo1: deconstify where appropriate MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/src/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 9f23749..41facd6 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -7,19 +7,19 @@ int main() { int* tab = NULL; - const unsigned tab_size = 11; + unsigned tab_size = 11; tab = create_tab(tab, tab_size); printf("%d\n", tab_size); display_array(tab, tab_size); - const unsigned tab_new_size = 20; - tab = resize_tab(tab, tab_new_size); + tab_size = 20; + tab = resize_tab(tab, tab_size); - printf("%d\n", tab_new_size); + printf("%d\n", tab_size); - display_array(tab, tab_new_size); + display_array(tab, tab_size); free_tab(tab); exit(EXIT_SUCCESS); -- 2.34.1 From cfdd46d2e85b05f77a03ae31f721e2fd4030996f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 22:08:42 +0100 Subject: [PATCH 05/16] TP 13 exo1: Implement more asked features and test them MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/io.c | 26 ++++++++++++++++++++++---- TP_13/exo1/lib/io.h | 1 + TP_13/exo1/lib/sort.c | 6 ++++-- TP_13/exo1/src/main.c | 10 ++++------ 4 files changed, 31 insertions(+), 12 deletions(-) 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); -- 2.34.1 From 475ee86d70921638c700bc0934441c7fe2c905d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 14 Mar 2017 22:21:44 +0100 Subject: [PATCH 06/16] TP 13 exo1: Implement all the required features. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/Makefile | 2 +- TP_13/exo1/lib/array.c | 13 +++++-- TP_13/exo1/lib/array.h | 2 +- TP_13/exo1/lib/io.c | 86 ++++++++++++++++++++++++++++++++++++++---- TP_13/exo1/lib/io.h | 8 +++- TP_13/exo1/lib/sort.c | 2 +- TP_13/exo1/lib/utils.c | 10 +++++ TP_13/exo1/lib/utils.h | 2 + TP_13/exo1/src/main.c | 36 ++++++++++++++---- 9 files changed, 139 insertions(+), 22 deletions(-) diff --git a/TP_13/exo1/Makefile b/TP_13/exo1/Makefile index 791c7d4..fd95878 100644 --- a/TP_13/exo1/Makefile +++ b/TP_13/exo1/Makefile @@ -50,7 +50,7 @@ OPTI_FLAG = -O3 endif # Putting header files in the source directory is not the purpose of this INCLUDES variable -INCLUDES := $(INCLUDES) -I$(SRC_PATH) -I$(LIBRARY_PATH) +INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) CFLAGS := $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) LIBCFLAGS := -fPIC $(CFLAGS) LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index d2e6182..ec612c1 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -1,4 +1,5 @@ #include +#include #include "array.h" @@ -21,17 +22,23 @@ void free_tab(int tab[]) { static void copy_tab(int src_tab[], int dest_tab[], unsigned src_tab_size, unsigned index_offset) { /* FIXME: I think it's worth doing some sanity checks on the array size: * dest_tab_size >= src_tab_size */ + if (src_tab == NULL || dest_tab == NULL) { + printf("please ensure you have created both arrays beforehand\n"); + return; + } for (unsigned i = 0; i < src_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); +/* one must free the two source tabs in case they will be unused after to concatenation */ +int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2) { + int* tab_dest = NULL; + tab_dest = 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; + return tab_dest; } int* resize_tab(int tab[], unsigned new_tab_size) { diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index 18596b2..efdb488 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -5,7 +5,7 @@ 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* 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); void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria); diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 1217750..989e746 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -1,7 +1,9 @@ #include +#include #include "io.h" #include "array.h" +#include "utils.h" int prompt_value(const char* msg, int* result) { puts(msg); @@ -15,21 +17,89 @@ int* prompt_array(int array[], unsigned* size) { for (unsigned i = 0; i < *size; i++) { errno += prompt_value("Valeur?", &array[i]); } - if (errno == 0) { - return array; - } else { - return NULL; + /* error might have occured */ + handle_prompt_error(errno); + return array; +} + +void display_choice_menu() { + printf("\n=== Menu ===\n\n"); + printf("1) Saisir puis concatener un autre tableau.\n"); + 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"); +} + +int* do_concat(int array[], unsigned* size) { + int* tab_to_concat = NULL; + unsigned tab_to_concat_size = 0; + printf("\n=== Saisie d'un tableau ===\n\n"); + tab_to_concat = prompt_array(tab_to_concat, &tab_to_concat_size); + int* tab_concat = concat_tab(array, *size, tab_to_concat, tab_to_concat_size); + *size += tab_to_concat_size; + free_tab(array); + free_tab(tab_to_concat); + return tab_concat; +} + +void do_sort(int array[], unsigned size) { + int errno = 0; + int choice = 0; + bool done = false; + criteria_cb criteria; + + printf("\n=== Menu de tri ===\n\n"); + printf("1) Croissant.\n"); + printf("2) Decroissant.\n"); + printf("3) Croissant pairs en premier.\n"); + printf("4) Croissant impairs en premier.\n"); + do { + errno = prompt_value("Choix?", &choice); + handle_prompt_error(errno); + done = true; + if (1 > choice || 4 < choice) { + printf("\nFaire un choix compris entre 1 et 4\n"); + done = false; + } + } while (!done); + switch (choice) { + case 1: + criteria = ascending; + break; + case 2: + criteria = descending; + break; + case 3: + criteria = ascending_and_even; + break; + case 4: + criteria = ascending_and_odd; + break; + default: + criteria = ascending; + break; } + sort_tab(array, size, criteria); +} + +void do_count(int array[], unsigned size) { + int errno = 0; + int search_value = 0; + + errno = prompt_value("\nValeur a chercher?", &search_value); + handle_prompt_error(errno); + printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); } -void display_array(int* array, int size) { +void display_array(int array[], unsigned size) { if (array != NULL) { - printf("--array begin--\n"); - for (int i = 0; i < size; i++) { + printf("\n--array begin--\n"); + for (unsigned 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("\n--array NULL--\n"); } } diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 21a33e0..13f14e0 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -4,6 +4,12 @@ int prompt_value(const char* msg, int* result); int* prompt_array(int array[], unsigned* size); -void display_array(int* array, int size); +void display_choice_menu(); + +int* do_concat(int array[], unsigned* size); +void do_sort(int array[], unsigned size); +void do_count(int array[], unsigned size); + +void display_array(int array[], unsigned size); #endif /* IO_H */ diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index f8917ea..01a742f 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -30,7 +30,7 @@ static bool sort_first(int* array, unsigned length, criteria_cb criteria) { return rt; } -/* this function is awaited in the array.c file */ +/* the feature of this function is awaited in the array.c file */ void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) { bool rt; do { diff --git a/TP_13/exo1/lib/utils.c b/TP_13/exo1/lib/utils.c index 04a20e4..fbd7e01 100644 --- a/TP_13/exo1/lib/utils.c +++ b/TP_13/exo1/lib/utils.c @@ -1,3 +1,5 @@ +#include + #include "utils.h" void swap_int(int* v1, int* v2) { @@ -11,3 +13,11 @@ void swap_ptr(void* v1, void* v2) { v1 = v2; v2 = tmp; } + +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); + } +} diff --git a/TP_13/exo1/lib/utils.h b/TP_13/exo1/lib/utils.h index 03f1226..fe3fa08 100644 --- a/TP_13/exo1/lib/utils.h +++ b/TP_13/exo1/lib/utils.h @@ -6,4 +6,6 @@ void swap_int(int* v1, int* v2); void swap_ptr(void* v1, void* v2); +void handle_prompt_error(int errno); + #endif /* UTILS_H */ diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index ea4abc3..e747218 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -8,16 +8,38 @@ int main() { int* tab = NULL; unsigned tab_size = 0; + int errno = 0; + int choice = 0; + printf("=== Saisie initiale ===\n\n"); tab = prompt_array(tab, &tab_size); - printf("%d\n", tab_size); - - display_array(tab, tab_size); - - sort_tab(tab, tab_size, ascending_and_odd); - - display_array(tab, tab_size); + do { + 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"); + continue; + } + switch (choice) { + case 1: + tab = do_concat(tab, &tab_size); + break; + case 2: + do_sort(tab, tab_size); + break; + case 3: + display_array(tab, tab_size); + break; + case 4: + do_count(tab, tab_size); + break; + default: + /* do nothing, unused code path */ + break; + } + } while (choice != 5); free_tab(tab); exit(EXIT_SUCCESS); -- 2.34.1 From 889d586254feccc26f294af86ce7e698ea006dfb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 14 Mar 2017 23:11:23 +0100 Subject: [PATCH 07/16] TP 13 exo1: implement odd and even elements counting with callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 23 ++++++++++++++++++++++- TP_13/exo1/lib/array.h | 10 +++++++++- TP_13/exo1/lib/io.c | 5 ++++- TP_13/exo1/src/main.c | 12 +++++++++--- 4 files changed, 44 insertions(+), 6 deletions(-) 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); -- 2.34.1 From 59941dc1cf415077fbfbde8b313e52e8f3fc6fe5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 14 Mar 2017 23:42:27 +0100 Subject: [PATCH 08/16] TP 13 exo1: Implement array resizing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 7 ++++++- TP_13/exo1/lib/array.h | 2 +- TP_13/exo1/lib/io.c | 13 ++++++++++++- TP_13/exo1/lib/io.h | 1 + TP_13/exo1/src/main.c | 9 ++++++--- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index 7743ca5..57c15de 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -41,8 +41,13 @@ int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2) return tab_dest; } -int* resize_tab(int tab[], unsigned new_tab_size) { +int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size) { tab = realloc(tab, sizeof(int) * new_tab_size); + if (old_tab_size < new_tab_size) { + for (unsigned i = old_tab_size; i < new_tab_size; i++) { + tab[i] = 0; + } + } return tab; } diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index ebf92c5..216fa0a 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -13,7 +13,7 @@ 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* 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); diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 7639410..884d4a0 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -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) { @@ -95,6 +96,16 @@ 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); + *old_size = new_size; +} + void display_array(int array[], unsigned size) { if (array != NULL) { printf("\n--array begin--\n"); diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 13f14e0..24de1b1 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -9,6 +9,7 @@ void display_choice_menu(); int* do_concat(int array[], unsigned* size); void do_sort(int array[], unsigned size); void do_count(int array[], unsigned size); +void do_resize(int array[], unsigned* size); void display_array(int array[], unsigned size); diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 2c81a63..ef4d545 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 || 7 < choice) { - printf("\nFaire un choix compris entre 1 et 7\n"); + if (1 > choice || 8 < choice) { + printf("\nFaire un choix compris entre 1 et 8\n"); continue; } switch (choice) { @@ -41,11 +41,14 @@ int main() { case 6: printf("\nLe nombre d'entiers impairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_odd)); break; + case 7: + do_resize(tab, &tab_size); + break; default: /* do nothing, unused code path */ break; } - } while (choice != 7); + } while (choice != 8); free_tab(tab); exit(EXIT_SUCCESS); -- 2.34.1 From 25dc671f8b36ed62a1d2b689347902a122493782 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Mar 2017 11:42:43 +0100 Subject: [PATCH 09/16] TP 13 exo1: Add LTO and GOLD support to the Makefile build options MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_11/exo2/Makefile | 2 +- TP_13/exo1/Makefile | 21 +++++++++++++++++---- TP_13/exo1/lib/utils.c | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/TP_11/exo2/Makefile b/TP_11/exo2/Makefile index 73f0988..d1ca467 100644 --- a/TP_11/exo2/Makefile +++ b/TP_11/exo2/Makefile @@ -50,7 +50,7 @@ OPTI_FLAG = -O3 endif # Putting header files in the source directory is not the purpose of this INCLUDES variable -INCLUDES := $(INCLUDES) -I$(SRC_PATH) -I$(LIBRARY_PATH) +INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) CFLAGS := $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) LIBCFLAGS := -fPIC $(CFLAGS) LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) diff --git a/TP_13/exo1/Makefile b/TP_13/exo1/Makefile index fd95878..0e2661f 100644 --- a/TP_13/exo1/Makefile +++ b/TP_13/exo1/Makefile @@ -21,8 +21,8 @@ BINARY_NAME=exo1 SRC_PATH:=src LIBRARY_NAME=libexo1 LIBRARY_PATH:=lib -BUILD_TYPE=debug -#BUILD_TYPE=release +#BUILD_TYPE=debug +BUILD_TYPE=release # ==================================== # DO NOT CHANGE STUFF BEYOND THIS LINE @@ -42,18 +42,31 @@ BUILDDIR := .build/debug DEBUG_FLAG = -g STRIP_FLAG = OPTI_FLAG = -O0 +LTO_SUPPORT = no +GOLD_SUPPORT = no else BUILDDIR := .build/release DEBUG_FLAG = STRIP_FLAG = -s OPTI_FLAG = -O3 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +endif + +ifeq ($(LTO_SUPPORT),yes) +CFLAGS_LTO = -flto -ffat-lto-objects +LDFLAGS_LTO = -fuse-linker-plugin -flto +endif + +ifeq ($(GOLD_SUPPORT),yes) +LDFLAGS_GOLD = -fuse-ld=gold endif # Putting header files in the source directory is not the purpose of this INCLUDES variable INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) -CFLAGS := $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) +CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) LIBCFLAGS := -fPIC $(CFLAGS) -LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) +LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) LIBLDFLAGS := -shared $(LDFLAGS) STATICLIBLDFLAGS := -static $(LDFLAGS) LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) diff --git a/TP_13/exo1/lib/utils.c b/TP_13/exo1/lib/utils.c index fbd7e01..5139bae 100644 --- a/TP_13/exo1/lib/utils.c +++ b/TP_13/exo1/lib/utils.c @@ -17,7 +17,7 @@ void swap_ptr(void* v1, void* v2) { 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 */ + /* it's somewhat violent but better than looping forever */ exit(EXIT_FAILURE); } } -- 2.34.1 From 709c39546ac5df7fd3154073fd40227ea4939826 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Mar 2017 11:45:22 +0100 Subject: [PATCH 10/16] TP 13 exo1: Enable LTO and GOLD on all build types MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TP_13/exo1/Makefile b/TP_13/exo1/Makefile index 0e2661f..5d1b9ab 100644 --- a/TP_13/exo1/Makefile +++ b/TP_13/exo1/Makefile @@ -21,8 +21,8 @@ BINARY_NAME=exo1 SRC_PATH:=src LIBRARY_NAME=libexo1 LIBRARY_PATH:=lib -#BUILD_TYPE=debug -BUILD_TYPE=release +BUILD_TYPE=debug +#BUILD_TYPE=release # ==================================== # DO NOT CHANGE STUFF BEYOND THIS LINE @@ -42,8 +42,8 @@ BUILDDIR := .build/debug DEBUG_FLAG = -g STRIP_FLAG = OPTI_FLAG = -O0 -LTO_SUPPORT = no -GOLD_SUPPORT = no +LTO_SUPPORT = yes +GOLD_SUPPORT = yes else BUILDDIR := .build/release DEBUG_FLAG = -- 2.34.1 From 210f7f057cbb8acbc783ffd1ac7333d6cf613ce4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Mar 2017 19:42:03 +0100 Subject: [PATCH 11/16] TP 13 exo1: Give more explicits name to callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 6 +++--- TP_13/exo1/lib/array.h | 6 +++--- TP_13/exo1/lib/io.c | 16 +++++++++------- TP_13/exo1/lib/sort.c | 8 ++++---- TP_13/exo1/lib/sort.h | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) 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 */ -- 2.34.1 From 7515f89753dd7d16c4f2a9b11003f67d8148eb09 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Mar 2017 23:06:02 +0100 Subject: [PATCH 12/16] TP 13 exo2: Add the code structure and some basic displaying routines for the tic tac toe game MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/macros.h | 2 +- TP_13/exo2/Makefile | 145 +++++++++++++++++++++++++++++++++++++++ TP_13/exo2/lib/display.c | 56 +++++++++++++++ TP_13/exo2/lib/display.h | 26 +++++++ TP_13/exo2/lib/macros.h | 32 +++++++++ TP_13/exo2/lib/utils.c | 13 ++++ TP_13/exo2/lib/utils.h | 9 +++ TP_13/exo2/src/main.c | 38 ++++++++++ 8 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 TP_13/exo2/Makefile create mode 100644 TP_13/exo2/lib/display.c create mode 100644 TP_13/exo2/lib/display.h create mode 100644 TP_13/exo2/lib/macros.h create mode 100644 TP_13/exo2/lib/utils.c create mode 100644 TP_13/exo2/lib/utils.h create mode 100644 TP_13/exo2/src/main.c diff --git a/TP_13/exo1/lib/macros.h b/TP_13/exo1/lib/macros.h index 4b39cf4..3c0ad02 100644 --- a/TP_13/exo1/lib/macros.h +++ b/TP_13/exo1/lib/macros.h @@ -3,7 +3,7 @@ * * Filename: macros.h * - * Description: + * Description: Some useful macros * * Version: 1.0 * Created: 09/03/2017 15:28:46 diff --git a/TP_13/exo2/Makefile b/TP_13/exo2/Makefile new file mode 100644 index 0000000..8f48f4e --- /dev/null +++ b/TP_13/exo2/Makefile @@ -0,0 +1,145 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# $(SRC_PATH) directory. +# +# This Makefile expect all embedded library source files (.c) to be at the same level, in the +# $(LIBRARY_PATH) directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name linked against the library if necessary. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set LIBRARY_NAME to the name of the library file to build. +# The default path for the library code and object is lib. +# By default the linker will look for $(BINARY_NAME) library name. +# Set BUILD_TYPE to either debug or release. +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=exo2 +SRC_PATH:=src +LIBRARY_NAME=libexo2 +LIBRARY_PATH:=lib +LDLIBS=-lncurses -ltinfo +BUILD_TYPE=debug +#BUILD_TYPE=release + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static + +CC=gcc +LD=gcc +AR=ar + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +STRIP_FLAG = +OPTI_FLAG = -O0 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +else +BUILDDIR := .build/release +DEBUG_FLAG = +STRIP_FLAG = -s +OPTI_FLAG = -O3 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +endif + +ifeq ($(LTO_SUPPORT),yes) +CFLAGS_LTO = -flto -ffat-lto-objects +LDFLAGS_LTO = -fuse-linker-plugin -flto +endif + +ifeq ($(GOLD_SUPPORT),yes) +LDFLAGS_GOLD = -fuse-ld=gold +endif + +# Putting header files in the source directory is not the purpose of this INCLUDES variable +INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) +CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) +LIBCFLAGS := -fPIC $(CFLAGS) +LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) +LIBLDFLAGS := -shared $(LDFLAGS) +STATICLIBLDFLAGS := -static $(LDFLAGS) +LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard $(SRC_PATH)/*.c) +LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) +LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td +POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d + +$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS) + @echo "[AR StO] $@" + @$(AR) rcs $@ $^ + +$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS) + @echo "[LD ShO] $@" + @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@ + +#$(BINARY_NAME): $(OBJS) $(LIBOBJS) +# @echo "[LD ] $@" +# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +#FIXME: add an install target + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + -@rm $(BINARY_NAME).static + -@rm $(BINARY_NAME).dynamic + -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a + -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TP_13/exo2/lib/display.c b/TP_13/exo2/lib/display.c new file mode 100644 index 0000000..22deb96 --- /dev/null +++ b/TP_13/exo2/lib/display.c @@ -0,0 +1,56 @@ +/* + * ===================================================================================== + * + * Filename: display.c + * + * Description: Routines to handle the display + * + * Version: 1.0 + * Created: 15/03/2017 20:06:11 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#include + +/* in all print routine, y and x are the coordinates of the first character of the shape + * which can be a space ' ' */ + +void print_board(int y, int x) { + mvprintw(y, x, " | |"); + mvprintw(y+1, x, " | |"); + mvprintw(y+2, x, "----+----+----"); + mvprintw(y+3, x, " | |"); + mvprintw(y+4, x, " | |"); + mvprintw(y+5, x, "----+----+----"); + mvprintw(y+6, x, " | |"); + mvprintw(y+7, x, " | |"); +} + +/* there's only nine valid (y, x) 2-uplets for this two shapes + * that are : - base_y, base_x +1 + * - base_y, base_x + 6 + * - base_y, base_x + 11 + * - base_y + 3, base_x + 1 + * - base_y + 6, base_x + 1 + * - base_y + 3, base_x + 6 + * - base_y + 3, base_x + 11 + * - base_y + 6, base_x + 6 + * - base_y + 6, base_x + 11 + * The added y value can be {0, 3, 6} + * The added x value can be {1, 6, 11} */ + +void print_x(int y, int x) { + mvprintw(y, x, "\\/"); + mvprintw(y+1, x,"/\\"); +} + +void print_o(int y, int x) { + mvprintw(y, x, "/\\"); + mvprintw(y+1, x, "\\/"); +} diff --git a/TP_13/exo2/lib/display.h b/TP_13/exo2/lib/display.h new file mode 100644 index 0000000..f0b1136 --- /dev/null +++ b/TP_13/exo2/lib/display.h @@ -0,0 +1,26 @@ +/* + * ===================================================================================== + * + * Filename: display.h + * + * Description: Headers for display routines + * + * Version: 1.0 + * Created: 15/03/2017 20:07:12 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef DISPLAY_H +#define DISPLAY_H + +void print_board(int y, int x); +void print_x(int y, int x); +void print_o(int y, int x); + +#endif /* DISPLAY_H */ diff --git a/TP_13/exo2/lib/macros.h b/TP_13/exo2/lib/macros.h new file mode 100644 index 0000000..3c0ad02 --- /dev/null +++ b/TP_13/exo2/lib/macros.h @@ -0,0 +1,32 @@ +/* + * ===================================================================================== + * + * Filename: macros.h + * + * Description: Some useful macros + * + * Version: 1.0 + * Created: 09/03/2017 15:28:46 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#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) + +/* FIXME: ensure we manipulate real array */ +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +#endif /* MACROS_H */ diff --git a/TP_13/exo2/lib/utils.c b/TP_13/exo2/lib/utils.c new file mode 100644 index 0000000..04a20e4 --- /dev/null +++ b/TP_13/exo2/lib/utils.c @@ -0,0 +1,13 @@ +#include "utils.h" + +void swap_int(int* v1, int* v2) { + int tmp = *v1; + *v1 = *v2; + *v2 = tmp; +} + +void swap_ptr(void* v1, void* v2) { + void* tmp = v1; + v1 = v2; + v2 = tmp; +} diff --git a/TP_13/exo2/lib/utils.h b/TP_13/exo2/lib/utils.h new file mode 100644 index 0000000..03f1226 --- /dev/null +++ b/TP_13/exo2/lib/utils.h @@ -0,0 +1,9 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "macros.h" + +void swap_int(int* v1, int* v2); +void swap_ptr(void* v1, void* v2); + +#endif /* UTILS_H */ diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c new file mode 100644 index 0000000..fcd3bcc --- /dev/null +++ b/TP_13/exo2/src/main.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#include "display.h" + +int main() { + int row, col; + + initscr(); + getmaxyx(stdscr,row,col); + noecho(); + curs_set(0); + + /* center base coordinates for the board */ + const int base_y = row/2 - 4; + const int base_x = col/2 - 7; + + print_board(base_y, base_x); + + print_x(base_y, base_x + 1); + print_o(base_y, base_x + 6); + print_o(base_y, base_x + 11); + print_o(base_y + 3, base_x + 1); + print_o(base_y + 6, base_x + 1); + print_o(base_y + 3, base_x + 6); + print_x(base_y + 3, base_x + 11); + print_x(base_y + 6, base_x + 6); + print_x(base_y + 6, base_x + 11); + + refresh(); + + while (getch() != 'q'); + + endwin(); + + exit(EXIT_SUCCESS); +} -- 2.34.1 From aae22ca298198e5791490702bfceefd78a0140b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Mar 2017 23:12:24 +0100 Subject: [PATCH 13/16] TP 11 exo2: sync with the latest Makefile version with LTO and GOLD support. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_11/exo2/Makefile | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/TP_11/exo2/Makefile b/TP_11/exo2/Makefile index d1ca467..5555dc6 100644 --- a/TP_11/exo2/Makefile +++ b/TP_11/exo2/Makefile @@ -42,18 +42,31 @@ BUILDDIR := .build/debug DEBUG_FLAG = -g STRIP_FLAG = OPTI_FLAG = -O0 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes else BUILDDIR := .build/release DEBUG_FLAG = STRIP_FLAG = -s OPTI_FLAG = -O3 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +endif + +ifeq ($(LTO_SUPPORT),yes) +CFLAGS_LTO = -flto -ffat-lto-objects +LDFLAGS_LTO = -fuse-linker-plugin -flto +endif + +ifeq ($(GOLD_SUPPORT),yes) +LDFLAGS_GOLD = -fuse-ld=gold endif # Putting header files in the source directory is not the purpose of this INCLUDES variable INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) -CFLAGS := $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) +CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) LIBCFLAGS := -fPIC $(CFLAGS) -LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) +LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) LIBLDFLAGS := -shared $(LDFLAGS) STATICLIBLDFLAGS := -static $(LDFLAGS) LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) -- 2.34.1 From 5df3071eec60ca43bc66e1266820de0572d4b629 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 16 Mar 2017 23:08:16 +0100 Subject: [PATCH 14/16] TP 13 exo2: More routine for MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit - handle the grid elements; - print the active elements. Signed-off-by: Jérôme Benoit --- TP_13/exo2/README | 10 +++++++ TP_13/exo2/lib/coordinates.c | 51 ++++++++++++++++++++++++++++++++++++ TP_13/exo2/lib/coordinates.h | 35 +++++++++++++++++++++++++ TP_13/exo2/lib/display.c | 41 +++++++++++++++++++++++++++-- TP_13/exo2/lib/display.h | 3 +++ TP_13/exo2/src/main.c | 31 +++++++++++++--------- 6 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 TP_13/exo2/README create mode 100644 TP_13/exo2/lib/coordinates.c create mode 100644 TP_13/exo2/lib/coordinates.h diff --git a/TP_13/exo2/README b/TP_13/exo2/README new file mode 100644 index 0000000..10090fe --- /dev/null +++ b/TP_13/exo2/README @@ -0,0 +1,10 @@ +Tic-tac-toe +----------- + +Touches correspondantes au case de la grille: + +a|z|e +-+-+- +q|s|d +-+-+- +w|x|c diff --git a/TP_13/exo2/lib/coordinates.c b/TP_13/exo2/lib/coordinates.c new file mode 100644 index 0000000..f9123e9 --- /dev/null +++ b/TP_13/exo2/lib/coordinates.c @@ -0,0 +1,51 @@ +/* + * ===================================================================================== + * + * Filename: coordinates.c + * + * Description: Data definition and functions to manipulate elements in the grid + * + * Version: 1.0 + * Created: 16/03/2017 19:05:02 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#include "coordinates.h" + +void init_coordinates(coordinates_t* coordinates_array) { + for (unsigned i = 0; i < MAX_COORDINATES; i++) { + coordinates_array[i] = set_coordinates(0, 0, 0); + } +} + +coordinates_t set_coordinates(int y, int x, unsigned type) { + coordinates_t new_coordinates; + + new_coordinates.y = y; + new_coordinates.x = x; + new_coordinates.type = type; + return new_coordinates; +} + +unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) { + /* valid coordinates are in the [1-3] range */ + if (new_coordinates.y < 1 || new_coordinates.y > 3 || new_coordinates.x < 1 || new_coordinates.x > 3) { + return 3; /* error value for invalid coordinates */ + } + for (unsigned i = 0; i < MAX_COORDINATES; i++) { + /* check if already entered */ + if (new_coordinates.y == (coordinates_array + i)->y && new_coordinates.x == (coordinates_array + i)->x) { + return 2; /* error value for duplicates */ + } else if ((coordinates_array + i)->y == 0 && (coordinates_array + i)->x == 0) { + coordinates_array[i] = new_coordinates; + return 0; /* error value when everything if fine */ + } + } + return 1; /* error value for full array */ +} diff --git a/TP_13/exo2/lib/coordinates.h b/TP_13/exo2/lib/coordinates.h new file mode 100644 index 0000000..873d404 --- /dev/null +++ b/TP_13/exo2/lib/coordinates.h @@ -0,0 +1,35 @@ +/* + * ===================================================================================== + * + * Filename: coordinates.h + * + * Description: Header for data definition and functions to manipulate elements in the grid + * + * Version: 1.0 + * Created: 16/03/2017 19:06:16 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef COORDINATES_H +#define COORDINATES_H + +/* we only have nine elements in the grid */ +#define MAX_COORDINATES 9 + +typedef struct coordinates_s { + int y; + int x; + unsigned type; /* 0 = O, 1 = X */ +} coordinates_t; + +void init_coordinates(coordinates_t* coordinates_array); +coordinates_t set_coordinates(int y, int x, unsigned type); +unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array); + +#endif /* COORDINATES_H */ diff --git a/TP_13/exo2/lib/display.c b/TP_13/exo2/lib/display.c index 22deb96..1e5def8 100644 --- a/TP_13/exo2/lib/display.c +++ b/TP_13/exo2/lib/display.c @@ -18,6 +18,8 @@ #include +#include "display.h" + /* in all print routine, y and x are the coordinates of the first character of the shape * which can be a space ' ' */ @@ -42,8 +44,8 @@ void print_board(int y, int x) { * - base_y + 3, base_x + 11 * - base_y + 6, base_x + 6 * - base_y + 6, base_x + 11 - * The added y value can be {0, 3, 6} - * The added x value can be {1, 6, 11} */ + * The added (y, x) couple values can be {0, 3, 6}x{1, 6, 11} + */ void print_x(int y, int x) { mvprintw(y, x, "\\/"); @@ -54,3 +56,38 @@ void print_o(int y, int x) { mvprintw(y, x, "/\\"); mvprintw(y+1, x, "\\/"); } + +/* y: 1 -> +0 x: 1 -> +1 + * 2 -> +3 2 -> +6 + * 3 -> +6 3 -> +11 */ +static int remap_y(int y) { + if (y == 1) { + return 0; + } else if (y == 2) { + return 3; + } else { + return 6; + } +} + +static int remap_x(int x) { + if (x == 1) { + return 1; + } else if (x == 2) { + return 6; + } else { + return 11; + } +} + +void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x) { + unsigned i = 0; + while ((coordinates_array + i)->y != 0 && (coordinates_array + i)->x != 0) { + if ((coordinates_array + i)->type == 0) { + print_o(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x)); + } else { + print_x(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x)); + } + i++; + } +} diff --git a/TP_13/exo2/lib/display.h b/TP_13/exo2/lib/display.h index f0b1136..c3390fe 100644 --- a/TP_13/exo2/lib/display.h +++ b/TP_13/exo2/lib/display.h @@ -19,8 +19,11 @@ #ifndef DISPLAY_H #define DISPLAY_H +#include "coordinates.h" + void print_board(int y, int x); void print_x(int y, int x); void print_o(int y, int x); +void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x); #endif /* DISPLAY_H */ diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index fcd3bcc..123ab08 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -1,32 +1,39 @@ #include #include +#include #include #include "display.h" +#include "coordinates.h" int main() { int row, col; + char* top_msg = ""; + char* back_msg = ""; initscr(); - getmaxyx(stdscr,row,col); + getmaxyx(stdscr, row, col); noecho(); curs_set(0); + /* array of the active coordinates in the entered order */ + coordinates_t coordinates_array[MAX_COORDINATES]; + init_coordinates(coordinates_array); + /* center base coordinates for the board */ - const int base_y = row/2 - 4; - const int base_x = col/2 - 7; + int base_y = row/2 - 4; + int base_x = col/2 - 7; + if (!top_msg) mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); print_board(base_y, base_x); + if (!back_msg) mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); + + int errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); + errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); + errno = add_coordinates(set_coordinates(2, 3, 1), coordinates_array); + errno = add_coordinates(set_coordinates(1, 1, 0), coordinates_array); - print_x(base_y, base_x + 1); - print_o(base_y, base_x + 6); - print_o(base_y, base_x + 11); - print_o(base_y + 3, base_x + 1); - print_o(base_y + 6, base_x + 1); - print_o(base_y + 3, base_x + 6); - print_x(base_y + 3, base_x + 11); - print_x(base_y + 6, base_x + 6); - print_x(base_y + 6, base_x + 11); + print_coordinates(coordinates_array, base_y, base_x); refresh(); -- 2.34.1 From 2737ed7c252e5f624b8f98cbe28eb05450c9420e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 17 Mar 2017 21:42:01 +0100 Subject: [PATCH 15/16] TP 13 exo2: preliminary loop for a full tic-tac-toe game for 2 players MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Still a little bit buggy. Signed-off-by: Jérôme Benoit --- TP_13/exo2/src/main.c | 74 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index 123ab08..0606beb 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -7,7 +7,7 @@ #include "coordinates.h" int main() { - int row, col; + int row, col, errno = 0, round = 0, player = 0, key_pressed; char* top_msg = ""; char* back_msg = ""; @@ -19,25 +19,77 @@ int main() { /* array of the active coordinates in the entered order */ coordinates_t coordinates_array[MAX_COORDINATES]; init_coordinates(coordinates_array); + coordinates_t new_coordinates = {0, 0, 0}; /* center base coordinates for the board */ int base_y = row/2 - 4; int base_x = col/2 - 7; - if (!top_msg) mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); print_board(base_y, base_x); - if (!back_msg) mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); - int errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); - errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); - errno = add_coordinates(set_coordinates(2, 3, 1), coordinates_array); - errno = add_coordinates(set_coordinates(1, 1, 0), coordinates_array); + do { + if (round % 2 == 0 || round == 0) { + player = 0; + top_msg = "Joueur 1 joue"; + } else { + player = 1; + top_msg = "Joueur 2 joue"; + } - print_coordinates(coordinates_array, base_y, base_x); - - refresh(); + mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); + mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); - while (getch() != 'q'); + print_coordinates(coordinates_array, base_y, base_x); + + key_pressed = getch(); + switch (key_pressed) { + case 'a': + new_coordinates = set_coordinates(1, 1, player); + break; + case 'z': + new_coordinates = set_coordinates(1, 2, player); + break; + case 'e': + new_coordinates = set_coordinates(1, 3, player); + break; + case 'q': + new_coordinates = set_coordinates(2, 1, player); + break; + case 's': + new_coordinates = set_coordinates(2, 2, player); + break; + case 'd': + new_coordinates = set_coordinates(2, 3, player); + break; + case 'w': + new_coordinates = set_coordinates(3, 1, player); + break; + case 'x': + new_coordinates = set_coordinates(3, 2, player); + break; + case 'c': + new_coordinates = set_coordinates(3, 3, player); + break; + default: + continue; + break; + } + + errno = add_coordinates(new_coordinates, coordinates_array); + + if (errno == 2) { + back_msg = "Choisir une case vide"; + continue; + } else if (errno == 3) { + back_msg = "Coordonnees invalides"; + continue; + } else if (errno == 0) { + back_msg = ""; + } + + round++; + + } while (errno == 0 || errno == 2); endwin(); -- 2.34.1 From 5d64063095177554839e67ddc73193929f115d07 Mon Sep 17 00:00:00 2001 From: Jerome Benoit Date: Sat, 18 Mar 2017 22:40:40 +0100 Subject: [PATCH 16/16] Buildsystem: be more friendly with cygwin environment Disable GOLD linker and some minor code cleanups Signed-off-by: Jerome Benoit --- TP_11/exo2/Makefile | 5 +++++ TP_11/exo2/lib/clist.c | 9 +++++---- TP_13/exo1/Makefile | 5 +++++ TP_13/exo2/Makefile | 5 +++++ TP_9/exo2/clist.c | 42 +++++++++++++++++++++--------------------- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/TP_11/exo2/Makefile b/TP_11/exo2/Makefile index 5555dc6..d8dd23a 100644 --- a/TP_11/exo2/Makefile +++ b/TP_11/exo2/Makefile @@ -36,6 +36,7 @@ AR=ar WARN_FLAGS = -Wall -Wextra STD_FLAG = -std=c11 +UNAME := $(shell uname -o) ifeq ($(BUILD_TYPE),debug) BUILDDIR := .build/debug @@ -53,6 +54,10 @@ LTO_SUPPORT = yes GOLD_SUPPORT = yes endif +ifeq ($(UNAME),Cygwin) +GOLD_SUPPORT = no +endif + ifeq ($(LTO_SUPPORT),yes) CFLAGS_LTO = -flto -ffat-lto-objects LDFLAGS_LTO = -fuse-linker-plugin -flto diff --git a/TP_11/exo2/lib/clist.c b/TP_11/exo2/lib/clist.c index 244a687..3031154 100644 --- a/TP_11/exo2/lib/clist.c +++ b/TP_11/exo2/lib/clist.c @@ -34,10 +34,11 @@ link_t* list_prepend(link_t* head, int value) { } link_t* list_insert(link_t* head, unsigned index, int value) { + unsigned max_index = list_count(head); if (index == 0) { return list_prepend(head, value); - } else if (index == list_count(head)) { + } else if (index == max_index) { return list_append(head, value); } else { link_t* link_insrt = list_new(value); @@ -58,7 +59,7 @@ link_t* list_delete(link_t* head, unsigned index) { link_t* head_prev = NULL; link_t* head_next = NULL; link_t* head_ret = NULL; - + if (head == NULL) { return NULL; } else if (index == 0) { @@ -209,8 +210,8 @@ void list_display_values(link_t* head) { printf("------Begin------\n"); while (head != NULL) { printf("value at [%d]=%d\n", i, head->value); - head = head->next; - i++; + head = head->next; + i++; } printf("------End------\n"); } diff --git a/TP_13/exo1/Makefile b/TP_13/exo1/Makefile index 5d1b9ab..152498c 100644 --- a/TP_13/exo1/Makefile +++ b/TP_13/exo1/Makefile @@ -36,6 +36,7 @@ AR=ar WARN_FLAGS = -Wall -Wextra STD_FLAG = -std=c11 +UNAME := $(shell uname -o) ifeq ($(BUILD_TYPE),debug) BUILDDIR := .build/debug @@ -53,6 +54,10 @@ LTO_SUPPORT = yes GOLD_SUPPORT = yes endif +ifeq ($(UNAME),Cygwin) +GOLD_SUPPORT = no +endif + ifeq ($(LTO_SUPPORT),yes) CFLAGS_LTO = -flto -ffat-lto-objects LDFLAGS_LTO = -fuse-linker-plugin -flto diff --git a/TP_13/exo2/Makefile b/TP_13/exo2/Makefile index 8f48f4e..2fa2084 100644 --- a/TP_13/exo2/Makefile +++ b/TP_13/exo2/Makefile @@ -37,6 +37,7 @@ AR=ar WARN_FLAGS = -Wall -Wextra STD_FLAG = -std=c11 +UNAME := $(shell uname -o) ifeq ($(BUILD_TYPE),debug) BUILDDIR := .build/debug @@ -54,6 +55,10 @@ LTO_SUPPORT = yes GOLD_SUPPORT = yes endif +ifeq ($(UNAME),Cygwin) +GOLD_SUPPORT = no +endif + ifeq ($(LTO_SUPPORT),yes) CFLAGS_LTO = -flto -ffat-lto-objects LDFLAGS_LTO = -fuse-linker-plugin -flto diff --git a/TP_9/exo2/clist.c b/TP_9/exo2/clist.c index 244a687..2fd3dfd 100644 --- a/TP_9/exo2/clist.c +++ b/TP_9/exo2/clist.c @@ -5,7 +5,7 @@ #include "clist.h" link_t* list_new(int value) { - link_t* link_new; + link_t* link_new; link_new = malloc(sizeof(link_t)); link_new->value = value; link_new->next = NULL; @@ -14,12 +14,12 @@ link_t* list_new(int value) { link_t* list_append(link_t* head, int value) { - if (head == NULL) { + if (head == NULL) { return head = list_new(value); } else { link_t* head_first = head; while (head->next != NULL) { - head = head->next; + head = head->next; } head->next = list_new(value); return head_first; @@ -28,7 +28,7 @@ link_t* list_append(link_t* head, int value) { link_t* list_prepend(link_t* head, int value) { link_t* first_link = list_new(value); - + first_link->next = head; return first_link; } @@ -36,11 +36,11 @@ link_t* list_prepend(link_t* head, int value) { link_t* list_insert(link_t* head, unsigned index, int value) { if (index == 0) { - return list_prepend(head, value); + return list_prepend(head, value); } else if (index == list_count(head)) { return list_append(head, value); } else { - link_t* link_insrt = list_new(value); + link_t* link_insrt = list_new(value); link_t* head_first = head; link_t* head_next = NULL; for (unsigned i = 0; i < index-1; i++) { @@ -49,7 +49,7 @@ link_t* list_insert(link_t* head, unsigned index, int value) { head_next = head->next; head->next = link_insrt; head = link_insrt; - head->next = head_next; + head->next = head_next; return head_first; } } @@ -58,7 +58,7 @@ link_t* list_delete(link_t* head, unsigned index) { link_t* head_prev = NULL; link_t* head_next = NULL; link_t* head_ret = NULL; - + if (head == NULL) { return NULL; } else if (index == 0) { @@ -108,12 +108,12 @@ link_t* list_sort(link_t* head) { tmp = head->value; head->value = head->next->value; head->next->value = tmp; - isswaped = true; + isswaped = true; } - head = head->next; + head = head->next; } - /* Reloop at the beginning of the list until there's values swaped */ - head = head_first; + /* Reloop at the beginning of the list until there's values swaped */ + head = head_first; } while (isswaped); return head_first; } @@ -129,10 +129,10 @@ static link_t* _list_merge_sort(link_t* head1, link_t* head2) { } if (head1->value < head2->value) { head_result = head1; - head_result->next = _list_merge_sort(head1->next, head2); + head_result->next = _list_merge_sort(head1->next, head2); } else { head_result = head2; - head_result->next = _list_merge_sort(head1, head2->next); + head_result->next = _list_merge_sort(head1, head2->next); } return head_result; } @@ -161,11 +161,11 @@ link_t* list_merge_sort(link_t* head) { unsigned list_count(link_t* head) { unsigned count = 0; - + while (head != NULL) { ++count; head = head->next; - } + } return count; } @@ -180,13 +180,13 @@ void list_set(link_t* head, unsigned index, int value) { } int list_get(link_t* head, unsigned index) { - unsigned count = 0; + unsigned count = 0; while (head != NULL && count < index) { ++count; - head = head->next; + head = head->next; } - if (head != NULL) { + if (head != NULL) { return head->value; } else { return -1; @@ -209,8 +209,8 @@ void list_display_values(link_t* head) { printf("------Begin------\n"); while (head != NULL) { printf("value at [%d]=%d\n", i, head->value); - head = head->next; - i++; + head = head->next; + i++; } printf("------End------\n"); } -- 2.34.1