From: Jérôme Benoit Date: Wed, 15 Feb 2017 21:20:18 +0000 (+0100) Subject: exo2.c: Finish the implementation of the desired features: X-Git-Url: https://git.piment-noir.org/?p=TD_C.git;a=commitdiff_plain;h=e6c858930912957d218329822392267886ee46c2 exo2.c: Finish the implementation of the desired features: - sequence the chosen operation on the new value and the last operation result; - implement the main loop exit conditions. Signed-off-by: Jérôme Benoit --- diff --git a/exo2/exo2.c b/exo2/exo2.c index 8bb939d..4a76130 100644 --- a/exo2/exo2.c +++ b/exo2/exo2.c @@ -1,5 +1,5 @@ #include -//#include +#include //FIXME: Comment the code !!! @@ -25,7 +25,7 @@ int promptValue(const char* invite) { int promptOperation() { displayMenu(); - return promptValue("Veuillez saisir un choix ?"); + return promptValue("Veuillez choisir une operation ?"); } int doAddition(int val1, int val2) { @@ -41,6 +41,7 @@ int doMultiplication(int val1, int val2) { } int doDivision(int val1, int val2) { + //FIXME: Useless code path given the exit main loop condition if (val2 == 0) { printf("Division par zero !\n"); // FIXME: I'm not very fond of this convention ... @@ -62,9 +63,52 @@ int doPuissance(int base, int expo) { return power; } +int doOperation(int selection, int val1, int val2) { + int op_result; + switch (selection) { + case 1: + op_result = doAddition(val1, val2); + break; + case 2: + op_result = doSubstraction(val1, val2); + break; + case 3: + op_result = doMultiplication(val1, val2); + break; + case 4: + op_result = doDivision(val1, val2); + break; + case 5: + op_result = doPuissance(val1, val2); + break; + case 6: + break; + default: + puts("Faire un choix compris entre 1 et 6"); + } + return op_result; +} + int main() { int choice = promptOperation(); - printf("Choice: %d\n", choice); + int value1 = promptValue("Veuillez saisir une valeur entiere initiale ?"); + int value2 = 0, result = 0; + bool first_loop = true; + + for (;;) { + if (choice == 6) break; + if (first_loop) { + value2 = promptValue("Veuillez saisir une valeur entiere avec laquelle l'operation sera effectue ?"); + if (value2 == 0) break; + result = doOperation(choice, value1, value2); + first_loop = false; + } else { + value2 = promptValue("Veuillez saisir la prochaine valeur entiere avec laquelle l'operation sera effectue sur l'ancien resultat ?"); + if (value2 == 0) break; + result = doOperation(choice, result, value2); + } + printf("Le resultat de l'operation choisie est %d\n\n", result); + } return 0; }