Add TP_4
[TD_C.git] / TP_4 / tp4.c
CommitLineData
af2ef71d
JB
1#include <stdio.h>
2
3void displayMenuEntry(int num, const char* option) {
4 printf("%d - %s\n", num, option);
5}
6
7void displayMenu() {
8 displayMenuEntry(1, "Addition");
9 displayMenuEntry(2, "Soustraction");
10 displayMenuEntry(3, "Multiplication");
11 displayMenuEntry(4, "Division");
12 displayMenuEntry(5, "Puissance");
13 displayMenuEntry(6, "Quitter");
14}
15
16int prompt(const char* msg) {
17 puts(msg);
18 int value;
19 scanf("%d", &value);
20 return value;
21}
22
23int promptUserChoice() {
24 int choice;
25 displayMenu();
26 return choice = prompt("Veuillez saisir un choix d'operation ?");
27}
28
29int doAddition(int val1, int val2) {
30 return val1 + val2;
31}
32
33int doSubstraction(int val1, int val2) {
34 return val1 - val2;
35}
36
37int doMultiplication(int val1, int val2) {
38 return val1 * val2;
39}
40
41int doDivision(int val1, int val2) {
42 if (val2 == 0) {
43 printf("Division par zero !\n");
44 //FIXME: I'm not very fond of this convention ...
45 return 0;
46 } else {
47 return val1 / val2;
48 }
49}
50
51int doPuissance(int base, int expo) {
52 int power = 1;
53 for (int iter = 0; iter < expo; iter++) {
54 power *= base ;
55 }
56 return power;
57}
58
59int doPuissanceSmart(int base, int expo) {
60 int power;
61 if (expo == 1) {
62 power = base;
63 } else if (expo % 2 == 0) {
64 power = doPuissanceSmart(base*base, expo/2);
65 } else {
66 power = base*doPuissanceSmart(base*base,(expo-1)/2);
67 }
68 return power;
69}
70
71int doPuissanceSmartIter(int base, int expo) {
72 int power = 1;
73 while (expo > 1) {
74 if (expo % 2 == 0) {
75 expo /= 2;
76 } else {
77 power *= base;
78 expo = (expo - 1) / 2;
79 }
80 base *= base;
81 }
82 return power * base;
83}
84
85int doOperation(int selection, int val1, int val2) {
86 int op_result;
87 switch (selection) {
88 case 1:
89 op_result = doAddition(val1, val2);
90 break;
91 case 2:
92 op_result = doSubstraction(val1, val2);
93 break;
94 case 3:
95 op_result = doMultiplication(val1, val2);
96 break;
97 case 4:
98 op_result = doDivision(val1, val2);
99 break;
100 case 5:
101 op_result = doPuissanceSmartIter(val1, val2);
102 break;
103 case 6:
104 break;
105 default:
106 puts("Faire un choix entre 1 et 6");
107 }
108 return op_result;
109}
110
111/* int promptValue(const char* invite) {
112 int input;
113 return input = prompt(invite);
114} */
115
116void promptValue(const char* invite, int* value) {
117 puts(invite);
118 scanf("%d", value);
119}
120
121void promptBothValues(int* val1, int* val2) {
122 promptValue("Veuillez saisir la premiere valeur", val1);
123 promptValue("Veuillez saisir la deuxieme valeur", val2);
124}
125
126int main() {
127 int choiceresult, value1, value2, result;
128 for (;;) {
129 choiceresult = promptUserChoice();
130 if (choiceresult == 6) break;
131 promptBothValues(&value1, &value2);
132 result = doOperation(choiceresult, value1, value2);
133 printf("Le resultat est %d\n", result);
134 printf("\n");
135 }
136}