Facebook
From Gray Owl, 3 Years ago, written in Plain Text.
This paste is a reply to Untitled from Ungracious Leopard - view diff
Embed
Download Paste or View Raw
Hits: 116
  1. /*
  2. * Створити клас Matrix3F (матриця цілих чисел), використовуючи клас Vector3F. Розробити такі елементи класу:    Поля (захищені):  Vector3F  * PointArray; // масив  int n; //  розмір матриці (к-ть векторів)  int codeError; // код помилки  static int num_matrix; // кількість матриць (загально доступних)  У змінну стану встановлювати код помилки, коли не вистачає пам'яті, виходить за межі матриці. У класі визначити:  конструктори:   конструктор без параметрів(PointArray = nullptr, n  = codeError = 0);  конструктор з одним параметрів типу int size( створює матрицю n=size на 3, інінціалізує поля в нуль );  конструктор із двома параметрами - розмір матриці (виділяє місце та ініціалізує значенням другого параметру);  конструктор із чотирма параметрами - розмір матриці, значення ініціалізації полів вектора;  копіювання;  функції доступу до полів : n та codeError.  деструктор (деструктор звільняє пам'ять);   перевантаження операцій (операції перевантажувати через функції класу або дружні функції, якщо не вказано яким чином це робити):   унарних префіксних та постфіксних ++ та --: одночасно збільшує (зменшує) значення елементів масиву на 1;   унарної логічної ! (заперечення): повертає значення true, якщо елементи якщо n не дорівнює – нулю, інакше false;   унарний арифметичний - (мінус) : повертає всі елементи масиву класу матриця з протилежним знаком;    присвоєння =: копіює матрицю (перевантажити через функцію класу);  арифметичних бінарні:
  3. * a. + додавання: i. для двох матриць; ii. для матриці та скаляра типу int; iii. для матриці та скаляра типу double; iv. для матриці та скаляра типу float; b. - (віднімання):  i. для двох матриць ii. для матриці та скаляра типу int; iii. для матриці та скаляра типу double; iv. для матриці та скаляра типу float; c. *(множення)  i. для двох матриць, ii. для матриці та вектора Vector3F; iii. для матриці та скаляра типу double; iv. для матриці та скаляра типу float; d. /(ділення)  i. векторне множення елементів масивів PointArray; ii. для матриці та скаляра типу int; iii. для матриці та скаляра типу float; e. %(остача від ділення)  i. для матриці та скаляра типу int; f. += присвоєння з додаванням: i. для двох матриць ii. для матриці та скаляра типу double; iii. для матриці та скаляра типу float; g. -= присвоєння з відніманням:  i. для двох матриць; ii. для матриці та скаляра типу double; iii. для матриці та скаляра типу float; h. *= присвоєння з множенням  i. для двох матриць, ii. для матриці та вектора Vector3F; iii. для матриці та скаляра типу double; iv. для матриці та скаляра типу float; i. /= присвоєння з діленням  i. для матриці та скаляра типу int; ii. для матриці та скаляра типу double; iii. для матриці та скаляра типу float; j. %= присвоєння з остачею від ділення  i. для матриці та скаляра типу int;  операцій == (рівності) та != (нерівності), функція-операція виконує певні дії над кожною парою елементів матриці за індексом;  порівняння (функція-операція виконує певні дії над кожною парою елементів матриць за індексом) a. > (більше) для двох матриць;  b. >= (більше рівне) для двох матриць; c. < (менше) для двох матриць; d. <= (менше рівне) для двох матриць.
  4. * операцію індексації [] – функцію, яка звертається до елементу  Vector3F, якщо індекс невірний вказує на останній елемент масиву та встановлює код помилки у змінну CodeError.   розподілу пам’яті new та delete;  виклику функції ();  побітові операції зсувів, як дружні операції введення та виведення вектора у потік (перевантажувати через дружні функції) a. введення  >> (побітовий зсув право) ; b. введення << (побітовий зсув ліво);  Передбачити можливість підрахунку числа об'єктів даного типу. Перевірити роботу цього класу.
  5. */
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  int matrix_count=0;
  14.  int vectors_count=0;
  15. class vector
  16. {
  17. private:
  18.     int *x;
  19.     int size_vector;
  20.  
  21. public:
  22.     vector()
  23.     {
  24.         const int size=5;
  25.         size_vector=size;
  26.         x=new int[size_vector];
  27.         for(int i=0; i<size_vector; i++)
  28.         {
  29.             x[i]=0;
  30.         }
  31.         vectors_count++;
  32.     }
  33.  
  34.     vector(const int _n)
  35.     {
  36.         size_vector=_n;
  37.         x=new int[size_vector];
  38.         for(int i=0; i<size_vector; i++)
  39.         {
  40.             x[i]=5;
  41.         }
  42.         ++vectors_count;
  43.     }
  44.  
  45.     vector(const int _n, int _value):size_vector(_n)
  46.     {
  47.         x=new int[size_vector];
  48.         for(int i=0; i<size_vector; i++)
  49.         {
  50.             x[i]=_value;
  51.         }
  52.         ++vectors_count;
  53.     }
  54.  
  55.     vector(const vector &other)
  56.     {
  57.         this->x=new int[other.size_vector];
  58.         for(int i=0; i<other.size_vector; i++)
  59.         {
  60.             this->x[i]=other.x[i];
  61.         }
  62.         ++vectors_count;
  63.     }
  64.  
  65.     void operator = (const vector &other)
  66.     {
  67.         this->size_vector=other.size_vector;
  68.         if(this->x != nullptr )
  69.         {
  70.             delete[] this->x;
  71.         }
  72.         this->x=new int[other.size_vector];
  73.         for(int i=0; i<other.size_vector; i++)
  74.         {
  75.             this->x[i]=other.x[i];
  76.         }
  77.     }
  78.  
  79.     vector &operator ~()
  80.     {
  81.         for(int i=0; i<this->size_vector; i++)
  82.         {
  83.             this->x[i]=~(this->x[i]);
  84.         }
  85.         return *this;
  86.     }
  87.  
  88.     bool operator!=(const vector &other)
  89.     {
  90.         for(int i=0; i<this->size_vector; i++)
  91.         {
  92.             if(this->x[i]!=other.x[i]) return true;
  93.             else return false;
  94.         }
  95.     }
  96.  
  97.      vector& operator -()
  98.      {
  99.         for(int i=0; i<this->size_vector; i++)
  100.         {
  101.             this->x[i]=-(this->x[i]);
  102.         }
  103.         return *this;
  104.      }
  105. /*
  106.  
  107.  
  108.      vector operator+(const vector &other)
  109.     {
  110.         for(int i=0; i<this->size_vector; i++)
  111.         {
  112.             this->x[i] = this->x[i]+other.x[i];
  113.         }
  114.         return *this;
  115.     } */
  116.  
  117.     void print()
  118.     {
  119.         for (int i=0; i<this->size_vector; i++)
  120.         {
  121.             cout<<x[i]<<" ";
  122.         }
  123.         cout<<"n";
  124.     }
  125.     friend ostream& operator<< (ostream &out, const vector &other);
  126.     friend class matrix;
  127.     ~vector(){--vectors_count;delete[] x;}
  128.  
  129. };
  130.  
  131. ostream &operator<< (ostream &out, const vector &other)
  132. {
  133.     for(int i=0; i<other.size_vector; i++)
  134.     {
  135.       out << other.x[i]<< " ";
  136.     }
  137.     return out;
  138.  
  139. }
  140.  
  141. class matrix
  142. {
  143. private:
  144.     vector *arr;
  145.     int vector_count;
  146. public:
  147.     matrix()
  148.     {
  149.         const int size=5;
  150.         vector_count=size;
  151.         arr=new vector[vector_count];
  152.         for(int i=0; i<vector_count; i++)
  153.         {
  154.             arr[i]=5;
  155.         }
  156.     }
  157.  
  158.     matrix(int _n)
  159.     {
  160.         arr=new vector[vector_count];
  161.         for(int i=0; i<vector_count; i++)
  162.         {
  163.             arr[i]=5;
  164.         }
  165.         ++matrix_count;
  166.     }
  167.  
  168.     matrix(const int _n, const int _value):vector_count(_n)
  169.     {
  170.         arr=new vector[vector_count];
  171.         for(int i=0; i<vector_count; i++)
  172.         {
  173.             arr[i]=_value;
  174.         }
  175.         ++matrix_count;
  176.     }
  177.  
  178.     matrix(const matrix &other)
  179.     {
  180.         this->arr=new vector[other.vector_count];
  181.         for(int i=0; i<other.vector_count; i++)
  182.         {
  183.             this->arr[i]=other.arr[i];
  184.         }
  185.         ++matrix_count;
  186.     }
  187.  
  188.     void operator = (const matrix &other)
  189.     {
  190.            this->vector_count=other.vector_count;
  191.             if(this->arr != nullptr)
  192.             {
  193.                 delete[] this->arr;
  194.             }
  195.         this->arr=new vector[other.vector_count];
  196.         for(int i=0; i<other.vector_count; i++)
  197.         {
  198.             this->arr[i]=other.arr[i];
  199.         }
  200.     }
  201.  
  202.     matrix &operator ++()
  203.     {
  204.         vector *temp=new vector[this->vector_count];
  205.         for(int i=0; i<this->vector_count; i++)
  206.         {
  207.             temp[i]=arr[i];
  208.         }
  209.         delete[] this->arr;
  210.         ++vector_count;
  211.         arr=new vector[vector_count];
  212.         for(int i=0; i<(this->vector_count)-1; i++)
  213.         {
  214.             arr[i]=temp[i];
  215.         }
  216.         delete[] temp;
  217.         return *this;
  218.     }
  219.     matrix &operator --()
  220.     {
  221.         vector *temp=new vector[this->vector_count];
  222.         for(int i=0; i<this->vector_count; i++)
  223.         {
  224.             temp[i]=arr[i];
  225.         }
  226.         delete[] this->arr;
  227.         --vector_count;
  228.         arr=new vector[vector_count];
  229.         for(int i=0; i<(this->vector_count)-1; i++)
  230.         {
  231.             arr[i]=temp[i];
  232.         }
  233.         delete[] temp;
  234.         return *this;
  235.     }
  236.  
  237.     bool operator !() const
  238.     {
  239.         if(this->vector_count != 0) return true;
  240.         else return false;
  241.     }
  242.  
  243.     matrix &operator~()
  244.     {
  245.         for(int i=0; i<this->vector_count; i++)
  246.         {
  247.             this->arr[i]=~(this->arr[i]);
  248.         }
  249.         return *this;
  250.     }
  251.  
  252.     matrix &operator-()
  253.     {
  254.         for(int i=0; i<this->vector_count; i++)
  255.         {
  256.             this->arr[i]=-(this->arr[i]);
  257.         }
  258.         return *this;
  259.     }
  260.     /*
  261.     matrix& operator+(const matrix &other)
  262.     {
  263.         for(int i=0; i<this->vector_count; i++)
  264.         {
  265.             this->arr[i] = this->arr[i]+other.arr[i];
  266.         }
  267.     return *this;
  268.     } */
  269.  
  270.     int getSize()
  271.     {
  272.         return vector_count;
  273.     }
  274.     friend ostream& operator<<(ostream &out, const matrix &other);
  275.     ~matrix(){--matrix_count; delete[] arr;}
  276. };
  277.  
  278. ostream &operator<<(ostream &outs, const matrix &other)
  279. {
  280.     for(int i=0; i<other.vector_count; i++)
  281.     {
  282.       outs << "n"<<other.arr[i];
  283.     }
  284.     return outs;
  285.  
  286. }
  287.  
  288. int main(int argc, char *argv[])
  289. {
  290.  vector obj1(4,4);
  291.  matrix obj2(4,5);
  292.  cout<<obj1;
  293.  cout<<"n"<<~obj1;
  294.  cout<<"n"<<obj2;
  295.  cout<<"n"<<(obj2);
  296.  cout<<"n"<<++obj2;
  297.  cout<<"n"<<(--obj2);
  298.  /*cout<<"n"<<obj2;
  299.  cout<<"n"<<++obj2;
  300.  cout<<"n"<<--obj2;
  301.  cout<<"nn"<<!obj2;
  302.   */
  303.  
  304.  return 0;
  305. }
  306.