Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void comp2(string in, string out)
- {
- int ct = 0;
- ifstream vxod(in, ios::out | ios::binary);
- ofstream vixod(out);
- vixod.seekp(sizeof(int));
- string w;
- char s;
- int dictSize = 256;
- bool byte[8];
- int code_size = 8;
- int count = 0;
- map<string, int> dictionary;
- for (int i = 0; i < 256; i++)
- dictionary[string(1, i)] = i;
- s = vxod.get();
- w = s;
- char k = 0;
- while (!vxod.eof())
- {
- if (z(dictSize - 1) > code_size) code_size++;
- s = vxod.get();
- if (dictionary.count(w + s)) w = w + s;
- else
- {
- for (int i = 0; i < code_size; i++)
- {
- byte[count] = dictionary[w] & (1 << code_size - 1 - i);
- count++;
- ct++;
- if (count == 8)
- {
- for (int i = 0; i < 8; i++)
- {
- k += (byte[i] << 7 - i);
- byte[i] = 0;
- }
- count = 0;
- vixod << k;
- k = 0;
- }
- }
- dictionary[w + s] = dictSize++;
- w = s;
- }
- }
- for (int i = 0; i < code_size; i++)
- {
- byte[count] = dictionary[w] & (1 << code_size - 1 - i);
- count++;
- ct++ ;
- if (count == 8)
- {
- for (int i = 0; i < 8; i++)
- {
- k += (byte[i] << 7 - i);
- byte[i] = 0;
- }
- vixod << k;
- k = 0;
- count = 0;
- }
- }
- if(count!=8)
- for (int i = 0; i < 8; i++)
- {
- ct++;
- k += (byte[i] << 7 - i);
- byte[i] = 0;
- }
- vixod << k;
- k = 0;
- count = 0;
- vixod.seekp(0);
- vixod << ct;
- vxod.close();
- vixod.close();
- }
- void decomp2(string in,string out)
- {
- ifstream vxod(in, ios::out | ios::binary);
- ofstream vixod(out);
- string symbol;
- string str;
- int dictSize = 256;
- bool byte[8];
- int code_size = 8;
- int count = 0;
- map<int, string> dictionary;
- for (int i = 0; i < 256; i++)
- dictionary[i] = string(1, i);
- int od = 0;
- int nk = 0;
- vxod.seekg(sizeof(int));
- nk = 0;
- char c = vxod.get();
- for (int j = 0; j < code_size; j++)
- {
- byte[count] = c & (1 << 7 - count);
- if (byte[count]) od = (od<< 1) + 1;
- else od = od << 1;
- count++;
- if (count == 8) {
- count = 0; c = vxod.get();
- }
- }
- code_size++;
- vixod << dictionary[od];
- symbol = dictionary[od];
- while (!vxod.eof())
- {
- nk = 0;
- for (int j = 0; j < code_size; j++)
- {
- byte[count] = c & (1 << 7 - count);
- if (byte[count]) nk = (nk << 1) + 1;
- else nk = nk << 1;
- count++;
- if (count == 8) {
- count = 0; c = vxod.get();
- }
- }
- if (!dictionary.count(nk))
- {
- str = dictionary[od]; str = str + symbol;
- }
- else str = dictionary[nk];
- vixod << str;
- symbol = str[0];
- dictionary[dictSize++] = dictionary[od] + symbol;
- od = nk;
- }
- vxod.close();
- vixod.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement