Advertisement
Guest User

Untitled

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