Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <ios>
  5. #include <iostream>
  6. #include <istream>
  7. #include <map>
  8. #include <ostream>
  9. #include <string>
  10. #include <vector>
  11. using namespace std;
  12.  
  13.  
  14.  
  15. void kodowanie(istream &wejscie, ostream &wyjscie)
  16. {
  17.     map<vector<char>, uint16_t> Slownik;
  18.  
  19.     uint16_t n=0;
  20.     for (int i = -128; i <= 127; i++)
  21.     {
  22.         Slownik[{(char)(i)}] = n;
  23.         n++;
  24.     }
  25.  
  26.     vector<char> ciag_znakow; // String
  27.     char znak_zczytywany;
  28.  
  29.     while (wejscie.get(znak_zczytywany))
  30.     {
  31.  
  32.  
  33.         ciag_znakow.push_back(znak_zczytywany);
  34.  
  35.         if (Slownik.count(ciag_znakow) == 0)
  36.         {
  37.  
  38.  
  39.             Slownik[ciag_znakow] = n;
  40.             n++;
  41.             ciag_znakow.pop_back();
  42.             wyjscie.write((char*) (&Slownik.at(ciag_znakow)), sizeof (uint16_t));
  43.             ciag_znakow = {znak_zczytywany};
  44.  
  45.  
  46.         }
  47.     }
  48.  
  49.     if (!ciag_znakow.empty())
  50.     {
  51.  
  52.         wyjscie.write((char*)(&Slownik.at(ciag_znakow)), sizeof (uint16_t));
  53.  
  54.     }
  55. }
  56.  
  57.  
  58.  
  59. void dekodowanie(istream &wejscie, ostream &wyjscie)
  60. {
  61.     vector<vector<char>> Slownik;
  62.  
  63.  
  64.  
  65.     for (int i = -128; i <= 127; i++)
  66.     {
  67.          Slownik.push_back({(char)(i)});
  68.     }
  69.     vector<char> ciag_znakow; // String
  70.     uint16_t k; // Key
  71.  
  72.     while (wejscie.read(reinterpret_cast<char *> (&k), sizeof (uint16_t)))
  73.     {
  74.  
  75.         if (k == Slownik.size()){
  76.             ciag_znakow.push_back(ciag_znakow[0]);
  77.             Slownik.push_back(ciag_znakow);
  78.         }
  79.         else
  80.         if (!ciag_znakow.empty())
  81.         {
  82.             ciag_znakow.push_back(Slownik.at(k).front());
  83.             Slownik.push_back(ciag_znakow);
  84.         }
  85.  
  86.  
  87.         wyjscie.write(&Slownik.at(k).front(), Slownik.at(k).size());
  88.         ciag_znakow = Slownik.at(k);
  89.     }
  90.  
  91. }
  92.  
  93.  
  94. int main(int argc, char *argv[])
  95. {
  96.  
  97.  
  98.     enum class Mode {
  99.         Kodowanie,
  100.         Dekodowanie
  101.     };
  102.  
  103.     Mode m;
  104.  
  105.     if (std::string(argv[1]) == "-znak_zczytywany")
  106.         m = Mode::Kodowanie;
  107.     else
  108.     if (std::string(argv[1]) == "-d")
  109.         m = Mode::Dekodowanie;
  110.  
  111.  
  112.     ifstream input_file(argv[2], std::ios_base::binary);
  113.  
  114.  
  115.     ofstream output_file(argv[3], std::ios_base::binary);
  116.  
  117.         if (m == Mode::Kodowanie)
  118.             kodowanie(input_file, output_file);
  119.         else
  120.         if (m == Mode::Dekodowanie)
  121.             dekodowanie(input_file, output_file);
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement