Advertisement
smatskevich

Seminar12

Dec 5th, 2022
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main1() {
  5.   int a = 2'000'000'000;
  6.  int b = a << 1;
  7.  std::cout << b << std::endl;
  8.  
  9.  while (b) {
  10.    b >>= 1;
  11.    std::cout << b << std::endl;
  12.  }
  13.  return 0;
  14. }
  15.  
  16. int main2() {
  17.  unsigned int n; std::cin >> n;
  18.  int count = -1;
  19.  while (n) {
  20.    count++;
  21.    n >>= 1;
  22.  }
  23.  std::cout << count << std::endl;
  24.  return 0;
  25. }
  26.  
  27. typedef char byte;
  28.  
  29. struct IInputStream {
  30.  // Возвращает false, если поток закончился
  31.  virtual bool Read(byte& value) = 0;
  32. };
  33.  
  34. struct IOutputStream {
  35.  virtual void Write(byte value) = 0;
  36. };
  37.  
  38. class InputString : public IInputStream {
  39. public:
  40.  explicit InputString(std::string s_) : s(std::move(s_)) {}
  41.  bool Read(byte& value) override {
  42.    if (current >= s.length()) return false;
  43.    value = s[current++];
  44.    return true;
  45.  }
  46.  
  47. private:
  48.  std::string s;
  49.  int current = 0;
  50. };
  51.  
  52. class OutputCout : public IOutputStream {
  53. public:
  54.  void Write(byte value) override { std::cout << value; }
  55. };
  56.  
  57. class OutputCout2 : public IOutputStream {
  58. public:
  59.  void Write(byte value) override {
  60.    for (int i = 7; i >= 0; --i) std::cout << ((value >> i) & 1);
  61.    std::cout << " ";
  62.  }
  63. };
  64.  
  65. class OutputAdapter {
  66. public:
  67.  explicit OutputAdapter(IOutputStream& out_) : out(out_) {}
  68.  void WriteBit(bool bit);
  69.  void WriteByte(byte b);
  70.  void Finish();
  71.  
  72. private:
  73.  IOutputStream& out;
  74.  byte buffer;  // 8 бит буфера, накапливается перед сбросом в out.
  75.  int cursor = 0;  // Число заполненных битов в buffer. Используются cursor младших битов в buffer.
  76. };
  77.  
  78. // 10110001|01010aaa|aaaaa
  79. //
  80. void OutputAdapter::WriteBit(bool bit) {
  81.  // 000|01101 -> 00|011010 -> 00|011011
  82.  buffer = (buffer << 1) + bit;
  83.  ++cursor;
  84.  if (cursor == 8) {
  85.    out.Write(buffer);
  86.    cursor = 0;
  87.  }
  88. }
  89. void OutputAdapter::WriteByte(byte b) {
  90.  // 00000|010 + AAAAAaaa -> 010AAAAA + 00000|aaa
  91.  int shift = 8 - cursor;
  92.  byte value = (buffer << shift) | (b >> cursor);
  93.  out.Write(value);
  94.  buffer = ((int)(b << shift) & 0x000000FF) >> shift;  // buffer = b & 0b00000111;
  95. }
  96. void OutputAdapter::Finish() {
  97.  if (cursor != 0) {
  98.    int shift = 8 - cursor;
  99.    // 10000000 >> 7 -> 11111111
  100.    buffer = ((int)(buffer << shift) & 0x000000FF) >> shift;
  101.    out.Write(buffer);
  102.  }
  103.  out.Write((byte)(cursor));
  104. }
  105.  
  106. void Encode(IInputStream& original, IOutputStream& compressed) {
  107.  OutputAdapter outAdapter(compressed);
  108.  byte value;
  109.  while (original.Read(value)) {
  110.    outAdapter.WriteByte(value);
  111.  }
  112. }
  113.  
  114. int main3() {
  115.  std::string s;
  116.  std::cin >> s;
  117.  InputString source(s);
  118.  OutputCout target;
  119.  
  120.  Encode(source, target);
  121.  return 0;
  122. }
  123.  
  124. int main() {
  125.  OutputCout2 out;
  126.  OutputAdapter adapter(out);
  127.  adapter.WriteByte('a');
  128.  adapter.WriteBit(true);
  129.  adapter.WriteByte('a');
  130.  adapter.Finish();
  131.  return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement