Commit | Line | Data |
---|---|---|
04b0afb5 | 1 | #include <stdlib.h> |
33b9c646 | 2 | #include <stdio.h> |
475ee86d | 3 | #include <stdbool.h> |
33b9c646 JB |
4 | |
5 | #include "io.h" | |
cfdd46d2 | 6 | #include "array.h" |
33b9c646 JB |
7 | |
8 | int prompt_value(const char* msg, int* result) { | |
9 | puts(msg); | |
10 | int retVal = scanf("%d", result); | |
11 | return (retVal == 1) ? 0 : 1; | |
12 | } | |
13 | ||
cfdd46d2 JB |
14 | int* prompt_array(int array[], unsigned* size) { |
15 | int errno = prompt_value("Taille du tableau?", (int*)size); | |
16 | array = create_tab(array, *size); | |
17 | for (unsigned i = 0; i < *size; i++) { | |
18 | errno += prompt_value("Valeur?", &array[i]); | |
19 | } | |
475ee86d JB |
20 | /* error might have occured */ |
21 | handle_prompt_error(errno); | |
22 | return array; | |
23 | } | |
24 | ||
25 | void display_choice_menu() { | |
26 | printf("\n=== Menu ===\n\n"); | |
27 | printf("1) Saisir puis concatener un autre tableau.\n"); | |
28 | printf("2) Trier le tableau.\n"); | |
29 | printf("3) Afficher le tableau.\n"); | |
30 | printf("4) Compter le nombre d'occurence d'un entier dans le tableau.\n"); | |
889d5862 JB |
31 | printf("5) Compter le nombre d'entiers pairs dans le tableau.\n"); |
32 | printf("6) Compter le nombre d'entiers impairs dans le tableau.\n"); | |
59941dc1 JB |
33 | printf("7) Redimensionner le tableau.\n"); |
34 | printf("8) Quitter.\n"); | |
475ee86d JB |
35 | } |
36 | ||
37 | int* do_concat(int array[], unsigned* size) { | |
38 | int* tab_to_concat = NULL; | |
39 | unsigned tab_to_concat_size = 0; | |
40 | printf("\n=== Saisie d'un tableau ===\n\n"); | |
41 | tab_to_concat = prompt_array(tab_to_concat, &tab_to_concat_size); | |
42 | int* tab_concat = concat_tab(array, *size, tab_to_concat, tab_to_concat_size); | |
43 | *size += tab_to_concat_size; | |
44 | free_tab(array); | |
45 | free_tab(tab_to_concat); | |
46 | return tab_concat; | |
47 | } | |
48 | ||
49 | void do_sort(int array[], unsigned size) { | |
50 | int errno = 0; | |
51 | int choice = 0; | |
52 | bool done = false; | |
210f7f05 | 53 | s_criteria_cb sort_criteria; |
475ee86d JB |
54 | |
55 | printf("\n=== Menu de tri ===\n\n"); | |
56 | printf("1) Croissant.\n"); | |
57 | printf("2) Decroissant.\n"); | |
58 | printf("3) Croissant pairs en premier.\n"); | |
59 | printf("4) Croissant impairs en premier.\n"); | |
60 | do { | |
61 | errno = prompt_value("Choix?", &choice); | |
62 | handle_prompt_error(errno); | |
63 | done = true; | |
64 | if (1 > choice || 4 < choice) { | |
65 | printf("\nFaire un choix compris entre 1 et 4\n"); | |
66 | done = false; | |
67 | } | |
68 | } while (!done); | |
69 | switch (choice) { | |
70 | case 1: | |
210f7f05 | 71 | sort_criteria = ascending; |
475ee86d JB |
72 | break; |
73 | case 2: | |
210f7f05 | 74 | sort_criteria = descending; |
475ee86d JB |
75 | break; |
76 | case 3: | |
210f7f05 | 77 | sort_criteria = ascending_and_even; |
475ee86d JB |
78 | break; |
79 | case 4: | |
210f7f05 | 80 | sort_criteria = ascending_and_odd; |
475ee86d JB |
81 | break; |
82 | default: | |
889d5862 | 83 | /* sort ascending by default, unused code path */ |
210f7f05 | 84 | sort_criteria = ascending; |
475ee86d | 85 | break; |
cfdd46d2 | 86 | } |
210f7f05 | 87 | sort_tab(array, size, sort_criteria); |
475ee86d JB |
88 | } |
89 | ||
90 | void do_count(int array[], unsigned size) { | |
91 | int errno = 0; | |
92 | int search_value = 0; | |
93 | ||
94 | errno = prompt_value("\nValeur a chercher?", &search_value); | |
95 | handle_prompt_error(errno); | |
96 | printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); | |
cfdd46d2 JB |
97 | } |
98 | ||
59941dc1 JB |
99 | void do_resize(int array[], unsigned* old_size) { |
100 | int errno = 0; | |
101 | unsigned new_size = 0; | |
102 | ||
103 | errno = prompt_value("\nNouvelle taille?", (int*)&new_size); | |
104 | handle_prompt_error(errno); | |
210f7f05 JB |
105 | /* FIXME: one should able the set the array new content if new_size > *old_size |
106 | * for now, new values are zeroed */ | |
59941dc1 JB |
107 | array = resize_tab(array, *old_size, new_size); |
108 | *old_size = new_size; | |
109 | } | |
110 | ||
04b0afb5 JB |
111 | void handle_prompt_error(int errno) { |
112 | if (errno != 0) { | |
113 | printf("\nMerci de saisir un nombre entier, exiting\n"); | |
114 | /* it's somewhat violent but better than looping forever */ | |
115 | exit(EXIT_FAILURE); | |
116 | } | |
117 | } | |
118 | ||
475ee86d | 119 | void display_array(int array[], unsigned size) { |
cfdd46d2 | 120 | if (array != NULL) { |
475ee86d JB |
121 | printf("\n--array begin--\n"); |
122 | for (unsigned i = 0; i < size; i++) { | |
cfdd46d2 JB |
123 | printf("value in array at index[%d]=%d\n", i, array[i]); |
124 | } | |
125 | printf("--array end--\n"); | |
126 | } else { | |
475ee86d | 127 | printf("\n--array NULL--\n"); |
33b9c646 JB |
128 | } |
129 | } |