Advertisement
Rakibul_Ahasan

Reverse Polish notation evaluation

Dec 1st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7. int top = 0, S[1000];
  8.  
  9. int pop()
  10. {
  11.     top--;
  12.     return S[top + 1];
  13. }
  14.  
  15. void push(int x)
  16. {
  17.     S[++top] = x;
  18. }
  19.  
  20. int main()
  21. {
  22.     stack<int>st;
  23.  
  24.     char s[100];
  25.     while(scanf("%s", s) != EOF)
  26.     {
  27.  
  28.         if(s[0] == '+')
  29.         {
  30.             int a = pop();
  31.             int b = pop();
  32.             push(a + b);
  33.         }
  34.         else if(s[0] == '*')
  35.         {
  36.             int a = pop();
  37.             int b = pop();
  38.             push(a * b);
  39.         }
  40.         else if(s[0] == '-')
  41.         {
  42.             int a = pop();
  43.             int b = pop();
  44.             push(b - a);
  45.         }
  46.         else
  47.         {
  48.             push(atoi(s));
  49.         }
  50.     }
  51.  
  52.     printf("%d\n", S[1]);
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement