Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. template<class T>
  7. struct Node
  8. {
  9. T data;
  10. Node* next=nullptr;
  11. };
  12.  
  13. template<class T>
  14. class Stack
  15. {
  16. private:
  17. Node<T>* first=nullptr;
  18. public:
  19. void add_element(T data)
  20. {
  21. if(first==nullptr)
  22. {
  23. Node<T>* a = new Node<T>;
  24. a->data=data;
  25. first=a;
  26. }
  27. else
  28. {
  29. Node<T>* current=first;
  30. while(current->next!=nullptr)
  31. {
  32. current=current->next;
  33. }
  34. Node<T>* a = new Node<T>;
  35. a->data=data;
  36. current->next=a;
  37. }
  38. }
  39. T peak()
  40. {
  41. Node<T>* current=first;
  42. while(current->next!=nullptr)
  43. {
  44. current=current->next;
  45. }
  46. return current->data;
  47. }
  48. void pop()
  49. {
  50. Node<T>* current=first;
  51. if(current->next==nullptr)
  52. {
  53. delete current;
  54. first=nullptr;
  55. }
  56. else
  57. {
  58. while(current->next->next!=nullptr)
  59. {
  60. current=current->next;
  61. }
  62. delete current->next;
  63. current->next=nullptr;
  64. }
  65. }
  66. void print()
  67. {
  68. Node<T>* current=first;
  69. while(current!=nullptr)
  70. {
  71. cout<<current->data;
  72. current=current->next;
  73. }
  74. }
  75. bool empt()
  76. {
  77. return first==nullptr;
  78. }
  79.  
  80. };
  81.  
  82. int main()
  83. {
  84. Stack<char> s;
  85.  
  86. string sentence;
  87. cout<<"Enter equazion: ";
  88. cin>>sentence;
  89. //sentence="1+(3+2-[2+3]*4-{<3+1>*(4-2)})";
  90. for(int i=0;i<sentence.size();i++)
  91. {
  92. if(sentence[i]=='('||sentence[i]=='{'||sentence[i]=='['||sentence[i]=='<')
  93. {
  94. s.add_element(sentence[i]);
  95. }
  96. if(sentence[i]==')'||sentence[i]=='}'||sentence[i]==']'||sentence[i]=='>')
  97. {
  98. if(sentence[i]==')'&&s.peak()=='(')
  99. {
  100. s.pop();
  101. }
  102. else if(sentence[i]=='}'&&s.peak()=='{')
  103. {
  104. s.pop();
  105. }
  106. else if(sentence[i]==']'&&s.peak()=='[')
  107. {
  108. s.pop();
  109. }
  110. else if(sentence[i]=='>'&&s.peak()=='<')
  111. {
  112. s.pop();
  113. }
  114. else
  115. {
  116. cout<<"Error"<<endl;
  117. break;
  118. }
  119. }
  120. }
  121. if(s.empt()==1)
  122. {
  123. cout<<"Correct"<<endl;
  124. return s.empt()==1;
  125. }
  126. else
  127. {
  128. cout<<"Sorry"<<endl;
  129. return s.empt()==1;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement