traceonelast

Untitled

May 29th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1.  
  2. void comp2(string in, string out)
  3. {
  4.     ifstream vxod(in, ios::out | ios::binary);
  5.     ofstream vixod(out);
  6.     string w;
  7.     char s;
  8.     int dictSize = 256;
  9.     bool byte[8];
  10.     int code_size = 8;
  11.     int count = 0;
  12.     map<string, int> dictionary;
  13.     for (int i = 0; i < 256; i++)
  14.         dictionary[string(1, i)] = i;
  15.     s = vxod.get();
  16.     w = s;
  17.     char k = 0;
  18.     while (!vxod.eof())
  19.     {
  20.         if (z(dictSize - 1) > code_size) code_size++;
  21.         s = vxod.get();
  22.         if (dictionary.count(w + s)) w = w + s;
  23.         else
  24.         {                      
  25.             for (int i = 0; i < code_size; i++)
  26.             {
  27.                 byte[count] = dictionary[w] & (1 << code_size - 1 - i);
  28.                
  29.                 count++;
  30.                 if (count == 8)
  31.                 {
  32.                     for (int i = 0; i < 8; i++)
  33.                     {
  34.                         k += (byte[i] << 7 - i);
  35.                         byte[i] = 0;
  36.                     }
  37.                     count = 0;
  38.                     vixod << k;
  39.                     k = 0;
  40.                 }
  41.             }  
  42.             cout << w + s << "- " << dictSize << endl;
  43.             dictionary[w + s] = dictSize++;
  44.            
  45.             w = s;
  46.         }
  47.  
  48.     }
  49.     for (int i = 0; i < code_size; i++)
  50.     {
  51.         byte[count] = dictionary[w] & (1 << code_size - 1 - i);
  52.  
  53.         count++;
  54.         if (count == 8)
  55.         {
  56.             for (int i = 0; i < 8; i++)
  57.             {
  58.                 k += (byte[i] << 7 - i);
  59.                 byte[i] = 0;
  60.             }
  61.             vixod << k;
  62.             k = 0;
  63.             count = 0;
  64.         }
  65.     }
  66.     if(count!=8)
  67.         for (int i = 0; i < 8; i++)
  68.         {
  69.             k += (byte[i] << 7 - i);
  70.             byte[i] = 0;
  71.         }
  72.     k = 0;
  73.     count = 0;
  74.  
  75.     vxod.close();
  76.     vixod.close();
  77. }
  78.  
  79.  
  80. void decomp2(string in,string out)
  81.  
  82. {
  83.     ifstream vxod(in, ios::out | ios::binary);
  84.     ofstream vixod(out);
  85.     string symbol;
  86.     string str;
  87.     int dictSize = 256;
  88.     bool byte[8];
  89.     int code_size = 8;
  90.     int count = 0;
  91.     map<int, string> dictionary;
  92.     for (int i = 0; i < 256; i++)
  93.         dictionary[i] = string(1, i);
  94.     int od = 0;
  95.     int nk = 0;
  96.     char c = vxod.get();
  97.     for (int j = 0; j < code_size; j++)
  98.     {
  99.         byte[count] = c & (1 << 7 - count);
  100.        
  101.         if (byte[count]) od = (od<< 1) + 1;
  102.         else od = od << 1;
  103.         count++;
  104.         if (count == 8) {
  105.             count = 0; c = vxod.get();
  106.         }
  107.  
  108.     }
  109.     code_size++;
  110.     vixod << dictionary[od];
  111.     symbol = dictionary[od];
  112.     while (!vxod.eof())
  113.     {
  114.         nk = 0;
  115.  
  116.  
  117.         for (int j = 0; j < code_size; j++)
  118.         {
  119.             byte[count] = c & (1 << 7 - count);
  120.            
  121.             if (byte[count]) nk = (nk << 1) + 1;
  122.             else nk = nk << 1;
  123.             count++;
  124.             if (count == 8) {
  125.                 count = 0; c = vxod.get();
  126.             }
  127.  
  128.         }
  129.  
  130.        
  131.         if (!dictionary.count(nk))
  132.         {
  133.             str = dictionary[od]; str = str + symbol;
  134.         }
  135.         else str = dictionary[nk];
  136.         vixod << str;
  137.         symbol = str[0];
  138.         dictionary[dictSize++] = dictionary[od] + symbol;
  139.         od = nk;
  140.     }
  141.  
  142.  
  143.     vxod.close();
  144.     vixod.close();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment