TP 13 exo2: Implement the missing bits to do a full game.
[TD_C.git] / TP_13 / exo1 / lib / io.c
index 1217750eb86ffd271cf1507af174e9dbf5a7aeb7..69b13d768f2b68e4cd0a01e01f2e23ab30a8368a 100644 (file)
@@ -1,4 +1,6 @@
+#include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 
 #include "io.h"
 #include "array.h"
@@ -15,21 +17,113 @@ 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) Compter le nombre d'entiers pairs dans le tableau.\n");
+    printf("6) Compter le nombre d'entiers impairs dans le tableau.\n");
+    printf("7) Redimensionner le tableau.\n");
+    printf("8) 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;
+    s_criteria_cb sort_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:
+            sort_criteria = ascending;
+            break;
+        case 2:
+            sort_criteria = descending;
+            break;
+        case 3:
+            sort_criteria = ascending_and_even;
+            break;
+        case 4:
+            sort_criteria = ascending_and_odd;
+            break;
+        default:
+            /* sort ascending by default, unused code path */
+            sort_criteria = ascending;
+            break;
+    }
+    sort_tab(array, size, sort_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 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);
+    /* 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;
+}
+
+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);
     }
 }
 
-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");
     }
 }