Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <vector>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Stack
  7. {
  8. vector<int> st;
  9. int size;
  10.  
  11. Stack(int n)
  12. {
  13. size = 0;
  14. st.resize(n);
  15. }
  16.  
  17. void push(int value)
  18. {
  19. st[++size] = value;
  20. }
  21.  
  22. int pop()
  23. {
  24. return st[size--];
  25. }
  26. };
  27.  
  28. int res(int a, char op, int b)
  29. {
  30. if (op == '+') return a + b;
  31. if (op == '-') return a - b;
  32. if (op == '*') return a * b;
  33. }
  34.  
  35. int main()
  36. {
  37. ifstream fin("postfix.in");
  38. ofstream fout("postfix.out");
  39. string str;
  40. getline(fin, str);
  41. Stack s(str.size());
  42.  
  43. for(int i = 0;i<str.length();i++)
  44. {
  45. if (str[i] == ' ') continue;
  46. if (isdigit(str[i]))
  47. {
  48. s.push(str[i] - '0');
  49. }
  50. else
  51. {
  52. s.push(res(s.pop(), str[i], s.pop()));
  53. }
  54. }
  55. fout << s.pop();
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement