Advertisement
vkichukova

Untitled

Apr 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. // 1 zadacha
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. //const int MAX = 100;
  8.  
  9. class Stack
  10. {
  11. int current; //индекс на върха на стека
  12. int capacity; //колко елемента има в стека
  13. int * elements; //елементите на стека
  14.  
  15. void copyStack(const Stack& other)
  16. {
  17. current = other.current;
  18. capacity = other.capacity;
  19. elements = new int[capacity];
  20. for(int i = 0; i < capacity; i++)
  21. elements[i] = other.elements[i];
  22. }
  23. void deleteStack()
  24. {
  25. delete [] elements;
  26. }
  27. void resize()
  28. {
  29. int newSize = capacity*2;
  30. int* helper = new int [capacity];
  31. for (int i = 0; i < capacity; i++)
  32. {
  33. helper[i] = elements[i];
  34. }
  35. delete[] elements;
  36. elements = new int[newSize];
  37. for (int i = 0; i < capacity; i++)
  38. {
  39. elements[i] = helper[i];
  40. }
  41. capacity = newSize;
  42. delete[] helper;
  43. }
  44. public:
  45. Stack()
  46. {
  47. current = -1;
  48. capacity = 10;
  49. elements = new int[capacity];
  50. }
  51. Stack(const Stack& other)
  52. {
  53. copyStack(other);
  54. }
  55. Stack& operator=(const Stack& other)
  56. {
  57. if(this != &other)
  58. {
  59. deleteStack();
  60. copyStack(other);
  61. }
  62. return *this;
  63. }
  64. bool isEmpty()
  65. {
  66. return current < 0;
  67. }
  68. bool isFull()
  69. {
  70. return current == capacity - 1;
  71. }
  72. int numOfElements()
  73. {
  74. return current+1;
  75. }
  76. void push(int element)
  77. {
  78. if(isFull())
  79. resize();
  80.  
  81. elements[++current] = element;
  82. }
  83. void pop()
  84. {
  85. if(!isEmpty())
  86. {
  87. current--;
  88. capacity--;
  89. }
  90. else
  91. cout << "The stack is empty!";
  92. }
  93. int peek() //top()
  94. {
  95. if(!isEmpty())
  96. return elements[current];
  97. else
  98. cout << "The stack is empty.";
  99. }
  100. friend ostream& operator<<(ostream& os, const Stack& s)
  101. {
  102. for (int i = 0; i < s.capacity; i++)
  103. {
  104. os << s.elements[i] << " ";
  105. }
  106. return os;
  107. }
  108. };
  109.  
  110. int calculate(char str[])
  111. {
  112. Stack st;
  113. int n = strlen(str);
  114. for(int i = 0; i < n; )
  115. {
  116. if(str[i] == ' ') i++;
  117. if(str[i] >= '0' && str[i] <= '9')
  118. {
  119. st.push(str[i] - 48);
  120. i++;
  121. }
  122. else if(str[i] == '+')
  123. {
  124. if(st.numOfElements() < 2) return -1;
  125. else
  126. {
  127. int x = st.peek();
  128. st.pop();
  129. int y = st.peek();
  130. st.pop();
  131. int z = x + y; //cout << z << endl;
  132. st.push(z);
  133. i++;
  134. }
  135. }
  136. else if(str[i] == '-')
  137. {
  138. if(st.numOfElements() < 2) return -1;
  139. else
  140. {
  141. int x = st.peek();
  142. st.pop();
  143. int y = st.peek();
  144. st.pop();
  145. int z = y - x; //cout << z << endl;
  146. st.push(z);
  147. i++;
  148. }
  149. }
  150. else if(str[i] == '*')
  151. {
  152. if(st.numOfElements() < 2) return -1;
  153. else
  154. {
  155. int x = st.peek();
  156. st.pop();
  157. int y = st.peek();
  158. st.pop();
  159. int z = x * y; //cout << z << endl;
  160. st.push(z);
  161. i++;
  162. }
  163. }
  164. else if(str[i] == '/')
  165. {
  166. if(st.numOfElements() < 2) return -1;
  167. else
  168. {
  169. int x = st.peek();
  170. st.pop();
  171. int y = st.peek();
  172. st.pop();
  173. if(x == 0) return -1;
  174. else
  175. {
  176. int z = y / x; //cout << z << endl;
  177. st.push(z);
  178. i++;
  179. }
  180. }
  181. }
  182. else if(str[i] == '%')
  183. {
  184. if(st.numOfElements() < 2) return -1;
  185. else
  186. {
  187. int x = st.peek();
  188. st.pop();
  189. int y = st.peek();
  190. st.pop();
  191. if(x == 0) return -1;
  192. else
  193. {
  194. int z = y % x; //cout << z << endl;
  195. st.push(z);
  196. i++;
  197. }
  198. }
  199. }
  200. }
  201. if(st.numOfElements() == 1)
  202. return st.peek();
  203. else return -1; // wrong input
  204. }
  205.  
  206. int main()
  207. {
  208. char expr[101];
  209. cin.getline(expr,101);
  210. cout << calculate(expr);
  211. return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement