Advertisement
traceonelast

Untitled

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