Commit | Line | Data |
---|---|---|
33b9c646 | 1 | #include <stdio.h> |
475ee86d | 2 | #include <stdbool.h> |
33b9c646 JB |
3 | |
4 | #include "io.h" | |
cfdd46d2 | 5 | #include "array.h" |
475ee86d | 6 | #include "utils.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"); | |
33 | printf("7) Quitter.\n"); | |
475ee86d JB |
34 | } |
35 | ||
36 | int* do_concat(int array[], unsigned* size) { | |
37 | int* tab_to_concat = NULL; | |
38 | unsigned tab_to_concat_size = 0; | |
39 | printf("\n=== Saisie d'un tableau ===\n\n"); | |
40 | tab_to_concat = prompt_array(tab_to_concat, &tab_to_concat_size); | |
41 | int* tab_concat = concat_tab(array, *size, tab_to_concat, tab_to_concat_size); | |
42 | *size += tab_to_concat_size; | |
43 | free_tab(array); | |
44 | free_tab(tab_to_concat); | |
45 | return tab_concat; | |
46 | } | |
47 | ||
48 | void do_sort(int array[], unsigned size) { | |
49 | int errno = 0; | |
50 | int choice = 0; | |
51 | bool done = false; | |
52 | criteria_cb criteria; | |
53 | ||
54 | printf("\n=== Menu de tri ===\n\n"); | |
55 | printf("1) Croissant.\n"); | |
56 | printf("2) Decroissant.\n"); | |
57 | printf("3) Croissant pairs en premier.\n"); | |
58 | printf("4) Croissant impairs en premier.\n"); | |
59 | do { | |
60 | errno = prompt_value("Choix?", &choice); | |
61 | handle_prompt_error(errno); | |
62 | done = true; | |
63 | if (1 > choice || 4 < choice) { | |
64 | printf("\nFaire un choix compris entre 1 et 4\n"); | |
65 | done = false; | |
66 | } | |
67 | } while (!done); | |
68 | switch (choice) { | |
69 | case 1: | |
70 | criteria = ascending; | |
71 | break; | |
72 | case 2: | |
73 | criteria = descending; | |
74 | break; | |
75 | case 3: | |
76 | criteria = ascending_and_even; | |
77 | break; | |
78 | case 4: | |
79 | criteria = ascending_and_odd; | |
80 | break; | |
81 | default: | |
889d5862 | 82 | /* sort ascending by default, unused code path */ |
475ee86d JB |
83 | criteria = ascending; |
84 | break; | |
cfdd46d2 | 85 | } |
475ee86d JB |
86 | sort_tab(array, size, criteria); |
87 | } | |
88 | ||
89 | void do_count(int array[], unsigned size) { | |
90 | int errno = 0; | |
91 | int search_value = 0; | |
92 | ||
93 | errno = prompt_value("\nValeur a chercher?", &search_value); | |
94 | handle_prompt_error(errno); | |
95 | printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); | |
cfdd46d2 JB |
96 | } |
97 | ||
475ee86d | 98 | void display_array(int array[], unsigned size) { |
cfdd46d2 | 99 | if (array != NULL) { |
475ee86d JB |
100 | printf("\n--array begin--\n"); |
101 | for (unsigned i = 0; i < size; i++) { | |
cfdd46d2 JB |
102 | printf("value in array at index[%d]=%d\n", i, array[i]); |
103 | } | |
104 | printf("--array end--\n"); | |
105 | } else { | |
475ee86d | 106 | printf("\n--array NULL--\n"); |
33b9c646 JB |
107 | } |
108 | } |