Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 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. /* switch (*e) {
  26. case ' ' : *e++ ;
  27. break;
  28. case '+' :
  29. a = pop (&s) ;
  30. b = pop (&s) ;
  31. push (a + b, &s) ;
  32. break;
  33. case '-' :
  34. a = pop (&s) ;
  35. b = pop (&s) ;
  36. push (a - b, &s) ;
  37. break ;
  38. case '*' :
  39. a = pop (&s) ;
  40. b = pop (&s) ;
  41. push (a * b, &s) ;
  42. break ;
  43. case '/' :
  44. a = pop (&s) ;
  45. b = pop (&s) ;
  46. push (a / b, &s) ;
  47. break ;
  48. } */
  49. if (*e == ' ') *e++ ;
  50. if (*e == '+') {
  51. a = pop (&s) ;
  52. b = pop (&s) ;
  53. push (a + b, &s) ;
  54. *e++ ;
  55. } else if (*e == '-') {
  56. a = pop (&s) ;
  57. b = pop (&s) ;
  58. push (a - b, &s) ;
  59. *e++ ;
  60. } else if (*e == '*') {
  61. a = pop (&s) ;
  62. b = pop (&s) ;
  63. push (a * b, &s) ;
  64. *e++ ;
  65. } else if (*e == '/') {
  66. a = pop (&s) ;
  67. b = pop (&s) ;
  68. push (a / b, &s) ;
  69. *e++ ;
  70. }
  71. }
  72. }
  73.  
  74. a = pop (&s) ;
  75. return a ;
  76. }
  77.  
  78. int main ()
  79. {
  80. char *expr = "1 4 5 + * 3 2 * +" ;
  81. double res ;
  82.  
  83. res = evaluer_operation (expr) ;
  84. printf ("%lf", res) ;
  85.  
  86. return 0 ;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement