lutunovoleg

vector of bits

Oct 24th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <chrono>
  4. using namespace std;
  5. // --- --- --- --- ---
  6. const short N = 16;
  7. class set
  8. {
  9.     private: bool array[N];
  10.     public:
  11.         void is_empty()
  12.         {
  13.             bool flag = true;
  14.             for(int i = 0; i < N; i++)
  15.                 if (this->array[i]==true) flag=false;
  16.             cout << endl;
  17.             if(flag) cout << "It's empty";
  18.             else cout << "It isn't empty";
  19.             cout<<endl;
  20.  
  21.         }
  22.         // --- --- ---
  23.         set() { for(int i = 0; i < N; i++) this->array[i] = false; };
  24.         // --- --- ---
  25.         void generating()
  26.         {
  27.             short size_tmp = rand() % N; // generates random size for one array
  28.             short tmp;
  29.             for(int i = 0; i < size_tmp; i++)
  30.             {   tmp = rand() % N;
  31.                 if (this->array[tmp]==false) this->array[tmp]=true;
  32.             }
  33.         };
  34.         // --- --- ---
  35.         set operator || (const set& second) const{
  36.             set third;
  37.             for(int i = 0; i < N; i++)
  38.                 if((this->array[i] == true) || (second.array[i] == true)) third.array[i]=true;
  39.             return third;
  40.         }
  41.         // --- --- ---
  42.         set operator && (const set& second) const{
  43.             set third;
  44.             for(int i = 0; i < N; i++)
  45.                 if((array[i] == true) && (second.array[i] == true)) third.array[i]=true;
  46.             return third;
  47.         }
  48.         // --- --- ---
  49.     set  operator - (const set& second) const{
  50.         set third;
  51.         for(int i = 0; i < N; i++)
  52.         if((array[i] == true) && (second.array[i] == false)) third.array[i]=true;
  53.             return third;
  54.     }
  55.        // --- --- ---
  56.         void print()
  57.         {
  58.             cout << "[ ";
  59.             for(int i = 0; i < N; i++)
  60.             {
  61.                 if (this->array[i]==true)
  62.                     cout << hex << i << ' ';
  63.             }
  64.             cout << ']' << endl;
  65.         }
  66. };
  67. // --- --- --- --- ---
  68. int main()
  69. {
  70.     srand(2);
  71.     set a, b, c, d, e, tmp;
  72.     a.generating(); cout << "A: "; a.print();
  73.     b.generating(); cout << "B: "; b.print();
  74.     c.generating(); cout << "C: "; c.print();
  75.     d.generating(); cout << "D: "; d.print();
  76.     double result;
  77.     cout << endl << "*Calculating result*" << endl;
  78.     for(int i = 0; i < pow(10, 7); i++)
  79.     {
  80.         auto t1 = chrono::high_resolution_clock::now();
  81.         e = ((a||c)-(b||d));
  82.         auto t2 = chrono::high_resolution_clock::now();
  83.         auto diff = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  84.         result += diff.count();
  85.     }
  86.     cout << endl << "E: "; e.print();
  87.     cout << "time: " << result << " seconds" << endl;
  88.     return 0;
  89. }
  90. // --- --- --- --- --- --- --- --- ---
  91.  
Add Comment
Please, Sign In to add comment