diff --git a/slides/cours_9.md b/slides/cours_9.md index f9cd6cefde05e47278dc93f7f198ed8d7af7148d..b8ba7a12db39b2b9d942617f0302d74ebd8971eb 100644 --- a/slides/cours_9.md +++ b/slides/cours_9.md @@ -301,195 +301,4 @@ tab est trié dans G -``` - -# La calculatrice (1/8) - -## Vocabulaire - -```C -2 + 3 = 2 3 +, -``` - -`2` et `3` sont les *opérandes*, `+` l'*opérateur*. - -. . . - -## La notation infixe - -```C -2 * (3 + 2) - 4 = 6. -``` - -## La notation postfixe - -```C -2 3 2 + * 4 - = 6. -``` - -## Exercice: écrire `2 * 3 * 4 + 2` en notation `postfixe` - -. . . - -```C -2 3 4 * * 2 + = (2 * (3 * 4)) + 2. -``` - -# La calculatrice (2/8) - -## De infixe à post-fixe - -* Une *pile* est utilisée pour stocker *opérateurs* et *parenthèses*. -* Les opérateurs on des *priorités* différentes. - -```C -^ : priorité 3 -* / : priorité 2 -+ - : priorité 1 -( ) : priorité 0 // pas un opérateur mais bon -``` - - -# La calculatrice (3/8) - -## De infixe à post-fixe: algorithme - -* On lit l'expression infixe de gauche à droite. - -* On examine le prochain caractère de l'expression infixe. - * Si opérande, le placer dans l'expression du résultat. - * Si parenthèse le mettre dans la pile (priorité 0). - * Si opérateur, comparer sa priorité avec celui du sommet de la pile: - * Si sa priorité est plus élevée, empiler. - * Sinon dépiler l'opérateur de la pile dans l'expression du résultat et - recommencer jusqu'à apparition d'un opérateur de priorité plus faible - au sommet de la pile (ou pile vide). - * Si parenthèse fermée, dépiler les opérateurs du sommet de la pile et les - placer dans l'expression du résultat, jusqu'à ce qu'une parenthèse - ouverte apparaisse au sommet, dépiler également la parenthèse. - * Si il n'y a pas de caractère dans l'expression dépiler tous les - opérateurs dans le résultat. - -# La calculatrice (4/8) - -## De infixe à post-fixe: exemple - -```C -Infixe Postfixe Pile Priorité -((A*B)/D-F)/(G+H) Vide Vide Néant - (A*B)/D-F)/(G+H) Vide ( 0 - A*B)/D-F)/(G+H) Vide (( 0 - *B)/D-F)/(G+H) A (( 0 - B)/D-F)/(G+H) A ((* 2 - )/D-F)/(G+H) AB ((* 2 - /D-F)/(G+H) AB* ( 0 - D-F)/(G+H) AB* (/ 2 - -F)/(G+H) AB*D (/ 2 - F)/(G+H) AB*D/ (- 1 - )/(G+H) AB*D/F (- 1 - /(G+H) AB*D/F- Vide Néant -``` - -# La calculatrice (5/8) - -## De infixe à post-fixe: exemple - -```C -Infixe Postfixe Pile Priorité -((A*B)/D-F)/(G+H) Vide Vide Néant --------------------------------------------------------- - /(G+H) AB*D/F- Vide Néant - (G+H) AB*D/F- / 2 - G+H) AB*D/F- /( 0 - +H) AB*D/F-G /( 0 - H) AB*D/F-G /(+ 1 - ) AB*D/F-GH /(+ 1 - Vide AB*D/F-GH+ / 2 - Vide AB*D/F-GH+/ Vide Néant -``` - -# La calculatrice (6/8) - -\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; -} -``` - - -# La calculatrice (7/8) - -## Évaluation d'expression postfixe: algorithme - -* Chaque *opérateur* porte sur les deux opérandes qui le précèdent. -* Le *résultat d'une opération* est un nouvel *opérande* qui est remis au - sommet de la pile. - -## Exemple - -```C -2 3 4 + * 5 - = ? -``` - -* On parcours de gauche à droite: - -```C -Caractère lu Pile opérandes - 2 2 - 3 2, 3 - 4 2, 3, 4 - + 2, (3 + 4) - * 2 * 7 - 5 14, 5 - - 14 - 5 = 9 -``` - -# La calculatrice (8/8) - -## Évaluation d'expression postfixe: algorithme - -1. La valeur d'un opérande est *toujours* empilée. -2. L'opérateur s'applique *toujours* au 2 opérandes au sommet. -3. Le résultat est remis au sommet. - -## 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); -} -``` - +``` \ No newline at end of file