1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
| #include <string>
#include <map>
#include <vector>
#include <bitset>
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
using namespace std;
class RLE
{
public:
void RLE_coding()
{
char sym; //символ, который мы будем считывать
int kol = 1;// количество повторяющихся символов
ifstream RLE_input("INPUT_TEXT.txt");
ofstream RLE_compr("RLE_COMPRESSED_TEXT.txt");
while (RLE_input.good())
{
RLE_input.get(sym);//считываем символ
if (sym != RLE_input.peek()) // если символ не совпадает со следующим символом в файле
{
RLE_compr << kol << sym; // записываем результаты в выходной файл
kol = 0;
}
kol++;
}
RLE_input.close();
RLE_compr.close();
}
void RLE_decoding()
{
ifstream RLE_input("RLE_COMPRESSED_TEXT.txt");
ofstream RLE_decompr("RLE_DECOMPRESSED_TEXT.txt");
char sym1, sym2; // предыдущий и последующий символы
const char zero = '0';
while (RLE_input.peek() != EOF)
{
RLE_input.get(sym1);
RLE_input.get(sym2);
for (int i = 0; i < sym1 - zero; i++)
RLE_decompr << sym2;
}
RLE_input.close();
RLE_decompr.close();
}
};
class ARF
{
public:
string poem;
long double ver; // вероятности символов
char symbol; // наименование символа
long double low; // Нижняя граница
long double high; // Верхняя граница
friend string operator>>(ifstream& ustream, ARF& entry)
{
if (typeid(ustream) == typeid(ifstream))
ustream >> entry.poem;
return entry.poem;
}
void ARFalg() {
int size = 0;
double maxCompression = 0;
string poem;
ifstream ARF_input("INPUT_TEXT.txt");
getline(ARF_input, poem);
ARF ARF1;
ARF ARF2;
ARF ARF3;
ARF ARF4;
ARF ARF5;
size = ARF1.uniqueSymbol(poem);
// cout << size << endl;
ARF* mas = new ARF[size];
ARF2.FullMas(mas, poem);
ARF3.defineBoundaries(mas, size);
long double result = ARF4.ARF_coding(poem, mas, size);
//cout << result << endl;
ofstream ARF_compr("ARF_COMPRESSED_TEXT.txt");
ARF_compr << "Таблица кода: " << endl;
for (int i = 0; i < size; i++)
{
ARF_compr << mas[i].symbol << ": [" << mas[i].low << " : " << mas[i].high << ")" << endl;
maxCompression += mas[i].ver * log2(1 / mas[i].ver);
}
ARF_compr << "Итоговый результат: " << endl << result << endl;
string decode = ARF5.ARF_decoding(result, mas, size, poem.length());
//cout << decode << endl;
ofstream ARF_decompr("ARF_DECOMPRESSED_TEXT.txt");
ARF_decompr << decode;
ARF_input.close();
ARF_compr.close();
ARF_decompr.close();
}
double ARF_coding(string entered, ARF mas[], int size)
{
long double low = 0.0;
long double high = 1.0;
for (int i = 0; i < entered.length(); i++)
{
int tmp = 0;
for (int j = 0; j < size; j++)
{
if (mas[j].symbol == entered[i])
{
tmp = j;
break;
}
}
long double CodeRange = high - low;
high = low + CodeRange * mas[tmp].high;
low = low + CodeRange * mas[tmp].low;
}
return (low + high) / 2;
}
string ARF_decoding(long double code, ARF mas[], int size, int length)
{
string result;
long double encode = code;
int iter = 0;
int presence = 0;
for (int i = 0; i < length; i++)
{
for (iter = 0; iter < size; iter++)
{
if (encode >= mas[iter].low && encode < mas[iter].high)
{
result.push_back(mas[iter].symbol);
break;
}
}
long double range = mas[iter].high - mas[iter].low;
encode -= mas[iter].low;
encode /= range;
}
return result;
}
void FullMas(ARF mas[], string entered)
{
int iter = 0;
for (int i = 0; i < entered.length(); i++)
{
bool presence = false;
for (int j = iter; j >= 0; j--)
{
if (entered[i] == mas[j].symbol)
{
presence = true;
mas[j].ver++;
break;
}
}
if (presence == false)
{
mas[iter].symbol = entered[i];
mas[iter].ver = 1;
iter++;
}
}
for (int i = 0; i <= iter; i++)
{
mas[i].ver /= entered.length();
}
}
void defineBoundaries(ARF mas[], int size)
{
long double low = 0.0;
for (int i = 0; i < size; i++)
{
mas[i].low = low;
mas[i].high = low + mas[i].ver;
low += mas[i].ver;
}
}
int uniqueSymbol(string entered)
{
int size = 0;
for (int i = 0; i < entered.length(); i++) // Подсчет уникальных символов
{
bool presence = false;
for (int j = i - 1; j >= 0; j--)
{
if (entered[i] == entered[j])
{
presence = true;
break;
}
}
if (presence == false)
{
size++;
}
}
return size;
}
};
class LZ78
{
public:
int index;
string data;
LZ78* next;
void LZalg() {
string input;
ifstream aza("INPUT_TEXT.txt");
getline(aza, input);
LZ78 C;
string result = C.LZ_coding(input);
ofstream rez("LZ_COMPRESSED_TEXT.txt");
rez << result;
rez.close();
aza.close();
ifstream aza_dec("LZ_COMPRESSED_TEXT.txt");
getline(aza_dec, input);
LZ78 B;
string result2 = B.LZ_decoding(input);
ofstream rez_dec("LZ_DECOMPRESSED_TEXT.txt");
rez_dec << result2;
rez_dec.close();
aza_dec.close();
}
void st_Node(LZ78* head, int index, string data) {
head->index = index;
head->data = data;
head->next = NULL;
}
void insert_Node(LZ78* head, int index, string data) {
LZ78* new_Node = new LZ78;
new_Node->index = index;
new_Node->data = data;
new_Node->next = NULL;
LZ78* curr = head;
while (curr != NULL)
{
if (curr->next == NULL)
{
curr->next = new_Node;
return;
}
curr = curr->next;
}
}
LZ78* search_Node(LZ78* head, string data)
{
LZ78* curr = head;
while (curr != NULL)
{
if (data.compare(curr->data) == 0)
return curr;
else
curr = curr->next;
}
return NULL;
}
LZ78* search_Node(LZ78* head, int index)
{
LZ78* curr = head;
while (curr != NULL)
{
if (index == curr->index)
return curr;
else
curr = curr->next;
}
return NULL;
}
vector <string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str);
string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
string LZ_coding(string input)
{
LZ78* dictionary = new LZ78;
string word, result;
int length, last_seen, index = 1;
length = (int)input.length();
word = input[0];
st_Node(dictionary, 1, word);
result += "0," + word;
for (int i = 1; i < length; i++)
{
string data;
data = input[i];
re_check:
LZ78* search = search_Node(dictionary, data);
if (search)
{
i++;
data += input[i];
last_seen = search->index;
goto re_check;
}
else
{
char zero;
if (input[i] == ' ')
zero = '0';
else
zero = input[i];
if ((int)data.length() < 2)
result += " " + to_string(0) + "," + zero;
else
result += " " + to_string(last_seen) + "," + zero;
index++;
if (i != length)
insert_Node(dictionary, index, data);
}
}
return result;
}
string LZ_decoding(string input)
{
LZ78* dictionary = new LZ78;
string result2;
vector <string> s_input = split(input, ' ');
int zz = 2;
for (int i = 0; i < s_input.size(); i++)
{
vector <string> ss_input = split(s_input[i], ',');
if (i == 0)
{
st_Node(dictionary, 1, ss_input[1]);
result2 += ss_input[1];
}
else
{
LZ78* serched;
string get_search = ss_input[1];
serched = search_Node(dictionary, stoi(ss_input[0]));
if (serched)
{
result2 += serched->data + get_search;
get_search = serched->data + split(s_input[i], ',')[1];
insert_Node(dictionary, zz, get_search);
}
else
{
if (stoi(ss_input[0]) == 0)
insert_Node(dictionary, zz, get_search);
else
insert_Node(dictionary, zz, get_search);
result2 += get_search;
}
zz++;
}
}
if (result2[(int)result2.length() - 1] == '0')
result2 = result2.substr(0, result2.size() - 1);
return result2;
}
};
int main()
{
//�'ыбор ----
setlocale(LC_ALL, "Russian");
int choice = 0;
cout << "Введите 1 для алгоритма RLE;" << endl << "Введите 2 для алгоритма ARF;" << endl << "Введите 3 для алгоритма LZ78;" << endl;
cin >> choice;
auto start = chrono::high_resolution_clock::now();
if (choice == 1)
{
RLE RLE1;
RLE RLE2;
RLE1.RLE_coding();// вызов функции кодирования алгоритм RLE
RLE2.RLE_decoding();// вызов функции декодирования алгоритм RLE
auto stop = chrono::high_resolution_clock::now();
chrono::duration<float> duration = stop - start;
system("cls");
cout << "Время выполнение алгоритма RLE: " << duration.count() << endl;
}
else if (choice == 2)
{
ARF ARFS;
ARFS.ARFalg();//вызов функции кодирования/деодирования ARF
auto stop = chrono::high_resolution_clock::now();
chrono::duration<float> duration = stop - start;
system("cls");
cout << "Время выполнение алгоритма ARF: " << duration.count() << endl;
}
else if (choice == 3)
{
LZ78 LZ78start;
LZ78start.LZalg();//вызов функции кодирования/деодирования LZ78
auto stop = chrono::high_resolution_clock::now();
chrono::duration<float> duration = stop - start;
system("cls");
cout << "Время выполнение алгоритма LZ78: " << duration.count() << endl;
}
cout << "Алгоритм выполнен, результат в корневой папке" << endl;
return 0;
system("PAUSE");
} |