Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <bitset>
  6.  
  7. using namespace std;
  8.  
  9. const int encLength = 1;
  10. const int dictSize = 8;
  11. const int bufferSize = 8;
  12.  
  13. //string sampleString = "abracadabra";
  14. string dictionary = "aaaaaaaa";
  15.  
  16. struct compressingInfo{
  17.     int length;
  18.     int offset;
  19.     bool found;
  20.     string off;
  21. };
  22.  
  23. static vector <compressingInfo> output;
  24.  
  25. string readFile(){
  26.     string input;
  27.     ifstream myFile ("example.txt");
  28.  
  29.     if (myFile.is_open()) {
  30.         while ( getline (myFile,input) ) {
  31.             cout << input << '\n';
  32.          }
  33.      myFile.close();
  34.     } else {
  35.       cout << "Unable to open file";
  36.       input = "abracadabra";
  37.     }
  38.  
  39.     return input;
  40. }
  41.  
  42. void showOutput(){
  43.     if(encLength <= 1){
  44.         for(int i = 0; i < output.size(); i++){
  45.             if(output[i].found == true){
  46.                 cout << "Found: " << output[i].found << " Offset: " << output[i].off << endl;
  47.             } else {
  48.             cout << "Found: " << output[i].found << " Offset: " << output[i].offset << " Length: " << output[i].length << endl;
  49.             }
  50.         }
  51.     } else {
  52.         for(int i = 0; i < output.size(); i++){
  53.             if(output[i].found == true){
  54.                 cout << "Found: " << output[i].found << " Offset: " << output[i].off << " Length: " << output[i].length << endl;
  55.             } else {
  56.             cout << "Found: " << output[i].found << " Offset: " << output[i].offset << " Length: " << output[i].length << endl;
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. void bitForm(){
  63.     for (int i = 0; i < output.size(); i++) {
  64.         if (output[i].found == true) {
  65.             string match = std::bitset<1>(output[i].found).to_string();
  66.             string character = std::bitset<8>(output[i].off.c_str()[0]).to_string();
  67.             cout << match << " " << character << " ";
  68.         } else {
  69.             string match = std::bitset<1>(output[i].found).to_string();
  70.             string offset = std::bitset<8>(output[i].offset).to_string();
  71.             string length = std::bitset<3>(output[i].length).to_string();
  72.             cout << match << " " << offset << " " << length << " ";
  73.         }
  74.     }
  75.     cout << endl;
  76. }
  77.  
  78. void decompress(){
  79.     cout << " ==== DECOMPRESSING ====" << endl;
  80.     string outputString = "";
  81.     string chain = dictionary;
  82.  
  83.     for(int i = 0 ; i < output.size(); i++) {
  84.         if(output[i].found == true) {
  85.             outputString += output[i].off;
  86.             chain += output[i].off;
  87.         } else {
  88.             outputString += chain.substr(output[i].offset, output[i].length);
  89.             chain += chain.substr(output[i].offset, output[i].length);
  90.         }
  91.         chain = chain.substr(chain.length() - dictSize);
  92.     }
  93.  
  94.     cout << outputString << endl;
  95. }
  96.  
  97. void compress(){
  98.     cout << "==== COMPRESSING ====" << endl;
  99.     string sampleString = readFile();
  100.     string chain = dictionary + sampleString;
  101.  
  102.     for(int i = 0; i < sampleString.length();) {
  103.         string toSearch = chain.substr(i, dictSize);
  104.         string buffer = sampleString.substr(i, bufferSize);
  105.  
  106.         while(toSearch.find(buffer) == string::npos && buffer.length() > encLength) {
  107.             buffer = buffer.substr(0, buffer.length()-1);
  108.         }
  109.  
  110.         if(toSearch.find(buffer) == string::npos) {
  111.             compressingInfo obj;
  112.             obj.found = true;
  113.             obj.off = buffer;
  114.             obj.offset = toSearch.find(buffer);
  115.             obj.length = buffer.length();
  116.             output.push_back(obj);
  117.         } else {
  118.             compressingInfo obj;
  119.             obj.found = false;
  120.             obj.off = buffer;
  121.             obj.offset = toSearch.find(buffer);
  122.             obj.length = buffer.length();
  123.             output.push_back(obj);
  124.         }
  125.         i += buffer.length();
  126.     }
  127.     showOutput();
  128.     bitForm();
  129.     decompress();
  130. }
  131.  
  132. int main(){
  133.     compress();
  134.     system("PAUSE");
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement