diff --git a/slides/cours_8.md b/slides/cours_8.md index 00d38d0beedc1e19917df4940b11686b8dcf52c2..c4b38f3164a4cafc7584dbb2776768f0330b4e39 100644 --- a/slides/cours_8.md +++ b/slides/cours_8.md @@ -634,6 +634,22 @@ Caractère lu Pile opérandes ## Exercice: écrire l'algorithme (et poster sur matrix) +```C +bool evaluate(char *postfix, double *val) { // init stack + for (size_t i = 0; i < strlen(postfix); ++i) { + if (is_operand(postfix[i])) { + stack_push(&s, postfix[i]); + } else if (is_operator(postfix[i])) { + double rhs = stack_pop(&s); + double lhs = stack_pop(&s); + stack_push(&s, op(postfix[i], lhs, rhs); + } } + return stack_pop(&s); +} +``` + + + # La calculatrice (4/N) ## De infixe à post-fixe @@ -709,6 +725,30 @@ Infixe Postfixe Pile Priorité # La calculatrice (8/N) +\footnotesize + ## Exercice: écrire le code et le poster sur matrix +* Quelle est la signature de la fonction? + . . . + +```C +char *infix_to_postfix(char* infix) { // init and alloc stack and postfix + for (size_t i = 0; i < strlen(infix); ++i) { + if (is_operand(infix[i])) { + // we just add operands in the new postfix string + } else if (infix[i] == '(') { // we push opening parenthesis into the stack + stack_push(&s, infix[i]); + } else if (infix[i] == ')') { + // we pop everything into the postfix + } else if (is_operator(infix[i])) { + // this is an operator. We add it to the postfix based + // on the priority of what is already in the stack and push it + } + } + // pop all the operators from the s at the end of postfix + // and end the postfix with `\0` + return postfix; +} +```