Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <list>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. class Node {
  10. public:
  11.  
  12.     int count;
  13.     char sym;
  14.     Node *left = 0;
  15.     Node *right = 0;
  16.  
  17.     Node(int cnt) {
  18.         count = cnt;
  19.     }
  20.  
  21.     Node(int cnt, char smb) {
  22.         count = cnt;
  23.         sym = smb;
  24.     }
  25.  
  26. };
  27.  
  28. bool isLeaf(Node *node) {
  29.     return node->left == nullptr and node->right == nullptr;
  30. }
  31.  
  32.  
  33. vector<bool> code;
  34. map<char, vector<bool> > table;
  35.  
  36. void MakeCodes(Node *root) {
  37.     if (root->left != nullptr) {
  38.         code.push_back(false);
  39.         MakeCodes(root->left);
  40.     }
  41.  
  42.     if (root->right != nullptr) {
  43.         code.push_back(true);
  44.         MakeCodes(root->right);
  45.     }
  46.  
  47.     if (isLeaf(root)) table[root->sym] = code;
  48.  
  49.     code.pop_back();
  50. }
  51.  
  52.  
  53. int main(int argc, char *argv[]) {
  54. ////// считываем посимвольно в мапу
  55.     ifstream f("CA.pptx", ios::out | ios::binary);
  56.  
  57.     map<unsigned char, int> symbols;
  58.  
  59.     while (!f.eof()) {
  60.         char symbol = f.get();
  61.         symbols[symbol]++;
  62.     }
  63.  
  64.  
  65. ////// записывам мапу в вектор формируя ноды
  66.  
  67.     vector<Node *> numbers;
  68.  
  69.     map<unsigned char, int>::iterator ii;
  70.     for (ii = symbols.begin(); ii != symbols.end(); ii++) {
  71.         cout<<int(ii->first)<<" : "<<ii->second<<endl;
  72.         //Node *p = new Node(i->first,i->second);
  73.         //cout << ii->first << ":" << ii->second << endl;
  74.         numbers.push_back(new Node(ii->second, ii->first));
  75.     }
  76.  
  77.     while (numbers.size() != 1) {
  78.         int last_element_index = numbers.size() - 1;
  79.         /// создаем новую ноду на основе двух последних
  80.         Node *new_node = new Node(numbers[last_element_index]->count + numbers[last_element_index - 1]->count);
  81.         /// присваиваем этой ноде левого-правого ребенка
  82.         new_node->left = numbers[last_element_index - 1];
  83.         new_node->right = numbers[last_element_index];
  84.         ///выкидываем последний
  85.         numbers.pop_back();
  86.         ///новый ставим на место последнего
  87.         numbers[last_element_index - 1] = new_node;
  88.         ///сортируем по размеру
  89.         for (int i = numbers.size() - 1; i > 0 and numbers[i - 1]->count < numbers[i]->count; i--) {
  90.             swap(numbers[i], numbers[i - 1]);
  91.         }
  92.     }
  93.  
  94.     Node *root = numbers[0];
  95.  
  96. ////// делаем коды
  97.  
  98.     MakeCodes(root);
  99.  
  100. ////// записываем сжатую версию файла
  101.  
  102.     f.clear();
  103.     f.seekg(0);
  104.  
  105.     ofstream g("output.huff", ios::out | ios::binary);
  106.  
  107.     int count = 0;
  108.     char buf = 0;
  109.     while (!f.eof()) {
  110.         char c = f.get();
  111.         vector<bool> x = table[c];
  112.         for (int n = 0; n < x.size(); n++) {
  113.             buf = buf | x[n] << (7 - count);
  114.             count++;
  115.             if (count == 8) {
  116.                 count = 0;
  117.                 g << buf;
  118.                 buf = 0;
  119.             }
  120.         }
  121.     }
  122.  
  123.     f.close();
  124.     g.close();
  125.  
  126. ///// разархивируем
  127.  
  128.     ifstream F("output.huff", ios::in | ios::binary);
  129.     ofstream file(  "CAposle.pptx", ios::binary);
  130.     //char c = 'a';
  131.     //file.write((char *)c,1);
  132.     setlocale(LC_ALL, "Russian");
  133.  
  134.     Node *node = root;
  135.     count = 0;
  136.     char byte;
  137.     byte = F.get();
  138.     while (!F.eof()) {
  139.         bool b = (bool (byte & (1 << (7 - count))));
  140.         if (b) node = node->right; else node = node->left;
  141.         if (isLeaf(node)) {
  142.             //cout << node->sym;
  143.             file.write((char *) &node->sym, sizeof(char));
  144.             node = root;
  145.         }
  146.         count++;
  147.         if (count == 8) {
  148.             count = 0;
  149.             byte = F.get();
  150.         }
  151.     }
  152.  
  153.     F.close();
  154.     file.close();
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement