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

added codes

parent 92402c7e
No related branches found
No related tags found
No related merge requests found
Pipeline #14959 passed
......@@ -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;
}
```
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