Commit | Line | Data |
---|---|---|
c2bc2b96 JB |
1 | #include <stdio.h> |
2 | #include <stdbool.h> | |
3 | #include <stdlib.h> | |
4 | ||
5 | /** Pointer to a function with this profile */ | |
6 | typedef int(*operation_cb)(int v1, int v2); | |
7 | ||
8 | /** Describe a single operation */ | |
9 | typedef struct { | |
10 | const char* label; | |
11 | operation_cb cb; | |
12 | } operation_t; | |
13 | ||
14 | int doAddition(int v1, int v2); | |
15 | int doSubstraction(int v1, int v2); | |
16 | int doMultiplication(int v1, int v2); | |
17 | int doXOR(int v1, int v2); | |
18 | ||
19 | /** All available operations */ | |
20 | operation_t operations[4] = { | |
21 | { | |
22 | "Addition", | |
23 | doAddition | |
24 | }, | |
25 | { | |
26 | "Substraction", | |
27 | doSubstraction | |
28 | }, | |
29 | { | |
30 | "Multiplication", | |
31 | doMultiplication | |
32 | }, | |
33 | { | |
34 | "XOR", | |
35 | doXOR | |
36 | } | |
37 | }; | |
38 | #define operations_count ((int) (sizeof(operations) / sizeof(operations[0]))) | |
39 | ||
40 | /** Display a prompt to the user then wait for an integer input. */ | |
41 | void promptValue(const char* prompt, int* value) { | |
42 | printf("%s:\n", prompt); | |
43 | scanf("%d", value); | |
44 | } | |
45 | ||
46 | void displayMenuEntry(int index, const char* label) { | |
47 | printf("%d | %s\n", index, label); | |
48 | } | |
49 | ||
50 | /** Display all available operations and an EXIT option. */ | |
51 | void displayMenu() { | |
52 | for (int menuIndex = 0; menuIndex < operations_count; ++menuIndex) { | |
53 | displayMenuEntry(menuIndex + 1, operations[menuIndex].label); | |
54 | } | |
55 | ||
56 | displayMenuEntry(operations_count + 1, "Exit"); | |
57 | } | |
58 | ||
59 | /** Prompt the user for his operation choice. | |
60 | * | |
61 | * @return The selected operation. If the exit option is chosen, return NULL | |
62 | */ | |
63 | operation_t* promptOperation() { | |
64 | displayMenu(); | |
65 | ||
66 | do { | |
67 | int result; | |
68 | promptValue("Choose an option", &result); | |
69 | ||
70 | if (result == (operations_count + 1)) { | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | if (result >= 1 && result <= operations_count) { | |
75 | return &operations[result - 1]; | |
76 | } | |
77 | ||
78 | printf("Please choose a valid option (1-%d)\n", operations_count); | |
79 | } while (true); | |
80 | } | |
81 | ||
82 | int doAddition(int v1, int v2) { | |
83 | return v1 + v2; | |
84 | } | |
85 | ||
86 | int doSubstraction(int v1, int v2) { | |
87 | return v1 - v2; | |
88 | } | |
89 | ||
90 | int doMultiplication(int v1, int v2) { | |
91 | return v1 * v2; | |
92 | } | |
93 | ||
94 | int doXOR(int v1, int v2) { | |
95 | return v1 ^ v2; | |
96 | } | |
97 | ||
98 | int main() { | |
99 | operation_t* operation = promptOperation(); | |
100 | ||
101 | if (operation == NULL) { | |
102 | return 0; | |
103 | } | |
104 | ||
105 | int initialValue; | |
106 | promptValue("Initial value", &initialValue); | |
107 | ||
108 | do { | |
109 | int nextValue; | |
110 | promptValue("Next value", &nextValue); | |
111 | ||
112 | if (nextValue == 0) { | |
113 | break; | |
114 | } | |
115 | ||
116 | initialValue = operation->cb(initialValue, nextValue); | |
117 | printf("Result: %d\n", initialValue); | |
118 | } while (true); | |
119 | } |