Guest User

Untitled

a guest
Jun 3rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class T>
  6. class Array
  7. {
  8. private:
  9.  
  10.     int size;
  11.     T *arr;
  12.  
  13.     int32_t countSetBits(uint8_t value)
  14.     {
  15.         if (value == 0)
  16.             return 0;
  17.         else
  18.             return (value & 1) + countSetBits(value >> 1);
  19.     }
  20.  
  21.     int32_t countOverByteArray(uint8_t* array, size_t size)
  22.     {
  23.         int32_t result = 0;
  24.  
  25.         for (size_t i = 0; i < size; ++i)
  26.             result += countSetBits(array[i]);
  27.  
  28.         return result;
  29.     }
  30.  
  31. public:
  32.  
  33.     Array<T>(size_t size)
  34.     {
  35.         arr = new T[this->size = size];
  36.         if (!arr)
  37.         {
  38.             cout << "Пам'ять не виділено!" << endl;
  39.             return;
  40.         }
  41.         cout << "Створено!" << endl;
  42.     }
  43.  
  44.     ~Array<T>()
  45.     {
  46.         cout << "Видалено!" << endl;
  47.         delete[] arr;
  48.     }
  49.  
  50.     int CountOfDigit()
  51.     {
  52.         return countOverByteArray(reinterpret_cast<uint8_t*>(arr), size * sizeof(T));
  53.     }
  54.  
  55.     friend class ArrayIterator<T>;
  56.     friend ostream &operator << <>(ostream&, const Array&);
  57.     friend istream &operator >> <>(istream&, Array&);
  58.  
  59. };
  60.  
  61. template <class T>
  62. ostream &operator << (ostream &out, Array<T> const &mas)
  63. {
  64.     if (sizeof(T) == 1)
  65.         for (size_t i = 0; i < mas.size; i++)
  66.             out << mas.arr[i];
  67.     else
  68.         for (size_t i = 0; i < mas.size; i++)
  69.             out << mas.arr[i] << ' ';
  70.  
  71.     return out;
  72. }
  73.  
  74. template <class T>
  75. istream &operator >> (istream &in, Array<T> &mas)
  76. {
  77.     for (size_t i = 0; i < mas.size; i++)
  78.         in >> mas.arr[i];
  79.  
  80.     return in;
  81. }
  82.  
  83. template <class T>
  84. class ArrayIterator
  85. {
  86. private:
  87.     const Array<T>& array;
  88.     int index = 0;
  89.  
  90. public:
  91.  
  92.     ArrayIterator(const Array<T> &prg) : array(prg)
  93.     {
  94.         index = 0;
  95.     }
  96.     void operator++(int)
  97.     {
  98.         index++;
  99.     }
  100.     bool operator()()
  101.     {
  102.         return index != Array.number;
  103.     }
  104.     Array<T>& operator *()
  105.     {
  106.         return *array.arr[index];
  107.     }
  108. };
  109.  
  110. int main(void)
  111. {
  112.     system("color 70 & chcp 1251 & cls");
  113.  
  114.     int ArraySize;
  115.  
  116.     cout << "Введіть розмір масиву -> ";
  117.     cin >> ArraySize;
  118.  
  119.     Array<int> arr(ArraySize);
  120.     ArrayIterator<int> ArrIT(arr);
  121.  
  122.     cout << "Введіть елементи масиву -> ";
  123.     cin >> arr;
  124.     cout << "Кількість одиниць" << endl;
  125.     cout << arr.CountOfDigit() << endl;
  126.  
  127.     //cout << " Введені елементи: " << arr << endl;
  128.  
  129.  
  130.     while (ArrIT())
  131.     {
  132.         cout << *ArrIT << endl;
  133.         ArrIT++;
  134.     }
  135.  
  136.     system("pause");
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment