Skip to content
Snippets Groups Projects
Verified Commit 3727c589 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

maj du dernier cours

parent 87d437c2
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment