Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3.  
  4. template <typename T>
  5. struct Node {
  6. T data;
  7. Node<T>* next;
  8. };
  9.  
  10. template <typename T>
  11. class Stack {
  12. private:
  13. Node<T>* topNode;
  14. void copy(Node<T>* toCopy);
  15. void eraseStack();
  16. void copyStack(Stack const& s);
  17.  
  18. public:
  19. Stack();
  20. Stack(Stack const& s);
  21. Stack& operator=(Stack const& s);
  22. ~Stack();
  23.  
  24. bool empty();
  25. void push(T const& x);
  26. T pop();
  27. T top() const;
  28. };
  29. template <typename T>
  30. inline void Stack<T>::copy(Node<T>* toCopy)
  31. {
  32. if (toCopy == NULL) return;
  33. copy(toCopy->next);
  34. push(toCopy->data);
  35. }
  36.  
  37. template <typename T>
  38. inline void Stack<T>::eraseStack()
  39. {
  40. while (!empty()) {
  41. pop();
  42. }
  43. }
  44.  
  45. template <typename T>
  46. inline void Stack<T>::copyStack(Stack const & s)
  47. {
  48. topNode = NULL;
  49. copy(s.topNode);
  50. }
  51.  
  52. template <typename T>
  53. inline Stack<T>::Stack() : topNode(NULL)
  54. {}
  55.  
  56. template <typename T>
  57. inline Stack<T>::Stack(Stack const & s)
  58. {
  59. copyStack(s);
  60. }
  61.  
  62. template <typename T>
  63. inline Stack<T> & Stack<T>::operator=(Stack const & s)
  64. {
  65. if (this != &s) {
  66. eraseStack();
  67. copyStack(s);
  68. }
  69. return *this;
  70. }
  71.  
  72. template <typename T>
  73. inline Stack<T>::~Stack()
  74. {
  75. eraseStack();
  76. }
  77.  
  78. template <typename T>
  79. inline bool Stack<T>::empty()
  80. {
  81. return topNode==NULL;
  82. }
  83.  
  84. template <typename T>
  85. inline void Stack<T>::push(T const & x)
  86. {
  87. Node<T>* p = new Node<T>;
  88. p->data = x;
  89. p->next = topNode;
  90. topNode = p;
  91. }
  92.  
  93. template <typename T>
  94. inline T Stack<T>::pop()
  95. {
  96. if (empty()) {
  97. std::cout << "Empty Stack!\n";
  98. return 0;
  99. }
  100. Node<T>* p = topNode;
  101. topNode = topNode->next;
  102. T x = p->data;
  103. delete p;
  104. return x;
  105. }
  106.  
  107. template <typename T>
  108. inline T Stack<T>::top() const
  109. {
  110. if (empty()) {
  111. std::cout << "Empty Stack!\n";
  112. return 0;
  113. }
  114. return topNode->data;
  115. }
  116.  
  117.  
  118. bool isNumber(char a)
  119. {
  120. return a >= '0' && a <= '9';
  121. }
  122.  
  123. int parseToInt(char a) {
  124. return a - '0';
  125. }
  126.  
  127. int calculate(char* arr) {
  128. Stack<int> st;
  129. int num = 0;
  130. for (int i = 0; arr[i] != '\0'; i++) {
  131. while(isNumber(arr[i])){
  132. num*=10;
  133. num+=parseToInt(arr[i]);
  134. i++;
  135. }
  136. if(arr[i] == ' '){
  137. st.push(num);
  138. num = 0;
  139. }
  140. if (arr[i] == '+') {
  141. int x = st.pop();
  142. int y = st.pop();
  143. st.push(x + y);
  144. if(arr[i+1]==' ') i++;
  145. }
  146. if (arr[i] == '-') {
  147. int x = st.pop();
  148. int y = st.pop();
  149. st.push(y - x);
  150. if(arr[i+1]==' ') i++;
  151. }
  152. if (arr[i] == '*') {
  153. int x = st.pop();
  154. int y = st.pop();
  155. st.push(x * y);
  156. if(arr[i+1]==' ') i++;
  157. }
  158. if (arr[i] == '/') {
  159. int x = st.pop();
  160. int y = st.pop();
  161. if(arr[i+1]==' ') i++;
  162. if (x != 0) {
  163. st.push(y / x);
  164. }
  165. else {
  166. st.push(0);
  167. }
  168. }
  169. return st.pop();
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement