Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #pragma region Stack Implementaton
- template <class T>
- class Node
- {
- public:
- T data;
- Node<T>* next;
- };
- template<class T>
- class Stack
- {
- private:
- Node<T>* top;
- int count;
- public:
- Stack()
- {
- top = nullptr;
- count = 0;
- }
- void push(T element)
- {
- Node<T>* newTop = new Node<T>;
- if (top == nullptr)
- {
- newTop->data = element;
- newTop->next = nullptr;
- top = newTop;
- count++;
- }
- else
- {
- newTop->data = element;
- newTop->next = top;
- top = newTop;
- count++;
- }
- }
- int getSize() const
- {
- return count;
- }
- T pop()
- {
- if (top == nullptr) std::cout << "The stack is empty!" << std::endl;
- else {
- char returnedValue = top->data;
- Node<T>* popped = top;
- top = top->next;
- count--;
- delete popped;
- return returnedValue;
- }
- }
- bool isEmpty() const
- {
- return top == nullptr;
- }
- T peek() const
- {
- return top->data;
- }
- void print() const
- {
- Node<T>* temp;
- temp = top;
- while (temp != nullptr)
- {
- std::cout << temp->data;
- temp = temp->next;
- }
- std::cout << "\n" << std::endl;
- }
- };
- #pragma endregion
- bool isDigit(char element)
- {
- bool flag = false;
- if ((int)element >= 48 && (int)element <= 57) flag = true;
- return flag;
- }
- bool isSlashed(Stack<char>& stack)
- {
- char last = stack.pop();
- if (stack.peek() == '\\')
- {
- stack.push(last);
- return true;
- }
- else
- {
- stack.push(last);
- return false;
- }
- }
- bool isLegit(Stack<char> stack)
- {
- int leftBracket = 0;
- int rightBracket = 0;
- int quote = 0;
- char fe = '//';
- char nextEl;
- while (!stack.isEmpty())
- {
- char peek = stack.peek();
- if (peek == (char)'(') leftBracket++;
- if (peek == (char)')') rightBracket++;
- if (peek == (char)'\"') quote++;
- stack.pop();
- if (stack.getSize() > 0)
- nextEl = stack.peek();
- if (nextEl == (char)'\\' && peek == (char)'(') leftBracket--;
- if (nextEl == (char)'\\' && peek == (char)')') rightBracket--;
- if (nextEl == (char)'\\' && peek == (char)'\"') quote--;
- if (leftBracket > rightBracket) return false;
- //fe = stack.peek();
- }
- return (leftBracket == rightBracket && quote % 2 == 0);
- }
- int errorResult(Stack<char>& p)
- {
- std::cout << "The input is not legit!" << std::endl;
- return -1;
- }
- int howTimes(Stack<char>& p)
- {
- if (p.isEmpty())
- return errorResult(p);
- int number = p.pop() - '0';
- if (!p.isEmpty() && p.peek() == '\\')
- return errorResult(p);
- int i;
- if (number >= 0 && number <= 9)
- i = 1;
- else
- return errorResult(p);
- if (p.getSize() != 0)
- while (isDigit(p.peek()))
- {
- char elem = p.pop();
- int elemDig = elem - '0';
- int helperNumber = elemDig * pow(10, i);
- number = number + helperNumber;
- i++;
- }
- return number;
- }
- int main()
- {
- Stack<char>* p = new Stack<char>();
- Stack<char>* result = new Stack<char>();
- Stack<char>* repeatedChars = new Stack<char>();
- Stack<char>* breckets = new Stack<char>();
- Stack<char>* isLegitS = new Stack<char>();
- Stack<char>* currRep = new Stack<char>();
- std::string input;
- std::string file1, file2;
- std::cout << "Enter compressed file: ";
- std::cin >> file1;
- std::cout << "Enter decompressed file: ";
- std::cin >> file2;
- std::ifstream compressedFile;
- compressedFile.open(file1);
- compressedFile >> input;
- compressedFile.close();
- for (size_t i = 0; i < input.length(); i++)
- {
- p->push(input[i]);
- isLegitS->push(input[i]);
- }
- if (!isLegit(*isLegitS))
- {
- std::cout << "The input is not legit!" << std::endl;
- return 0;
- }
- while (!p->isEmpty())
- {
- if (p->peek() == ')')
- {
- char element = p->pop();
- breckets->push(element);
- while (p->peek() != '(')
- {
- if (isSlashed(*p))
- {
- char el = p->pop();
- repeatedChars->push(el);
- repeatedChars->push('\\');
- }
- if (p->isEmpty()) {
- std::cout << "The input is not legit!" << std::endl;
- return 0;
- }
- //char m = p->peek();
- if (p->peek() == '\"')
- {
- char s = p->pop();
- repeatedChars->push(s);
- while (p->peek() != '\"')
- {
- char pChar = p->pop();
- repeatedChars->push(pChar);
- if (p->peek() == '\"')
- {
- if (isSlashed(*p))
- {
- repeatedChars->push('\"');
- repeatedChars->push('\\');
- p->pop();
- p->pop();
- }
- }
- }
- p->pop();
- repeatedChars->push(s);
- continue;
- }
- if (!p->isEmpty() && p->peek() == ')')
- {
- char el = p->pop();
- breckets->push(el);
- while (p->peek() != '(')
- {
- char pChar = p->pop();
- currRep->push(pChar);
- }
- }
- else
- {
- char el = p->pop();
- repeatedChars->push(el);
- }
- }// neasted while
- p->pop();
- breckets->pop();
- int number = howTimes(*p);
- if (number == -1) return 0;
- std::string symbols;
- if (!p->isEmpty() && !currRep->isEmpty())
- {
- while (!currRep->isEmpty())
- {
- symbols += currRep->pop();
- }
- }
- else
- {
- while (!repeatedChars->isEmpty())
- {
- symbols += repeatedChars->pop();
- }
- }
- int count = symbols.size();
- for (int j = 0; j < number; j++)
- {
- for (int f = count - 1; f >= 0; f--)
- {
- char eleme = symbols[f];
- repeatedChars->push(eleme);
- }
- }
- //if (p->peek() == ')') continue;
- while (!repeatedChars->isEmpty())
- p->push(repeatedChars->pop());
- if (!breckets->isEmpty())
- {
- while (!breckets->isEmpty()) p->push(breckets->pop());
- }
- }
- else if (p->peek() == '\"')
- {
- if (isSlashed(*p))
- {
- char f = p->pop();
- char sh = p->pop();
- result->push(f);
- result->push(sh);
- if (p->getSize() == 0) break;
- //p->pop();
- //p->pop();
- continue;
- }
- char s = p->pop();
- while (isSlashed(*p))
- {
- char first = p->pop();
- char slash = p->pop();
- result->push(first);
- result->push(slash);
- }
- if (p->peek() == '\\')
- {
- result->push(s);
- result->push('\\');
- }
- else
- {
- while (p->peek() != '\"') //|| p->peek() != (char)"\\"
- {
- char element = p->pop();
- if (p->peek() == '\"')
- {
- char curr = p->pop();
- if (!p->isEmpty() && p->peek() == '\\')
- {
- result->push(element);
- result->push(curr);
- result->push('\\');
- p->pop();
- }
- else
- {
- result->push(element);
- p->push(curr);
- }
- }
- else
- {
- if (element == '\"')
- p->push('\"');
- else
- result->push(element);
- }
- }
- }
- p->pop();
- }
- else if (p->peek() == '\\')
- {
- p->pop();
- if (p->peek() == '\\')
- {
- result->push(p->pop());
- }
- }
- else if (p->getSize() != 1 && isSlashed(*p))
- {
- char element = p->pop();
- result->push(element);
- }
- else
- {
- char element = p->pop();
- result->push(element);
- }
- }
- result->print();
- std::ofstream decompressedFile(file2, std::ios::trunc);
- while (!result->isEmpty()) {
- decompressedFile << result->pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement