Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <string>
- #include <math.h>
- #include <string>
- #include <Windows.h>
- #include <algorithm>
- #include <iterator>
- #include <fstream>
- using namespace std;
- ofstream outStream;
- ifstream inStream;
- string temp;
- string output = "";
- string outputDec = "";
- char text[] = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
- char lookAheadBuffer[5] = "";
- char dictionary[9];
- char secondDictionary[9];
- int offset = 0, length, counter = 0;
- void updDict(int move, char* dictionary, char* lookAheadbufferLoc)
- {
- int i;
- int iter = move;
- while (iter)
- {
- for (i = 0; i < 8; i++)
- {
- dictionary[i] = dictionary[i + 1];
- }
- iter--;
- }
- i = 8 - move;
- while (*lookAheadbufferLoc != NULL)
- {
- dictionary[i++] = *lookAheadbufferLoc;
- lookAheadbufferLoc++;
- }
- }
- string compressor(char* text, int textLength)
- {
- //fills dictionary with the first sign from the buffer
- for (int i = 0; i < size(dictionary); ++i)
- {
- dictionary[i] = text[0];
- secondDictionary[i] = text[0]; //for decompression
- }
- dictionary[8] = NULL;
- secondDictionary[8] = NULL;
- cout << "2. The initial dictionary is filled with: " << dictionary << endl;
- cout << "3. The compressed text: ";
- while (counter < textLength) //in this example 20
- {
- for (int i = 0; i < 4; i++) // 4 is a buffor length
- {
- lookAheadBuffer[i] = text[i + counter];
- }
- for (int i = 5; i > 0; i--)
- {
- string dict = dictionary;
- lookAheadBuffer[i - 1] = NULL;
- if (strlen(lookAheadBuffer) < 4)
- {
- i = strlen(lookAheadBuffer) + 1;
- }
- int pos = dict.rfind(lookAheadBuffer); //looking for the longest substring
- if (pos != string::npos)
- {
- output += "0"; //hit missed
- offset = size(dictionary) - 2 - pos;
- output += ((char)offset) + 48;
- output += (char)(i - 1) + 48; //length of a substring
- counter += (i - 1);
- updDict(i - 1, dictionary, lookAheadBuffer);
- break;
- }
- if (i == 2)
- {
- output += "1"; //hit
- output += lookAheadBuffer;
- counter += 1;
- updDict(1, dictionary, lookAheadBuffer);
- break;
- }
- }
- }
- return output;
- }
- string decompressor(char* text, int outputLength)
- {
- cout << "5. The decompressed text: ";
- string Dict = secondDictionary;
- counter = 0;
- while (counter < outputLength)
- {
- if (text[counter] == '0')
- {
- length = text[counter + 2] - 48;
- offset = text[counter + 1] - 48;
- char what[9];
- int i;
- for (i = 0; i < length; i++)
- {
- what[i] = secondDictionary[7 - offset + i];
- }
- what[i] = NULL;
- updDict(length, secondDictionary, what);
- for (i = 0; i < length; i++)
- {
- outputDec += what[i];
- }
- counter += 3;
- }
- if (text[counter] == '1')
- {
- offset = 0;
- length = 1;
- outputDec += text[counter + 1];
- char what[2];
- what[0] = text[counter + 1]; //as the text sign is a new letter
- what[1] = NULL;
- counter += 2;
- updDict(length, secondDictionary, what);
- }
- }
- return outputDec;
- }
- void writeToFile()
- {
- outStream.open("C:\\Users\\Kamil\\Desktop\\compression.txt");
- if (outStream.is_open())
- {
- cout << "\t-Compressed text successfully saved to a file... " << endl;
- outStream << output;
- outStream.close();
- }
- else
- {
- cout << "\t-Couldn't save compressed text to a file... " << endl;
- }
- }
- void readFromFile()
- {
- inStream.open("C:\\Users\\Kamil\\Desktop\\compression.txt");
- if (inStream.is_open())
- {
- cout << "\t-Data for decompression read successfully..." << endl;
- int i = 0;
- while (!inStream.eof())
- {
- inStream.get(text[i]);
- ++i;
- }
- inStream.close();
- }
- else
- {
- cout << "\t-Couldn't open file for decompression... " << endl;
- }
- }
- int main()
- {
- cout << "===========================================================================================" << endl;
- cout << "\t\t\t\tData compression algorithms" << endl;
- cout << "===========================================================================================" << endl;
- temp = text;
- int textLength = temp.size();
- cout << "1. Text we are trying to compress: " << text << ", and it's length is: " << textLength << endl;
- cout << compressor(text, textLength) << endl;
- writeToFile(); //saves compressed text
- readFromFile(); //reads data for decompression
- temp = text;
- int outputLength = temp.size();
- cout << "4. Text we are trying to decompress: " << text << ", and it's length is: " << outputLength << endl;
- cout << decompressor(text, outputLength) << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment