Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. struct Node{
  7. string obj;
  8. Node * next;
  9. };
  10.  
  11. class Stack{
  12. protected:
  13. Node * head;
  14. Node * tail;
  15. public:
  16. Stack():head(NULL), tail(NULL){};
  17. bool ifEmpty();
  18. Stack(const Stack&element);
  19. void Show_stack();
  20. void push(string object);
  21. void pop();
  22. // bool ifBrackets(char a);
  23. string GetAnOperator();
  24. };
  25.  
  26. bool Stack :: ifEmpty(){
  27. if(head==NULL)
  28. return true;
  29. return false;
  30. }
  31.  
  32. void Stack:: Show_stack(){
  33. Node * temp = head;
  34. while(temp!= NULL){
  35. cout<<temp->obj;
  36. temp = temp->next;
  37. }
  38. }
  39.  
  40. void Stack :: push(string object){
  41. Node * temp = new Node;
  42. temp->obj = object;
  43. if(head!=NULL){
  44. temp->next = head;
  45. head = temp;
  46. }
  47. else{
  48. head = temp;
  49. tail = temp;
  50. temp->next = NULL;
  51. }
  52. delete temp;
  53. }
  54.  
  55. void Stack::pop (){
  56. Node * temp = head;
  57. head = head->next;
  58. delete temp;
  59. }
  60. Stack::Stack(const Stack&element){
  61. Node * temp = element.head;
  62. while(temp){
  63. push(temp->obj);
  64. temp = temp->next;
  65. }
  66. }
  67.  
  68. class Queue{
  69. protected:
  70. Node * head;
  71. Node * tail;
  72. public:
  73. Queue():head(NULL), tail(NULL){};
  74. Queue(const Queue&element);
  75. void Show_queue();
  76. void push_queue(string obj);
  77. void pop_queue();
  78. };
  79.  
  80. void Queue :: Show_queue(){
  81. Node * temp = head;
  82. while(temp!= NULL){
  83. cout<<temp->obj;
  84. temp = temp->next;
  85. }
  86. }
  87.  
  88. void Queue :: push_queue(string object){
  89. Node * temp = new Node;
  90. temp->obj = object;
  91. if(head!=NULL){
  92. tail->next = temp;
  93. tail = temp;
  94. }
  95. else{
  96. tail = temp;
  97. head = temp;
  98. temp->next = NULL;
  99. }
  100. delete temp;
  101. }
  102.  
  103. void Queue :: pop_queue(){
  104. Node * temp = head;
  105. head = head->next;
  106. delete temp;
  107. }
  108.  
  109. Queue :: Queue(const Queue&element){
  110. Node * temp = element.head;
  111. while(temp){
  112. push_queue(temp->obj);
  113. temp = temp->next;
  114. }
  115. }
  116.  
  117.  
  118. bool ifOperator(char a){
  119. if (a == '(' || a == '+' || a=='-' || a=='{' || a=='[' || a=='*' || a=='/' || a=='^' || a == ')' || a == ']' || a == '}' )
  120. return true;
  121. else return false;
  122. }
  123.  
  124. int priority(string a){
  125. int prior = 0;
  126. if (a == "^")
  127. prior = 4;// приоритет текущего оператора
  128. else if (a == "*" || a == "/")
  129. prior = 3;
  130. else if (a == ")" || a == "]" || a == "}")
  131. prior = 2;
  132. else if (a == "+" || a == "-")
  133. prior = 1;
  134. else if (a == "(" || a == "[" || a == "{")
  135. prior = 0;
  136. return prior;
  137. }
  138.  
  139. string Stack :: GetAnOperator(){
  140. return head->obj;
  141. }
  142.  
  143. void PolishNotation (Stack & object, Queue & element,char a){
  144. if(ifOperator(a) == true){
  145. string str(1,a);
  146. int prior = priority(str);//вычисляем приоритет текущего оператора
  147. if(object.ifEmpty() != true){//если стек не пустой, то проверяем операторы на выход
  148. while(priority(object.GetAnOperator()) >= prior){
  149. if(object.GetAnOperator() != "(" || object.GetAnOperator() != "["
  150. || object.GetAnOperator() != "{" || object.GetAnOperator() != ")"
  151. || object.GetAnOperator() != "]" || object.GetAnOperator() != "}")
  152. element.push_queue(object.GetAnOperator());// добавляем в очередь
  153. object.pop();//убираем из стека
  154. }
  155. }
  156. if(str!= ")"|| str != "]" || str != "}")
  157. object.push(str);//кладем в стек текущий элемент
  158. }
  159. else{
  160. string str (1,a);
  161. element.push_queue(str);
  162. }
  163. }
  164.  
  165.  
  166. int main(){
  167. Stack operatory;
  168. string expression;
  169. getline(cin,expression);
  170. Queue express;
  171. for(int i =0; i<expression.size(); i++){
  172. PolishNotation(operatory,express,expression[i]);
  173. }
  174. express.Show_queue();
  175. system("pause");
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement