Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. template <typename T>
  8. struct stack {
  9. T* data;
  10. unsigned realSize,
  11. allocatedSize;
  12.  
  13. stack() {
  14. data = nullptr;
  15. realSize = allocatedSize = 0;
  16. };
  17.  
  18. void resize() {
  19. if (allocatedSize == 0 || allocatedSize == 1) {
  20. allocatedSize++;
  21. }
  22. else {
  23. allocatedSize *= 1.5;
  24. }
  25. T* temp_data = nullptr;
  26. if (data) {
  27. temp_data = new T[realSize];
  28. memcpy(temp_data, data, sizeof(T) * realSize);
  29. delete[] data;
  30. }
  31. data = new T[allocatedSize];
  32. if (realSize) {
  33. memcpy(data, temp_data, sizeof(T) * realSize);
  34. delete[] temp_data;
  35. }
  36. };
  37.  
  38. void push(T obj) {
  39. if (realSize >= allocatedSize) {
  40. resize();
  41. }
  42. data[realSize] = obj;
  43. realSize++;
  44. };
  45.  
  46. T pop() {
  47. if (realSize) {
  48. realSize--;
  49. return data[realSize];
  50. }
  51. return -1;
  52. };
  53.  
  54. void print() {
  55. for (int i = 0; i < realSize; i++) {
  56. cout << data[i] << " ";
  57. }
  58. cout << endl;
  59. };
  60.  
  61. T last() {
  62. if (realSize) {
  63. return data[realSize - 1];
  64. }
  65. return -1;
  66. }
  67.  
  68. bool is_empty() {
  69. if (!realSize)
  70. return true;
  71. else
  72. return false;
  73. }
  74.  
  75. ~stack() {
  76. delete[]data;
  77. realSize = allocatedSize = 0;
  78. };
  79. };
  80.  
  81. struct deque {
  82. int data;
  83. deque* next = 0, * prev = 0;
  84. deque* last = 0, * first = 0;
  85.  
  86. void push_back(int obj) {
  87. deque* adr = new deque;
  88. if (last) {
  89. adr->prev = last;
  90. last->next = adr;
  91. }
  92. adr->next = nullptr;
  93. adr->data = obj;
  94. last = adr;
  95. if (!first)
  96. first = adr;
  97. };
  98.  
  99. void push_front(int obj) {
  100. deque* adr = new deque;
  101. if (first) {
  102. adr->next = first;
  103. first->prev = adr;
  104. }
  105. adr->prev = nullptr;
  106. adr->data = obj;
  107. first = adr;
  108. if (!last)
  109. last = adr;
  110. };
  111.  
  112. int pop_back() {
  113. int res = last->data;
  114. if (last->prev) {
  115. last = last->prev;
  116. delete last->next;
  117. last->next = nullptr;
  118. }
  119. else {
  120. delete last;
  121. last = first = nullptr;
  122. }
  123. return res;
  124. };
  125.  
  126. int pop_front() {
  127. int res = first->data;
  128. if (first->next) {
  129. first = first->next;
  130. delete first->prev;
  131. first->prev = nullptr;
  132. }
  133. else {
  134. delete first;
  135. first = last = nullptr;
  136. }
  137. return res;
  138. };
  139.  
  140. void print() {
  141. deque* adr = first;
  142. while (adr) {
  143. cout << adr->data << " ";
  144. adr = adr->next;
  145. }
  146. cout << endl;
  147. }
  148.  
  149. ~deque() {
  150. if (first) {
  151. while (first->next) {
  152. first = first->next;
  153. delete first->prev;
  154. }
  155. delete first;
  156. first = last = nullptr;
  157. }
  158. }
  159. };
  160.  
  161. bool isPriorityHigher(char entered, char lastInStack) {
  162. if (lastInStack == '(')
  163. return true;
  164. switch (entered) {
  165. case '^':
  166. return true;
  167. case '*':
  168. case '/':
  169. if (lastInStack == '+' || lastInStack == '-')
  170. return true;
  171. }
  172. return false;
  173. }
  174.  
  175. string output(char ch) {
  176. string str{};
  177. switch (ch) {
  178. case 's':
  179. return "sin";
  180. case 'c':
  181. return "cos";
  182. case 'S':
  183. return "arcsin";
  184. case 'C':
  185. return "arccos";
  186. case 'T':
  187. return "tg";
  188. case 'G':
  189. return "ctg";
  190. default:
  191. return str + ch;
  192. }
  193. }
  194.  
  195. int main()
  196. {
  197. char task = 0, operation = 0;
  198. do {
  199. stack<int> intStack;
  200. deque deque;
  201. string str{};
  202. stack<char> operationsStack;
  203. bool digits = false;
  204. cout << "Choose ur destiny!\n" << "[1] - stack (int)\n" << "[2] - deque\n" << "[3] - postfix\n" << "[ESC] - exit\n";
  205. task = _getch();
  206. if (task != '1' && task != '2' && task != '3' && task != 27 /*esc*/) {
  207. system("cls");
  208. cout << "ERROR: BAD ENTER, TRY AGAIN!\n";
  209. }
  210. switch (task) {
  211. case '1' /*stack*/:
  212. system("cls");
  213. do {
  214. cout << "What do u want to do with ur stack?\n" << "[1] - push smt\n" << "[2] - pop last\n" << "[3] - display content\n" << "[ESC] - back\n";
  215. operation = _getch();
  216. if (operation != '1' && operation != '2' && operation != '3' && operation != 27 /*esc*/) {
  217. //system("cls");
  218. cout << "ERROR: BAD ENTER, TRY AGAIN!\n";
  219. }
  220. switch (operation) {
  221. case '1' /*put*/:
  222. int number;
  223. cout << "Enter what u want to push in stack: ";
  224. cin >> number;
  225. intStack.push(number);
  226. cout << number << " pushed successfully\n";
  227. system("pause");
  228. break;
  229. case '2' /*take last*/:
  230. if (intStack.realSize)
  231. cout << intStack.pop() << " popped succesfully\n";
  232. else
  233. cout << "ERROR: Stack is empty\n";
  234. system("pause");
  235. break;
  236. case '3' /*print*/:
  237. cout << "STACK CONTENT: ";
  238. intStack.print();
  239. system("pause");
  240. break;
  241. }
  242. } while (operation != 27 /*ESC*/);
  243. system("cls");
  244. break;
  245. case '2' /*deque*/:
  246. system("cls");
  247. do {
  248. cout << "What do u want to do with ur deque?\n" << "[1] - push back\n" << "[2] - push front\n" << "[3] - pop back\n" << "[4] - pop front\n" << "[5] - display content\n" << "[ESC] - back\n";
  249. operation = _getch();
  250. if (operation != '1' && operation != '2' && operation != '3' && operation != '4' && operation != '5' && operation != 27 /*esc*/) {
  251. //system("cls");
  252. cout << "ERROR: BAD ENTER, TRY AGAIN!\n";
  253. }
  254. switch (operation) {
  255. int number;
  256. case '1' /*push back*/:
  257. cout << "Enter what u want to push in ur deque back: ";
  258. cin >> number;
  259. deque.push_back(number);
  260. cout << number << " put successfully\n";
  261. system("pause");
  262. break;
  263. case '2' /*push front*/:
  264. cout << "Enter what u want to push in ur deque front: ";
  265. cin >> number;
  266. deque.push_front(number);
  267. cout << number << " put successfully\n";
  268. system("pause");
  269. break;
  270. case '3' /*pop back*/:
  271. if (deque.last)
  272. cout << deque.pop_back() << " popped successfully\n";
  273. else
  274. cout << "there is no last element\n";
  275. system("pause");
  276. break;
  277. case '4' /*pop front*/:
  278. if (deque.first)
  279. cout << deque.pop_front() << " popped successfully\n";
  280. else
  281. cout << "there is no front element\n";
  282. system("pause");
  283. break;
  284. case '5' /*print*/:
  285. cout << "DEQUE CONTENT: ";
  286. deque.print();
  287. system("pause");
  288. break;
  289. }
  290. } while (operation != 27 /*ESC*/);
  291. system("cls");
  292. break;
  293. case '3' /*postfix*/:
  294. system("cls");
  295. char ch = 0;
  296. short bracketLvl = 0;
  297. bool prevIsDigit = false, prevIsOp = true, prevIsLeftBracket = false, prevIsRightBracket = false, prevIsFactorial = false;
  298. cout << "Press ESC to exit or enter algebraic expression using: \ndigits \n( ) \n! \n+ \n- \n* \n/ \n^ (power) \nsin \ncos \narcsin \narccos \ntg \nctg\n" << "Entry field: ";
  299. while (ch != 13 && ch != -1 && ch != 27) {
  300. ch = _getch();
  301. if (ch == 's' && (prevIsOp || prevIsLeftBracket)) {
  302. cout << ch;
  303. ch = _getch();
  304. if (ch == 'i') {
  305. cout << ch;
  306. ch = _getch();
  307. if (ch == 'n') {
  308. cout << ch;
  309. operationsStack.push('s');
  310. }
  311. else {
  312. cout << "\nERROR: bad sin enter\n";
  313. system("pause");
  314. ch = -1;
  315. }
  316. }
  317. else {
  318. cout << "\nERROR: bad sin enter\n";
  319. system("pause");
  320. ch = -1;
  321. }
  322. }
  323. if (ch == 't' && (prevIsOp || prevIsLeftBracket)) {
  324. cout << ch;
  325. ch = _getch();
  326. if (ch == 'g') {
  327. cout << ch;
  328. operationsStack.push('T');
  329. }
  330. else {
  331. cout << "\nERROR: bad tg enter\n";
  332. system("pause");
  333. ch = -1;
  334. }
  335. }
  336. if (ch == 'c' && (prevIsOp || prevIsLeftBracket)) {
  337. cout << ch;
  338. ch = _getch();
  339. switch (ch) {
  340. case 'o':
  341. cout << ch;
  342. ch = _getch();
  343. if (ch == 's') {
  344. cout << ch;
  345. operationsStack.push('c');
  346. }
  347. else {
  348. cout << "\nERROR: bad cos enter\n";
  349. system("pause");
  350. ch = -1;
  351. }
  352. break;
  353. case 't':
  354. cout << ch;
  355. ch = _getch();
  356. if (ch == 'g') {
  357. cout << ch;
  358. operationsStack.push('G');
  359. }
  360. else {
  361. cout << "\nERROR: bad ctg enter\n";
  362. system("pause");
  363. ch = -1;
  364. }
  365. break;
  366. default:
  367. cout << "\nERROR: bad cos or ctr enter\n";
  368. system("pause");
  369. ch = -1;
  370. }
  371. }
  372. if (ch == 'a' && (prevIsOp || prevIsLeftBracket)) {
  373. cout << ch;
  374. ch = _getch();
  375. if (ch == 'r') {
  376. cout << ch;
  377. ch = _getch();
  378. if (ch == 'c') {
  379. cout << ch;
  380. ch = _getch();
  381. switch (ch) {
  382. case 'c':
  383. cout << ch;
  384. ch = _getch();
  385. if (ch == 'o') {
  386. cout << ch;
  387. ch = _getch();
  388. if (ch == 's') {
  389. cout << ch;
  390. operationsStack.push('C');
  391. }
  392. else {
  393. cout << "\nERROR: bad arccos enter\n";
  394. system("pause");
  395. ch = -1;
  396. }
  397. }
  398. else {
  399. cout << "\nERROR: bad arccos enter\n";
  400. system("pause");
  401. ch = -1;
  402. }
  403. break;
  404. case 's':
  405. cout << ch;
  406. ch = _getch();
  407. if (ch == 'i') {
  408. cout << ch;
  409. ch = _getch();
  410. if (ch == 'n') {
  411. cout << ch;
  412. operationsStack.push('S');
  413. }
  414. else {
  415. cout << "\nERROR: bad arcsin enter\n";
  416. system("pause");
  417. ch = -1;
  418. }
  419. }
  420. else {
  421. cout << "\nERROR: bad arcsin enter\n";
  422. system("pause");
  423. ch = -1;
  424. }
  425. break;
  426. default:
  427. cout << "\nERROR: bad arcsin or arccos enter\n";
  428. system("pause");
  429. ch = -1;
  430. }
  431. }
  432. else {
  433. cout << "\nERROR: bad arcsin or arccos enter\n";
  434. system("pause");
  435. ch = -1;
  436. }
  437. }
  438. else {
  439. cout << "\nERROR: bad arcsin or arccos enter\n";
  440. system("pause");
  441. ch = -1;
  442. }
  443. }
  444. if (ch == '!' && prevIsDigit) {
  445. prevIsFactorial = true;
  446. prevIsLeftBracket = prevIsRightBracket = prevIsOp = prevIsDigit = false;
  447. cout << ch;
  448. str += ch;
  449. }
  450. if ((ch >= '0' && ch <= '9') && (prevIsOp || prevIsLeftBracket || prevIsDigit)) {
  451. digits = true;
  452. prevIsDigit = true;
  453. prevIsLeftBracket = prevIsRightBracket = prevIsOp = prevIsFactorial = false;
  454. cout << ch;
  455. str += ch;
  456. }
  457. if ((ch == '+' || ch == '-' || ch == '/' || ch == '*' || ch == '^') && (prevIsDigit || prevIsRightBracket || prevIsFactorial)) {
  458. if (str[str.length() - 1] >= '0' && str[str.length() - 1] <= '9')
  459. str += ' ';
  460. prevIsOp = true;
  461. prevIsLeftBracket = prevIsRightBracket = prevIsDigit = prevIsFactorial = false;
  462. cout << ch;
  463. while (!operationsStack.is_empty() && !isPriorityHigher(ch, operationsStack.last())) {
  464. str += youtput(operationsStack.pop());
  465. }
  466. operationsStack.push(ch);
  467. }
  468. if (ch == '(' && (prevIsOp || prevIsLeftBracket)) {
  469. prevIsLeftBracket = true;
  470. prevIsRightBracket = prevIsOp = prevIsDigit = prevIsFactorial = false;
  471. cout << ch;
  472. bracketLvl++;
  473. operationsStack.push(ch);
  474. }
  475. if (ch == ')' && (prevIsDigit || prevIsRightBracket || prevIsFactorial) && bracketLvl > 0) {
  476. prevIsRightBracket = true;
  477. prevIsDigit = prevIsOp = prevIsLeftBracket = prevIsFactorial = false;
  478. cout << ch;
  479. bracketLvl--;
  480. while (operationsStack.last() != '(') {
  481. str += output(operationsStack.last());
  482. operationsStack.pop();
  483. }
  484. operationsStack.pop();
  485. }
  486. }
  487. if (!digits) {
  488. cout << "\nERROR: There is no numbers\n";
  489. } else
  490. if (bracketLvl > 0) {
  491. cout << "\nERROR with brackets\n";
  492. } else
  493. if (ch != -1 && ch != 27) {
  494. while (!operationsStack.is_empty()) {
  495. str += output(operationsStack.pop());
  496. }
  497. cout << "\nresult: " << str << endl;
  498. }
  499. system("pause");
  500. system("cls");
  501. break;
  502. }
  503. } while (task != 27 /*esc*/);
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement