From 33b9c6461d00f013d692dde3c3f79f0b002ce564 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 9 Mar 2017 21:53:57 +0100 Subject: [PATCH] TP 13 exo1: Add libraries functions already written 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.h | 1 + TP_13/exo1/lib/io.c | 15 +++++++++++++++ TP_13/exo1/lib/io.h | 2 ++ TP_13/exo1/lib/sort.c | 29 +++++++++++++++++++++++++++++ TP_13/exo1/lib/sort.h | 9 +++++++++ TP_13/exo1/lib/utils.c | 13 +++++++++++++ TP_13/exo1/lib/utils.h | 7 +++++++ TP_13/exo1/src/{exo1.c => main.c} | 0 8 files changed, 76 insertions(+) create mode 100644 TP_13/exo1/lib/utils.c create mode 100644 TP_13/exo1/lib/utils.h rename TP_13/exo1/src/{exo1.c => main.c} (100%) diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h index 1f0be27..3326460 100644 --- a/TP_13/exo1/lib/array.h +++ b/TP_13/exo1/lib/array.h @@ -2,4 +2,5 @@ #define ARRAY_H + #endif /* ARRAY_H */ diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index e69de29..4af0b66 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -0,0 +1,15 @@ +#include + +#include "io.h" + +int prompt_value(const char* msg, int* result) { + puts(msg); + int retVal = scanf("%d", result); + return (retVal == 1) ? 0 : 1; +} + +void display_array(int* array, int size) { + for (int i = 0; i < size; i++) { + printf("value in array at index[%d]=%d\n", i, array[i]); + } +} diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 56eef23..8b2fb6e 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -1,6 +1,8 @@ #ifndef IO_H #define IO_H +int prompt_value(const char* msg, int* result); +void display_array(int* array, int size); #endif /* IO_H */ diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index e69de29..aef713d 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -0,0 +1,29 @@ +#include "utils.h" +#include "sort.h" + +bool ascending(int a, int b) { + return a > b; +} + +bool descending(int a, int b) { + return a < b; +} + +static bool sort_first(int* array, int length, criteria_cb criteria) { + bool rt = false; + for (int i = 0; i < length-1; i++) { + if (criteria(array[i], array[i+1])) { + swap_int_ptr(&array[i], &array[i+1]); + if (!rt) { rt = true; }; + } + } + return rt; +} + +/* this function is awaited in the array.c file */ +void sort_array(int* array, int length, criteria_cb criteria) { + bool rt; + do { + rt = sort_first(array, length, criteria); + } while (rt); +} diff --git a/TP_13/exo1/lib/sort.h b/TP_13/exo1/lib/sort.h index 86fd5c4..57b51de 100644 --- a/TP_13/exo1/lib/sort.h +++ b/TP_13/exo1/lib/sort.h @@ -1,5 +1,14 @@ #ifndef SORT_H #define SORT_H +#include + +typedef bool(*criteria_cb)(int a, int b); + +/* sort criteria */ +bool ascending(int a, int b); +bool descending(int a, int b); + +void sort_array(int* array, int length, criteria_cb criteria); #endif /* SORT_H */ diff --git a/TP_13/exo1/lib/utils.c b/TP_13/exo1/lib/utils.c new file mode 100644 index 0000000..c48206f --- /dev/null +++ b/TP_13/exo1/lib/utils.c @@ -0,0 +1,13 @@ +#include "utils.h" + +void swap_int_ptr(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/exo1/lib/utils.h b/TP_13/exo1/lib/utils.h new file mode 100644 index 0000000..1acc243 --- /dev/null +++ b/TP_13/exo1/lib/utils.h @@ -0,0 +1,7 @@ +#ifndef UTILS_H +#define UTILS_H + +void swap_int_ptr(int* v1, int* v2); +void swap_ptr(void* v1, void* v2); + +#endif /* UTILS_H */ diff --git a/TP_13/exo1/src/exo1.c b/TP_13/exo1/src/main.c similarity index 100% rename from TP_13/exo1/src/exo1.c rename to TP_13/exo1/src/main.c -- 2.34.1