Advertisement
wurdalak007

Untitled

Sep 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class stack { // стек для хранения знаков операций
  8. public:
  9.  
  10. stack();
  11. ~stack();
  12. bool IsEmpty();
  13. char PopFront();
  14. void PushBack(char Operand);
  15. char GetFirst();
  16.  
  17. private:
  18.  
  19. int head;
  20. int count;
  21. int size;
  22. char* array;
  23.  
  24. void extendMem();
  25.  
  26. };
  27.  
  28. stack::stack() {
  29.  
  30. head = 0;
  31. count = 0;
  32. size = 8;
  33. array = new char[size];
  34.  
  35. }
  36.  
  37. char stack::GetFirst() {
  38. return array[head - 1];
  39. }
  40.  
  41. bool stack::IsEmpty() {
  42. if ( count == 0 ) {
  43. return true;
  44. }
  45. return false;
  46. }
  47.  
  48. char stack::PopFront() {
  49. if ( count == 0 ) {
  50. return -1;
  51. }
  52. head--;
  53. count--;
  54. return array[head];
  55. }
  56.  
  57. void stack::PushBack(char Operand) {
  58. if ( head == size ) {
  59. extendMem();
  60. }
  61.  
  62. array[head] = Operand;
  63. head++;
  64. count++;
  65.  
  66. }
  67.  
  68. void stack::extendMem() {
  69.  
  70. char* array1 = new char[size * 2];
  71. for (int j = 0; j < size; j++) {
  72. array1[j] = array[j];
  73. }
  74. delete[] array;
  75. array = array1;
  76. size = size * 2;
  77.  
  78. }
  79.  
  80. stack::~stack() {
  81. delete[] array;
  82. }
  83.  
  84. class stack1{ //стек для хранения чисел/ вычисления конечного результата
  85.  
  86. public:
  87. stack1();
  88. ~stack1();
  89. int PopFront();
  90. void PushBack(int Operand);
  91.  
  92. private:
  93. int head;
  94. int count;
  95. int size;
  96. int* array;
  97.  
  98. void extendMem();
  99.  
  100. };
  101.  
  102. stack1::stack1() {
  103.  
  104. head = 0;
  105. count = 0;
  106. size = 8;
  107. array = new int[size];
  108.  
  109. }
  110.  
  111. int stack1::PopFront() {
  112.  
  113. if (count == 0) {
  114. return -1;
  115. }
  116. head--;
  117. count--;
  118. return array[head];
  119.  
  120. }
  121.  
  122. void stack1::PushBack(int Operand) {
  123.  
  124. if (head == size) {
  125. extendMem();
  126. }
  127.  
  128. array[head] = Operand;
  129. head++;
  130. count++;
  131.  
  132. }
  133.  
  134. void stack1::extendMem() {
  135.  
  136. int* array1 = new int[size * 2];
  137. for (int j = 0; j < size; j++) {
  138. array1[j] = array[j];
  139. }
  140. delete[] array;
  141. array = array1;
  142. size = size * 2;
  143.  
  144. }
  145.  
  146. stack1::~stack1() {
  147. delete[] array;
  148. }
  149.  
  150. bool IsOperation(char data) {
  151. switch (data) {
  152. case '+': return true;
  153. case '-': return true;
  154. case '/': return true;
  155. case '*': return true;
  156. case ')': return true;
  157. case '(': return true;
  158. default: return false;
  159. }
  160. }
  161.  
  162. int computation(int a, int b, char j) {
  163. switch (j) {
  164. case '+': return a+b;
  165. case '-': return a-b;
  166. case '/': return a/b;
  167. case '*': return a*b;
  168. default: break;
  169. }
  170. }
  171.  
  172. int main() {
  173. string input; // входная строка
  174. string output; // выходная строка ОПЗ(Обратная Польская Запись)
  175. string CurrentResult; //хранение промежуточных чисел из ОПЗ строки
  176. stack operands;
  177. stack1 numbers;
  178. char k;
  179. int result = 0;
  180.  
  181.  
  182. cin >> input;
  183.  
  184. for (int i = 0; i < input.length(); i++) { // Перевод в ОПЗ
  185. if (!IsOperation(input[i])) {
  186. output = output + input[i];
  187.  
  188. }
  189. else {
  190. if (input[i] != '(') {
  191. output = output + ' ';
  192. }
  193. }
  194.  
  195. if (IsOperation(input[i])) {
  196. if (input[i] == ')') {
  197. while (!(operands.IsEmpty() || operands.GetFirst() == '('))
  198. output = output + operands.PopFront();
  199. k = operands.PopFront();
  200. }
  201. else {
  202. if ( ((((input[i] == '+') ||
  203. (input[i] == '-')) &&
  204. (operands.GetFirst() != '(')) ||
  205. (((input[i] == '/') ||
  206. (input[i] == '*')) &&
  207. ((operands.GetFirst() == '/') ||
  208. (operands.GetFirst() == '*'))))&&
  209. (IsOperation(operands.GetFirst())) ) {
  210.  
  211. output = output + operands.PopFront();
  212. operands.PushBack(input[i]);
  213. }
  214. else {
  215. operands.PushBack(input[i]);
  216. }
  217. }
  218. }
  219.  
  220. if (i == input.length() - 1) {
  221. while (!(operands.IsEmpty() || operands.GetFirst() == '('))
  222. output = output + operands.PopFront();
  223. }
  224. }
  225.  
  226.  
  227.  
  228. for (int j = 0; j < output.length(); j++) { // Обработка выходной строки для вычисления результата
  229.  
  230. if (!IsOperation(output[j])) {
  231. if (output[j] != ' ') {
  232. CurrentResult = CurrentResult + output[j];
  233. }
  234. else {
  235. if (CurrentResult.length() != 0) {
  236. numbers.PushBack(atoi(CurrentResult.c_str()));
  237. CurrentResult.clear();
  238. }
  239. }
  240.  
  241. }
  242.  
  243. else
  244. {
  245. if (CurrentResult.length() != 0) {
  246. numbers.PushBack(atoi(CurrentResult.c_str()));
  247. CurrentResult.clear();
  248. }
  249. int b = numbers.PopFront();
  250. int a = numbers.PopFront();
  251. numbers.PushBack(computation(a, b, output[j]));
  252. }
  253. }
  254.  
  255. cout << numbers.PopFront();
  256.  
  257. return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement