TP 13 exo1: Implement all the required features.
[TD_C.git] / TP_13 / exo1 / lib / io.c
index 1217750eb86ffd271cf1507af174e9dbf5a7aeb7..989e746aa237cd90d18c0b3861427deefd5f1bf2 100644 (file)
@@ -1,7 +1,9 @@
 #include <stdio.h>
+#include <stdbool.h>
 
 #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");
     }
 }