Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct Stack{
  5. private :
  6. char stack[150];
  7. int size=0;
  8. public:
  9.  
  10. void push(char a){
  11. size++;
  12. stack[size]=a;
  13. }
  14. char pop(){
  15. size--;
  16. return stack [size+1];
  17. }
  18. char top(){
  19. return stack[size];
  20. }
  21. int getSize()
  22. {
  23. return size;
  24. }
  25.  
  26. };
  27.  
  28. struct numStack{
  29. private :
  30. int stackOfNum[150];
  31. int sizeNum=0;
  32. public:
  33.  
  34. void push(int a){
  35. sizeNum++;
  36. stackOfNum[sizeNum]=a;
  37. }
  38. int pop(){
  39. sizeNum--;
  40. return stackOfNum[sizeNum+1];
  41. }
  42. int top(){
  43. return stackOfNum[sizeNum];
  44. }
  45. int getSizeNum()
  46. {
  47. return sizeNum;
  48. }
  49.  
  50. };
  51.  
  52. int priority(char op){
  53. if(op=='(')return 1;
  54. if(op=='+'||op=='-')return 2;
  55. if(op=='*'||op=='/')return 3;
  56. if(op=='^')return 4;
  57. }
  58. string polNot(string notation){
  59. string result ="";
  60. Stack operation;
  61. int i;
  62. for(i = 0; i<notation.size(); i++){
  63. if (notation[i]>='0'&& notation[i]<='9'){
  64. result.push_back(notation[i]);
  65. }
  66. else
  67. {
  68. if(notation[i]=='('){
  69. operation.push('(');
  70. }
  71. else{
  72. if(notation[i]==')'){
  73. while(operation.top() != '(')
  74. result.push_back(operation.pop());
  75. operation.pop();
  76. }
  77. else
  78. if(operation.getSize()==0 || priority(notation[i])>priority(operation.top())){
  79. operation.push(notation[i]);
  80. }
  81.  
  82. else{
  83. while(operation.getSize()>0 && priority(notation[i])<= priority(operation.top())){
  84. result.push_back(operation.pop());
  85. }
  86. operation.push(notation[i]);
  87.  
  88. }
  89. }
  90.  
  91. }
  92. }
  93.  
  94. while(operation.getSize()>0){
  95. result.push_back(operation.pop());
  96.  
  97. }
  98.  
  99. return result;
  100. }
  101. string str;
  102. numStack numberStack;
  103. int resultat(string str){
  104. for(int i = 0; i< str.size(); i++)
  105. if(str[i] >= '0' && str[i]<='9'){
  106. numberStack.push(str[i]-'0');
  107. }
  108. else {
  109. int a,b;
  110. a = numberStack.top();
  111. numberStack.pop();
  112. b = numberStack.pop();
  113. numberStack.pop();
  114. if(str[i]=='+') numberStack.push(b + a);
  115. if(str[i]=='-') numberStack.push(b - a);
  116. if(str[i]=='*') numberStack.push(b * a);
  117. if(str[i]=='/') numberStack.push(b / a);
  118. if(str[i]=='^') numberStack.push(b ^ a);
  119. }
  120. return numberStack.top();
  121. };
  122.  
  123. int main(){
  124. string s;
  125. cout << "Enter exercise: ";
  126. cin >> s;
  127. string polka = polNot(s);
  128. cout << "Result = " << resultat(polka);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement