Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Algo - TP3 */
- /* Module principal */
- #include <stdio.h>
- #include <stdlib.h>
- #include "listes.h"
- #include "liste_ext.h"
- #include "stack.h"
- #include <errno.h>
- double evaluer_operation (char *e)
- {
- stack s ;
- char *fin ;
- long n ;
- int a, b ;
- create_empty_stack (&s) ;
- while (*e != '\0') {
- n = strtol (e, &fin, 10) ;
- if ((n != 0)) {
- push (n, &s) ;
- e = fin ;
- } else {
- /* switch (*e) {
- case ' ' : *e++ ;
- break;
- case '+' :
- a = pop (&s) ;
- b = pop (&s) ;
- push (a + b, &s) ;
- break;
- case '-' :
- a = pop (&s) ;
- b = pop (&s) ;
- push (a - b, &s) ;
- break ;
- case '*' :
- a = pop (&s) ;
- b = pop (&s) ;
- push (a * b, &s) ;
- break ;
- case '/' :
- a = pop (&s) ;
- b = pop (&s) ;
- push (a / b, &s) ;
- break ;
- } */
- if (*e == ' ') *e++ ;
- if (*e == '+') {
- a = pop (&s) ;
- b = pop (&s) ;
- push (a + b, &s) ;
- *e++ ;
- } else if (*e == '-') {
- a = pop (&s) ;
- b = pop (&s) ;
- push (a - b, &s) ;
- *e++ ;
- } else if (*e == '*') {
- a = pop (&s) ;
- b = pop (&s) ;
- push (a * b, &s) ;
- *e++ ;
- } else if (*e == '/') {
- a = pop (&s) ;
- b = pop (&s) ;
- push (a / b, &s) ;
- *e++ ;
- }
- }
- }
- a = pop (&s) ;
- return a ;
- }
- int main ()
- {
- char *expr = "1 4 5 + * 3 2 * +" ;
- double res ;
- res = evaluer_operation (expr) ;
- printf ("%lf", res) ;
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement