kaamilp

Untitled

Mar 27th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include <string>
  5. #include <Windows.h>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. ofstream outStream;
  13. ifstream inStream;
  14. string temp;
  15. string output = "";
  16. string outputDec = "";
  17. char text[] = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
  18. char lookAheadBuffer[5] = "";
  19. char dictionary[9];
  20. char secondDictionary[9];
  21. int offset = 0, length, counter = 0;
  22.  
  23.  
  24. void updDict(int move, char* dictionary, char* lookAheadbufferLoc)
  25. {
  26.     int i;
  27.     int iter = move;
  28.     while (iter)
  29.     {
  30.         for (i = 0; i < 8; i++)
  31.         {
  32.             dictionary[i] = dictionary[i + 1];
  33.  
  34.         }
  35.         iter--;
  36.     }
  37.     i = 8 - move;
  38.     while (*lookAheadbufferLoc != NULL)
  39.     {
  40.         dictionary[i++] = *lookAheadbufferLoc;
  41.         lookAheadbufferLoc++;
  42.     }
  43. }
  44.  
  45. string compressor(char* text, int textLength)
  46. {
  47.     //fills dictionary with the first sign from the buffer
  48.     for (int i = 0; i < size(dictionary); ++i)
  49.     {
  50.         dictionary[i] = text[0];
  51.         secondDictionary[i] = text[0]; //for decompression
  52.     }
  53.     dictionary[8] = NULL;
  54.     secondDictionary[8] = NULL;
  55.     cout << "2. The initial dictionary is filled with: " << dictionary << endl;
  56.  
  57.     cout << "3. The compressed text: ";
  58.     while (counter < textLength) //in this example 20
  59.     {
  60.         for (int i = 0; i < 4; i++) // 4 is a buffor length
  61.         {
  62.             lookAheadBuffer[i] = text[i + counter];
  63.         }
  64.  
  65.         for (int i = 5; i > 0; i--)
  66.         {
  67.             string dict = dictionary;
  68.             lookAheadBuffer[i - 1] = NULL;
  69.             if (strlen(lookAheadBuffer) < 4)
  70.             {
  71.                 i = strlen(lookAheadBuffer) + 1;
  72.             }
  73.             int pos = dict.rfind(lookAheadBuffer); //looking for the longest substring
  74.             if (pos != string::npos)
  75.             {
  76.                 output += "0"; //hit missed
  77.                 offset = size(dictionary) - 2 - pos;
  78.                 output += ((char)offset) + 48;
  79.                 output += (char)(i - 1) + 48; //length of a substring
  80.                 counter += (i - 1);
  81.                 updDict(i - 1, dictionary, lookAheadBuffer);
  82.  
  83.                 break;
  84.             }
  85.             if (i == 2)
  86.             {
  87.                 output += "1"; //hit
  88.                 output += lookAheadBuffer;
  89.                 counter += 1;
  90.                 updDict(1, dictionary, lookAheadBuffer);
  91.                 break;
  92.             }
  93.         }
  94.     }
  95.     return output;
  96. }
  97.  
  98. string decompressor(char* text, int outputLength)
  99. {
  100.     cout << "5. The decompressed text: ";
  101.     string Dict = secondDictionary;
  102.     counter = 0;
  103.  
  104.     while (counter < outputLength)
  105.     {
  106.         if (text[counter] == '0')
  107.         {
  108.             length = text[counter + 2] - 48;
  109.             offset = text[counter + 1] - 48;
  110.             char what[9];
  111.             int i;
  112.             for (i = 0; i < length; i++)
  113.             {
  114.                 what[i] = secondDictionary[7 - offset + i];
  115.             }
  116.             what[i] = NULL;
  117.  
  118.             updDict(length, secondDictionary, what);
  119.  
  120.             for (i = 0; i < length; i++)
  121.             {
  122.                 outputDec += what[i];
  123.             }
  124.             counter += 3;
  125.         }
  126.         if (text[counter] == '1')
  127.         {
  128.             offset = 0;
  129.             length = 1;
  130.             outputDec += text[counter + 1];
  131.  
  132.             char what[2];
  133.             what[0] = text[counter + 1]; //as the text sign is a new letter
  134.             what[1] = NULL;
  135.             counter += 2;
  136.             updDict(length, secondDictionary, what);
  137.         }
  138.     }
  139.     return outputDec;
  140. }
  141.  
  142. void writeToFile()
  143. {
  144.     outStream.open("C:\\Users\\Kamil\\Desktop\\compression.txt");
  145.     if (outStream.is_open())
  146.     {
  147.         cout << "\t-Compressed text successfully saved to a file... " << endl;
  148.         outStream << output;
  149.         outStream.close();
  150.     }
  151.     else
  152.     {
  153.         cout << "\t-Couldn't save compressed text to a file... " << endl;
  154.     }
  155. }
  156.  
  157. void readFromFile()
  158. {
  159.     inStream.open("C:\\Users\\Kamil\\Desktop\\compression.txt");
  160.     if (inStream.is_open())
  161.     {
  162.         cout << "\t-Data for decompression read successfully..." << endl;
  163.  
  164.         int i = 0;
  165.         while (!inStream.eof())
  166.         {
  167.             inStream.get(text[i]);
  168.             ++i;
  169.         }
  170.         inStream.close();
  171.     }
  172.     else
  173.     {
  174.         cout << "\t-Couldn't open file for decompression... " << endl;
  175.     }
  176. }
  177.  
  178. int main()
  179. {
  180.     cout << "===========================================================================================" << endl;
  181.     cout << "\t\t\t\tData compression algorithms" << endl;
  182.     cout << "===========================================================================================" << endl;
  183.     temp = text;
  184.     int textLength = temp.size();
  185.     cout << "1. Text we are trying to compress: " << text << ", and it's length is: " << textLength << endl;
  186.     cout << compressor(text, textLength) << endl;
  187.     writeToFile(); //saves compressed text
  188.     readFromFile(); //reads data for decompression
  189.     temp = text;
  190.     int outputLength = temp.size();
  191.     cout << "4. Text we are trying to decompress: " << text << ", and it's length is: " << outputLength << endl;
  192.     cout << decompressor(text, outputLength) << endl << endl;
  193.     system("pause");
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment