/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node
{
char letter;
string bit;
string word;
node* next;
};
class S // only purpose of the stack is to fill the child threads correctly
{
public:
node* top;
S()
{
top == NULL;
}
void push(char L, string B)
{
node* temp = new node;
temp->letter = L;
temp->bit = B;
if (top == NULL)
{
top = temp;
temp->next = NULL;
}
else
{
temp->next = top;
top = temp;
}
}
void pop()
{
if (top != NULL)
{
node* temp = top;
top = top->next;
delete temp;
}
}
int S_size()
{
int size = 0;
node* curr = top;
while (curr != NULL)
{
size++;
curr = curr->next;
}
return size;
}
void print()
{
node* curr = top;
while (curr != NULL)
{
if (curr->letter == '\n')
{
cout << "<EOL>" << " Binary code = " << curr->bit << endl;
curr = curr->next;
}
else
{
cout << curr->letter << " Binary code = " << curr->bit << endl;
curr = curr->next;
}
}
return;
}
void decompress()
{
node* curr = top;
string m1 = "";
while (curr != NULL)
{
if(curr == NULL)
{
return;
}
int j = 0;
for (int i = 0; i < curr->bit.length(); i++)
{
if (curr->bit[i] == '1')
{
curr->word += curr->letter;
}
else if (curr->bit[i] == '0')
{
curr->word += m1[j];
j++;
}
}
m1 = curr->word;
curr = curr->next;
}
for (int i = 0; i < m1.length();i++)
{
if (m1[i] == '\n')
{
cout << "<EOL>";
}
else
{
cout << m1[i];
}
}
}
};
int main()
{
S o;
string word;
int s;
char a;
string b;
ifstream infile;
infile.open("input.txt");
while (getline(infile, word)/*!infile.eof()*/)
{
if (word != "") {
if (word.substr(0, 5) == "<EOL>")
{
a = '\n';
b = word.substr(6, word.length());
}
else
{
a = word[0];
b = word.substr(2, word.length());
}
//cout << a << ":" << b << endl;
}
//cout << a << ":" << b << endl;
o.push(a, b);
}
//s = o.S_size();
//cout << "size: " << s << endl;
o.print();
cout << "Decompressed file contents: " << endl;
o.decompress();
system("PAUSE");
return 0;
}