-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.cpp
205 lines (153 loc) · 4.01 KB
/
Code.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
vector<int> Compression(string s1)
{
cout << "Compression\n";
int code = 256;
int i;
unordered_map<string, int> dict;
for (i = 0; i <= 255; i++)
{
string ch = "";
ch += char(i);
dict[ch] = i;
}
string p = "";
string c = "";
p += s1[0];
vector<int> compressOut;
cout << "Current Next \tOutput Addition\n";
for (i = 0; i < s1.length(); i++)
{
if (i != s1.length() - 1)
c += s1[i + 1];
if (dict.find(p + c) != dict.end())
p = p + c;
else
{
cout << p << "\t " << c << "\t\t" << dict[p] << "\t\t"<< p + c << "\t" << code << endl;
compressOut.push_back(dict[p]);
dict[p + c] = code;
code++;
p = c;
}
c = "";
}
cout << p << "\t " << "-" << "\t\t" << dict[p] << endl;
compressOut.push_back(dict[p]);
return compressOut;
}
void Percent(float contIn, float contOut, int ok9, int ok10, int ok11)
{
float reduced;
if(ok11 == 1)
reduced = (contIn * 100 * 11) / contOut;
if(ok10 == 1)
reduced = (contOut * 100 * 10) / contIn;
if(ok9 == 1)
reduced = (contOut * 100 * 9) / contIn;
cout << "Text size was reduced by ";
cout << reduced;
cout << " percent";
cout << endl << endl;
}
void Decompression(vector<int> decOut)
{
cout << "\nDecompression\n";
unordered_map<int, string> dict;
int prev, cont = 256, n, i;
for (i = 0; i <= 255; i++) {
string ch = "";
ch += char(i);
dict[i] = ch;
}
prev = decOut[0];
string s = dict[prev];
string c = "";
c += s[0];
cout << s;
for (i = 0; i < decOut.size() - 1; i++)
{
n = decOut[i + 1];
if (dict.find(n) == dict.end())
{
s = dict[prev];
s = s + c;
}
else
s = dict[n];
cout << s;
c = "";
c += s[0];
dict[cont] = dict[prev] + c;
cont++;
prev = n;
}
}
void Help()
{
cout << "For each compression and decompression you have a separate array\n";
cout << "('s' for compression and 'arr' for decompression) to fill in with\n";
cout << "the corresponding data.\n";
cout << "Do not type characters in the command block!\n\n";
}
int main()
{
/// Modify s for Compression and arr for decompression
string s = "thisisthe";
vector<int> arr = {116, 104, 105, 115, 258, 256, 101};
int n;
cout << "Welcome to Lempel-Ziv-Welch Algorithm!\n\n";
cout << "Type the number from corresponding command below:\n";
while(1)
{
cout << "1. Compression\n";
cout << "2. Decompression\n";
cout << "3. Help\n";
cout << "4. Exit\n";
cout << "> ";
cin >> n;
cout << endl;
if(n == 1)
{
int ok9 = 0, ok10 = 0, ok11 = 0, i;
float contOut, contIn;
contIn = s.size() * 8;
vector<int> compressOut = Compression(s);
contOut = compressOut.size();
cout << "\nOutput: ";
for (i = 0; i < compressOut.size(); i++)
{
cout << compressOut[i] << " ";
if(compressOut[i] > 255 && compressOut[i] <= 511)
ok9 = 1;
if(compressOut[i] > 511 && compressOut[i] <= 1023)
ok10 = 1;
if(compressOut[i] > 1023)
ok11 = 1;
}
cout << endl;
Percent(contIn, contOut, ok9, ok10, ok11);
}
else if(n == 2)
{
Decompression(arr);
cout << endl << endl;;
}
else if(n == 3)
{
Help();
}
else if(n == 4)
{
break;
}
else
{
cout << "Wrong command, try again\n\n";
}
}
}