Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #pragma region Stack Implementaton
  4. template <class T>
  5. class Node
  6. {
  7. public:
  8. T data;
  9. Node<T>* next;
  10. };
  11.  
  12. template<class T>
  13. class Stack
  14. {
  15. private:
  16. Node<T>* top;
  17. int count;
  18. public:
  19. Stack()
  20. {
  21. top = nullptr;
  22. count = 0;
  23. }
  24.  
  25. void push(T element)
  26. {
  27. Node<T>* newTop = new Node<T>;
  28. if (top == nullptr)
  29. {
  30. newTop->data = element;
  31. newTop->next = nullptr;
  32. top = newTop;
  33. count++;
  34. }
  35. else
  36. {
  37. newTop->data = element;
  38. newTop->next = top;
  39. top = newTop;
  40. count++;
  41. }
  42. }
  43.  
  44. int getSize() const
  45. {
  46. return count;
  47. }
  48.  
  49. T pop()
  50. {
  51. if (top == nullptr) std::cout << "The stack is empty!" << std::endl;
  52. else {
  53. char returnedValue = top->data;
  54. Node<T>* popped = top;
  55. top = top->next;
  56. count--;
  57. delete popped;
  58. return returnedValue;
  59. }
  60. }
  61.  
  62. bool isEmpty() const
  63. {
  64. return top == nullptr;
  65. }
  66.  
  67. T peek() const
  68. {
  69. return top->data;
  70. }
  71.  
  72. void print() const
  73. {
  74. Node<T>* temp;
  75. temp = top;
  76. while (temp != nullptr)
  77. {
  78. std::cout << temp->data;
  79. temp = temp->next;
  80. }
  81. std::cout << "\n" << std::endl;
  82. }
  83. };
  84.  
  85. #pragma endregion
  86.  
  87. bool isDigit(char element)
  88. {
  89. bool flag = false;
  90. if ((int)element >= 48 && (int)element <= 57) flag = true;
  91.  
  92. return flag;
  93. }
  94.  
  95. bool isSlashed(Stack<char>& stack)
  96. {
  97. char last = stack.pop();
  98. if (stack.peek() == '\\')
  99. {
  100. stack.push(last);
  101. return true;
  102. }
  103. else
  104. {
  105. stack.push(last);
  106. return false;
  107. }
  108. }
  109.  
  110. bool isLegit(Stack<char> stack)
  111. {
  112. int leftBracket = 0;
  113. int rightBracket = 0;
  114. int quote = 0;
  115. char fe = '//';
  116. char nextEl;
  117. while (!stack.isEmpty())
  118. {
  119. char peek = stack.peek();
  120. if (peek == (char)'(') leftBracket++;
  121. if (peek == (char)')') rightBracket++;
  122. if (peek == (char)'\"') quote++;
  123. stack.pop();
  124. if (stack.getSize() > 0)
  125. nextEl = stack.peek();
  126. if (nextEl == (char)'\\' && peek == (char)'(') leftBracket--;
  127. if (nextEl == (char)'\\' && peek == (char)')') rightBracket--;
  128. if (nextEl == (char)'\\' && peek == (char)'\"') quote--;
  129.  
  130. if (leftBracket > rightBracket) return false;
  131. //fe = stack.peek();
  132. }
  133.  
  134. return (leftBracket == rightBracket && quote % 2 == 0);
  135. }
  136.  
  137. int errorResult(Stack<char>& p)
  138. {
  139. std::cout << "The input is not legit!" << std::endl;
  140. return -1;
  141. }
  142.  
  143. int howTimes(Stack<char>& p)
  144. {
  145. if (p.isEmpty())
  146. return errorResult(p);
  147.  
  148. int number = p.pop() - '0';
  149. if (!p.isEmpty() && p.peek() == '\\')
  150. return errorResult(p);
  151. int i;
  152. if (number >= 0 && number <= 9)
  153. i = 1;
  154. else
  155. return errorResult(p);
  156.  
  157. if (p.getSize() != 0)
  158. while (isDigit(p.peek()))
  159. {
  160. char elem = p.pop();
  161. int elemDig = elem - '0';
  162. int helperNumber = elemDig * pow(10, i);
  163.  
  164. number = number + helperNumber;
  165. i++;
  166. }
  167.  
  168. return number;
  169. }
  170.  
  171. int main()
  172. {
  173. Stack<char>* p = new Stack<char>();
  174. Stack<char>* result = new Stack<char>();
  175. Stack<char>* repeatedChars = new Stack<char>();
  176. Stack<char>* breckets = new Stack<char>();
  177. Stack<char>* isLegitS = new Stack<char>();
  178. Stack<char>* currRep = new Stack<char>();
  179.  
  180. char input[80];
  181. std::cin >> input;
  182.  
  183. for (size_t i = 0; i < strlen(input); i++)
  184. {
  185. p->push(input[i]);
  186. isLegitS->push(input[i]);
  187. }
  188.  
  189. if (!isLegit(*isLegitS))
  190. {
  191. std::cout << "The input is not legit!" << std::endl;
  192. return 0;
  193. }
  194.  
  195. while (!p->isEmpty())
  196. {
  197. char element = p->peek();
  198. if (p->peek() == ')')
  199. {
  200. char element = p->pop();
  201. breckets->push(element);
  202. while (p->peek() != '(')
  203. {
  204. if (isSlashed(*p))
  205. {
  206. char el = p->pop();
  207. repeatedChars->push(el);
  208. repeatedChars->push('\\');
  209. }
  210.  
  211. //char m = p->peek();
  212. if (p->peek() == '\"')
  213. {
  214. char s =p->pop();
  215.  
  216. repeatedChars->push(s);
  217.  
  218. while (p->peek() != '\"')
  219. {
  220. char pChar = p->pop();
  221.  
  222. repeatedChars->push(pChar);
  223.  
  224. if (p->peek() == '\"')
  225. {
  226. if (isSlashed(*p))
  227. {
  228. repeatedChars->push('\"');
  229. repeatedChars->push('\\');
  230. p->pop();
  231. p->pop();
  232. }
  233. }
  234. }
  235.  
  236. p->pop();
  237. repeatedChars->push(s);
  238. continue;
  239. }
  240.  
  241.  
  242. if (!p->isEmpty() && p->peek() == ')')
  243. {
  244. char el = p->pop();
  245. breckets->push(el);
  246. while (p->peek() != '(')
  247. {
  248. char pChar = p->pop();
  249. currRep->push(pChar);
  250. }
  251. }
  252.  
  253. else
  254. {
  255. char el = p->pop();
  256. repeatedChars->push(el);
  257. }
  258. }// neasted while
  259. p->pop();
  260. breckets->pop();
  261.  
  262. int number = howTimes(*p);
  263. if (number == -1) return 0;
  264.  
  265.  
  266. std::string symbols;
  267. if (!p->isEmpty() && !currRep->isEmpty())
  268. {
  269. while (!currRep->isEmpty())
  270. {
  271. symbols += currRep->pop();
  272. }
  273. }
  274. else
  275. {
  276. while (!repeatedChars->isEmpty())
  277. {
  278. symbols += repeatedChars->pop();
  279. }
  280. }
  281.  
  282. int count = symbols.size();
  283.  
  284. for (int j = 0; j < number; j++)
  285. {
  286. for (int f = count - 1; f >= 0; f--)
  287. {
  288. char eleme = symbols[f];
  289. repeatedChars->push(eleme);
  290. }
  291. }
  292.  
  293. //if (p->peek() == ')') continue;
  294.  
  295. while (!repeatedChars->isEmpty())
  296. p->push(repeatedChars->pop());
  297.  
  298. if (!breckets->isEmpty())
  299. {
  300. while (!breckets->isEmpty()) p->push(breckets->pop());
  301. }
  302. }
  303.  
  304. else if (p->peek() == '\"')
  305. {
  306. if (isSlashed(*p))
  307. {
  308. char f = p->pop();
  309. char sh = p->pop();
  310. result->push(f);
  311. result->push(sh);
  312. if (p->getSize() == 0) break;
  313.  
  314. //p->pop();
  315. //p->pop();
  316.  
  317. continue;
  318. }
  319. char s = p->pop();
  320.  
  321. while (isSlashed(*p))
  322. {
  323. char first = p->pop();
  324. char slash = p->pop();
  325. result->push(first);
  326. result->push(slash);
  327. }
  328.  
  329. if (p->peek() == '\\')
  330. {
  331. result->push(s);
  332. result->push('\\');
  333. }
  334. else
  335. {
  336. while (p->peek() != '\"') //|| p->peek() != (char)"\\"
  337. {
  338. char element = p->pop();
  339.  
  340. if (p->peek() == '\"')
  341. {
  342. char curr = p->pop();
  343. if (!p->isEmpty() && p->peek() == '\\')
  344. {
  345. result->push(element);
  346. result->push(curr);
  347. result->push('\\');
  348. p->pop();
  349. }
  350. else
  351. {
  352. result->push(element);
  353. p->push(curr);
  354. }
  355. }
  356. else
  357. {
  358. if (element == '\"')
  359. p->push('\"');
  360. else
  361. result->push(element);
  362. }
  363. }
  364. }
  365. p->pop();
  366. }
  367.  
  368. else if (p->peek() == '\\')
  369. {
  370. p->pop();
  371. if (p->peek() == '\\')
  372. {
  373. result->push(p->pop());
  374. }
  375. }
  376. else
  377. {
  378. char element = p->pop();
  379. result->push(element);
  380. }
  381.  
  382. //while (!repeatedChars->isEmpty())
  383. //{
  384. //p->push(repeatedChars->pop());
  385. //}
  386. }
  387.  
  388. result->print();
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement