Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /* Algo - TP3 */
  2. /* Module principal */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "listes.h"
  7. #include "liste_ext.h"
  8. #include "stack.h"
  9. #include <errno.h>
  10.  
  11. double evaluer_operation (char *e)
  12. {
  13. stack s ;
  14. char *fin ;
  15. long n ;
  16. int a, b ;
  17.  
  18. create_empty_stack (&s) ;
  19. while (*e != '\0') {
  20. n = strtol (e, &fin, 10) ;
  21. if ((n != 0)) {
  22. push (n, &s) ;
  23. e = fin ;
  24. } else {
  25. if (*e == ' ') *e++ ;
  26. if (*e == '+') {
  27. a = pop (&s) ;
  28. b = pop (&s) ;
  29. push (a + b, &s) ;
  30. *e++ ;
  31. } else if (*e == '-') {
  32. a = pop (&s) ;
  33. b = pop (&s) ;
  34. push (a - b, &s) ;
  35. *e++ ;
  36. } else if (*e == '*') {
  37. a = pop (&s) ;
  38. b = pop (&s) ;
  39. push (a * b, &s) ;
  40. *e++ ;
  41. } else if (*e == '/') {
  42. a = pop (&s) ;
  43. b = pop (&s) ;
  44. push (a / b, &s) ;
  45. *e++ ;
  46. }
  47. }
  48. }
  49.  
  50. a = pop (&s) ;
  51. return a ;
  52. }
  53.  
  54. int main ()
  55. {
  56. char *expr = "1 4 5 + * 3 2 * +" ;
  57. double res ;
  58.  
  59. res = evaluer_operation (expr) ;
  60. printf ("%lf", res) ;
  61.  
  62. return 0 ;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement