exo1.c: fix a typo
[TD_C.git] / exo2 / exo2.c
1 #include <stdio.h>
2 //#include <stdbool.h>
3
4 //FIXME: Comment the code !!!
5
6 void displayMenuEntry(int num, const char* option) {
7 printf("%d - %s\n", num, option);
8 }
9
10 void displayMenu() {
11 displayMenuEntry(1, "Addition");
12 displayMenuEntry(2, "Soustraction");
13 displayMenuEntry(3, "Multiplication");
14 displayMenuEntry(4, "Division");
15 displayMenuEntry(5, "Puissance");
16 displayMenuEntry(6, "Quitter");
17 }
18
19 int promptValue(const char* invite) {
20 int value = 0;
21 puts(invite);
22 scanf("%d", &value);
23 return value;
24 }
25
26 int promptOperation() {
27 displayMenu();
28 return promptValue("Veuillez saisir un choix ?");
29 }
30
31 int doAddition(int val1, int val2) {
32 return val1 + val2;
33 }
34
35 int doSubstraction(int val1, int val2) {
36 return val1 - val2;
37 }
38
39 int doMultiplication(int val1, int val2) {
40 return val1 * val2;
41 }
42
43 int doDivision(int val1, int val2) {
44 if (val2 == 0) {
45 printf("Division par zero !\n");
46 // FIXME: I'm not very fond of this convention ...
47 return 0;
48 } else {
49 return val1 / val2;
50 }
51 }
52
53 int doPuissance(int base, int expo) {
54 int power;
55 if ( expo == 1) {
56 power = base;
57 } else if ( expo % 2 == 0 ) {
58 power = doPuissance(base*base, expo/2);
59 } else {
60 power = base*doPuissance(base*base,(expo-1)/2);
61 }
62 return power;
63 }
64
65 int main() {
66 int choice = promptOperation();
67 printf("Choice: %d\n", choice);
68
69 return 0;
70 }