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.

enter image description here

enter image description here

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:

  1. The table *gen(char mass[]) (line 37-59) contains the uniq(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 the size array).
  2. How to transfer to res[i] (line 69) all contents of gen[j].code , and not just the first character?
  • It is better to post the code here. - Dmitriy
  • Let's start with the fact that you did not fulfill the first condition: you did not write a program in C ++ language - AR Hovsepyan
  • I do not really understand how to encode. Please show me an example of how the encoded symbol should be (01100001) in cases where it occurs most frequently and in the reverse case - AR Hovsepyan
  • Added information - maxim halyapin
  • that's another thing ... - AR Hovsepyan

1 answer 1

You tried so hard to write a question that I could not resist and made for me my own version (everything is written in C ++). Honestly, I didn’t think about optimization, but I think it’s okay:

 #include <iostream> #include <unordered_map> #include <bitset> #include <vector> #include <algorithm> #include <sstream> using namespace std; using Par = std::pair<char, int>; struct Cmp { bool operator ()(Par p1, Par p2) { return p1.second > p2.second; } }; unordered_map<char, string> info_from_string(const string& s) { unordered_map<char, int > m; int sz = s.size(); for (int i = 0; i < sz; ++i) { m[s[i]]++; } //ΠΎΠ±ΡŒΠ΅ΠΊΡ‚ m Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ содСрТит Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ символов ΠΈ ΠΈΡ… количСства Π² строкС //Π½Π°ΠΌ Π½ΡƒΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 8 отсорированных (ΠΏΠΎ количСству) элСмСнтов vector<Par> v(m.begin(), m.end()); partial_sort(v.begin(), v.begin() + 8, v.end(), Cmp()); //Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π·Π°ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅ΠΌ символы Π² строкС bitset<8> bs; string str; sz = v.size(); char c; //создадим Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ символ/ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ° unordered_map<char, string> res; for (int i = 0; i < sz; ++i) { c = v[i].first; if(i < 8) //Ссли символ ΠΈΠ· Ρ‚ΠΎΠΏ 8 { static int k{}; //ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ символ ΠΈΠΌΠ΅Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ bs = k; str = bs.to_string(); //ΠΊΠ°ΠΊ Π²Ρ‹ сказали, Π±Π΅Ρ€Π΅ΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ послСдныС 4 Π±ΠΈΡ‚Π°, хотя, //ΠΏΠΎ Ρ…ΠΎΡ€ΠΎΡˆΠ΅ΠΌΡƒ, Π½ΡƒΠΆΠ½ΠΎ Π±Ρ€Π°Ρ‚ΡŒ Π½Π° 1 большС, Ρ‡Π΅ΠΌ помСщаСтся // v[0].first (число, ΠΈΠΌΠ΅ΡŽΡ‰Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ наибольшСго количСства символа str = str.substr(4); ++k; } else { bs = c; str = "1" + bs.to_string(); } res.emplace(make_pair(c, str)); } return res; } // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ создадим Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ°/символ unordered_map<string, char> mm; const string code(const string& s) //ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ° { string str; auto m = info_from_string(s); for (auto p : m) mm.emplace(make_pair(p.second, p.first)); for (char c : s) str += m[c] + ' '; return str; } const string decode(const string& s) // раскодировка { istringstream is(s); //Ρ‡Ρ‚ΠΎΠ±Ρ‹ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ с ΠΊΠ°ΠΆΠ΄Ρ‹ΠΌ словом string ss, str; while (is >> ss) str += mm[ss]; return str; } int main() { const string s("we need to write code in C++. Pass a string to a function"), scode(code(s)), sdecode(decode(scode)); //ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΡƒ cout << scode << endl << endl; //ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ раскодировку cout << sdecode; return 0; } 

If you write an appropriate class, then everything will look much simpler and shorter, but your task talks about the implementation of functions.