Advertisement
DacCum

Bool(неудачная попытка...)

Sep 27th, 2021 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Bool {
  7.     private:   
  8.         int bit;
  9.         bool *q;
  10.     public:
  11.         Bool() {
  12.             bit = 1;
  13.             q = new bool[bit * 8];
  14.             for (int i = 0; i < bit * 8; i++)
  15.                 q[i] = 0;
  16.         }
  17.  
  18.         Bool(int _bit) {
  19.             bit = _bit;
  20.             q = new bool[bit * 8];
  21.             for (int i = 0; i < bit * 8; i++)
  22.                 q[i] = 0;
  23.         }
  24.  
  25.         void up_bit() {
  26.             bool* tmp_q = new bool[(bit + 1) * 8];
  27.             for (int i = 0; i < (bit + 1) * 8; i++) {
  28.                 if (i < bit * 8)
  29.                     tmp_q[i] = 0;
  30.                 else
  31.                     tmp_q[i] = q[i - bit * 8];
  32.             }
  33.             bit++;
  34.             delete[] q;
  35.             q = tmp_q;
  36.         }
  37.  
  38.         void input() {
  39.             string buffer;
  40.             getline(cin, buffer);
  41.             int j = 0;
  42.             bool f;
  43.             for (int i = 0; i < buffer.size(); i++) {
  44.                 if (buffer[buffer.size() - 1 - i] < 48 || buffer[buffer.size() - 1 - i] > 57) {
  45.                     j++;
  46.                     continue;
  47.                 }
  48.                 else {
  49.                     f = 0;
  50.                     while (i < buffer.size() && buffer[buffer.size() - 1 - i] != ' ') {
  51.                         if (buffer[buffer.size() - 1 - i] - '0' != 0)
  52.                             f = 1;
  53.                         i++;
  54.                         j++;
  55.                     }
  56.                 }
  57.                 if (!f)
  58.                     q[bit * 8 - 1 - i + j] = 0;
  59.                 else
  60.                     q[bit * 8 - 1 - i + j] = 1;
  61.  
  62.             }
  63.         }
  64.  
  65.         Bool &operator = (const Bool& b) {
  66.             bit = b.bit;
  67.             q = new bool[bit * 8];
  68.             for (int i = 0; i < bit * 8; i++)
  69.                 q[i] = b.q[i];
  70.  
  71.             return *this;
  72.         }
  73.  
  74.         Bool operator * (Bool &b) {
  75.             if (bit < b.bit)
  76.                 up_bit();
  77.             else if (bit > b.bit)
  78.                 b.up_bit();
  79.             Bool c(bit);
  80.             for (int i = 0; i < bit * 8; i++) {
  81.                 if (b.q[i] == 1 && q[i] == 1)
  82.                     c.q[i] = 1;
  83.                 else
  84.                     c.q[i] = 0;
  85.             }
  86.  
  87.             return c;
  88.         }
  89.  
  90.         Bool operator + (Bool& b) {
  91.             if (bit < b.bit)
  92.                 up_bit();
  93.             else if (bit > b.bit)
  94.                 b.up_bit();
  95.             Bool c(bit);
  96.             for (int i = 0; i < bit * 8; i++) {
  97.                 if (b.q[i] == 1 || q[i] == 1)
  98.                     c.q[i] = 1;
  99.                 else
  100.                     c.q[i] = 0;
  101.             }
  102.  
  103.             return c;
  104.         }
  105.  
  106.         Bool operator ^ (Bool& b) {
  107.             if (bit < b.bit)
  108.                 up_bit();
  109.             else if (bit > b.bit)
  110.                 b.up_bit();
  111.             Bool c(bit);
  112.             for (int i = 0; i < bit * 8; i++) {
  113.                 if (b.q[i] ^ q[i])
  114.                     c.q[i] = 1;
  115.                 else
  116.                     c.q[i] = 0;
  117.             }
  118.            
  119.             return c;
  120.         }
  121.  
  122.         friend Bool operator == (Bool &a, Bool &b);
  123.  
  124.         friend Bool operator != (Bool &a, Bool &b);
  125.  
  126.         void print() {
  127.             for (int i = 0; i < bit * 8; i++)
  128.                 cout << q[i] << ' ';
  129.             cout << endl;
  130.         }
  131. };
  132.  
  133. Bool operator == (Bool& a, Bool& b) {
  134.     if (a.bit < b.bit)
  135.         a.up_bit();
  136.     else if (a.bit > b.bit)
  137.         b.up_bit();
  138.     for (int i = 0; i < a.bit * 8; i++) {
  139.         if (b.q[i] != a.q[i])
  140.             return 0;
  141.     }
  142.  
  143.     return 1;
  144. }
  145.  
  146. Bool operator != (Bool& a, Bool& b) {
  147.  
  148. }
  149.  
  150. int main() {
  151.     Bool a, b, c;
  152.     a.input();
  153.     a.print();
  154.     b.input();
  155.     b.print();
  156.     c = a + b;
  157.     c.print();
  158.     c = a * b;
  159.     c.print();
  160.     c = a ^ b;
  161.     c.print();
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement