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

chaged order

parent f0d9ffcc
Branches
No related tags found
No related merge requests found
Pipeline #14962 passed
...@@ -353,59 +353,6 @@ tab est trié dans G ...@@ -353,59 +353,6 @@ tab est trié dans G
# La calculatrice (2/8) # La calculatrice (2/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 (3/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);
}
```
# La calculatrice (4/8)
## De infixe à post-fixe ## De infixe à post-fixe
* Une *pile* est utilisée pour stocker *opérateurs* et *parenthèses*. * Une *pile* est utilisée pour stocker *opérateurs* et *parenthèses*.
...@@ -419,7 +366,7 @@ bool evaluate(char *postfix, double *val) { // init stack ...@@ -419,7 +366,7 @@ bool evaluate(char *postfix, double *val) { // init stack
``` ```
# La calculatrice (5/8) # La calculatrice (3/8)
## De infixe à post-fixe: algorithme ## De infixe à post-fixe: algorithme
...@@ -439,7 +386,7 @@ bool evaluate(char *postfix, double *val) { // init stack ...@@ -439,7 +386,7 @@ bool evaluate(char *postfix, double *val) { // init stack
* Si il n'y a pas de caractère dans l'expression dépiler tous les * Si il n'y a pas de caractère dans l'expression dépiler tous les
opérateurs dans le résultat. opérateurs dans le résultat.
# La calculatrice (6/8) # La calculatrice (4/8)
## De infixe à post-fixe: exemple ## De infixe à post-fixe: exemple
...@@ -459,7 +406,7 @@ Infixe Postfixe Pile Priorité ...@@ -459,7 +406,7 @@ Infixe Postfixe Pile Priorité
/(G+H) AB*D/F- Vide Néant /(G+H) AB*D/F- Vide Néant
``` ```
# La calculatrice (7/8) # La calculatrice (5/8)
## De infixe à post-fixe: exemple ## De infixe à post-fixe: exemple
...@@ -477,7 +424,7 @@ Infixe Postfixe Pile Priorité ...@@ -477,7 +424,7 @@ Infixe Postfixe Pile Priorité
Vide AB*D/F-GH+/ Vide Néant Vide AB*D/F-GH+/ Vide Néant
``` ```
# La calculatrice (8/8) # La calculatrice (6/8)
\footnotesize \footnotesize
...@@ -507,6 +454,58 @@ char *infix_to_postfix(char* infix) { // init and alloc stack and postfix ...@@ -507,6 +454,58 @@ char *infix_to_postfix(char* infix) { // init and alloc stack and 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);
}
```
# La liste chaînée et pile (1/N) # La liste chaînée et pile (1/N)
## Structure de données ## Structure de données
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment