#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);
return (retVal == 1) ? 0 : 1;
}
-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]);
+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]);
+ }
+ /* 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 display_array(int array[], unsigned size) {
+ if (array != NULL) {
+ 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("\n--array NULL--\n");
}
- printf("--array end--\n");
}