Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include<stack>
  2. #include<iostream>
  3. #include<conio.h>
  4. using namespace std;
  5. int getPriority(char op)
  6. {
  7. switch (op)
  8. {
  9. case '(': return -1;
  10. case '*':
  11. case '/': return 1;
  12. case '+':
  13. case '-': return 2;
  14. }
  15. }
  16.  
  17. template<class T>
  18. void printStack(stack<T> s)
  19. {
  20. stack<T> buf;
  21. while(s.size() != 0)
  22. {
  23. buf.push(s.top());
  24. s.pop();
  25. }
  26. while(buf.size() != 0)
  27. {
  28. cout << buf.top()<<" ";
  29. buf.pop();
  30. }
  31. cout << endl;
  32. }
  33. int main()
  34. {
  35. stack<char> znak;
  36. stack<int> nums;
  37. int a = 0;char ch;
  38. while((ch = getchar()) != '\n')
  39. {
  40. if(ch == '+' || ch == '-' || ch == '*' || ch == '/') znak.push(ch);
  41. else if(ch >= '0' && ch <='9')
  42. {
  43. a = a*10 + ch-'0';
  44. }
  45. else if(a != 0) {nums.push(a);a = 0;}
  46. }
  47. printStack(nums);
  48. printStack(znak);
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement