Condition:
Write a program in C ++, in which to create:
character string and fill it;
a byte array (the elements of the array are of type char), designed to store the encoded string.
Each character in the string is encoded according to the following rule: the first 8 most common characters
they are encoded with 0 bit and the three-bit code following it (000 ... 111), the remaining characters - 1 bit and the next ASCII character code (8 bits). The resulting code is written to a byte array. As a result, in the byte array should be written sequentially, one after the other, codes of all characters of the string. Determine the compression ratio. Display the elements of the byte array in binary form.
Decode the bit sequence contained in the byte array. Decoding results over-write to file.
Implement in the program the functions necessary to solve the problem.
What I managed to do:
#include <iostream> #include <string> #include <string.h> #include <algorithm> #include <bitset> #include <array> using namespace std; void del(char el[], int j, int &size) { for(int i = j; i < size - 1; i++) el[i] = el[i + 1]; size--; } void uniq(char el[], int &size) { int i, j; char elem; for(i = 0; i < size; i++){ elem = el[i]; for(j = i + 1; j < size; j++){ if(elem == el[j]) { del(el, j, size); j--; } } } } struct table{ char text; int num; char* code; }; bool asort(table l, table r) { return l.num > r.num; } table *gen(char mass[]){ int size = strlen(mass); char str[size]; strcpy(str, mass); uniq(str, size); table *codetable; codetable = new table[size]; for(int i = 0; i < size; i++){ codetable[i].text = str[i]; codetable[i].num = count(mass, mass+size*10, str[i]); } sort(codetable, codetable+size, asort); for(int i = 0; i < size; i++){ if (i < 8){ bitset<4> a(i); codetable[i].code = strdup(a.to_string().c_str()); }else{ bitset<8> b((int)str[i]); codetable[i].code = strdup(("1" + b.to_string()).c_str()); } } return codetable; } char* encode(char mass[], table gen[]) { int size = strlen(mass); char* res; res = new char[size]; cout << "ΠΠ°ΠΊ Π½Π°Π΄ΠΎ Π·Π°ΡΠΈΡΡΠΎΠ²Π°ΡΡ:"<< endl; for(int i = 0; i < size; i++){ for(int j = 0; j < 20; j++){ if (mass[i] == gen[j].text){ res[i] = *gen[j].code; cout << gen[j].code << " "; } } } cout << endl; return res; } char* decode(char* crypt[], table gen[], int size){ char* res; res = new char[size]; for(int i = 0; i < size; i++){ for(int j = 0; j < size; j++){ if (gen[j].code == crypt[i]) { res[i] = gen[j].text; } } } return res; } int main() { setlocale(LC_CTYPE, "Russian"); char mass[] = "Yeah baby, I like it like that You gotta believe me when I tell you"; //char mass[] = "To have to go to bed by day?"; int size = strlen(mass); table *code; code = new table; code = gen(mass); cout << "Π‘ΡΡΠΎΠΊΠ°: \n" << mass << endl; cout << "Π’Π°Π±Π»ΠΈΡΠ° ΡΠΈΡΡΠΎΠ²Π°Π½ΠΈΡ:" << endl; for(int i = 0; i < 20; i++){ cout << code[i].text << " "<< code[i].code << endl; } char* encrypt; encrypt = new char[size]; encrypt = encode(mass, code); cout << "Π’ΠΎ, ΡΡΠΎ Π²ΡΠ΄Π°Π΅Ρ encode:" << endl; for(int i = 0; i < size; i++){ cout << encrypt[i] << " "; } //char* decrypt = decode(encrypt, gen); //cout << decrypt == mass ? "True" : "False"; return 0; } There are several questions:
- The
table *gen(char mass[])(line 37-59) contains theuniq(str, size)code. How can I find out the number of elements that this piece of code displays? (It is necessary in lines 43-49 to use the size of this sample, and not the entire length of thesizearray). - How to transfer to
res[i](line 69) all contents ofgen[j].code, and not just the first character?

