Advertisement
Dantenerosas

Untitled

Apr 27th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <Windows.h>
  4.  
  5. class OneD
  6. {
  7. friend class Matrix;
  8. private:
  9.     int *one_d;
  10.     int n;
  11. public:
  12.     OneD(int n)
  13.     {
  14.         this->n = n;
  15.         one_d = new int[n];
  16.     }
  17.     OneD(int* values, int n)
  18.     {
  19.         this->n = n;
  20.         one_d = new int[n];
  21.         for (int i = 0; i < this->n; i++)
  22.             one_d[i] = values[i];
  23.     }
  24.     ~OneD(){delete[] one_d;}
  25.     int GetN() {return n;}
  26.     int& operator [](int i)
  27.     {
  28.         return one_d[i];
  29.     }
  30. };
  31.  
  32.  
  33. class Matrix
  34. {
  35. private:
  36.     int **matrix;
  37.     int n, m;
  38. public:
  39.     friend void GenElements(Matrix*);
  40.     friend std::ostream& operator << (std::ostream&, const Matrix&);
  41.     friend float CalcAvgSecondDiag(Matrix&);
  42.     Matrix(int n, int m)
  43.     {
  44.         this->n = n;
  45.         this->m = m;
  46.         matrix = new int*[n];
  47.  
  48.         for (int i = 0; i < n; i++)
  49.         {
  50.             matrix[i] = new int[m];
  51.         }
  52.         GenElements(this);
  53.         std::cout << "Конструктор с параметрами" << std::endl;
  54.     }
  55.  
  56.     Matrix(const Matrix& copy)
  57.     {
  58.         n = copy.n;
  59.         m = copy.m;
  60.         matrix = new int*[n];
  61.         for (int i = 0; i < n; i++)
  62.         {
  63.             matrix[i] = new int[m];
  64.             for (int j = 0; j < m; j++)
  65.                 matrix[i][j] = copy.matrix[i][j];
  66.         }
  67.         std::cout << "Конструктор копии" << std::endl;
  68.     }
  69.  
  70.     ~Matrix()
  71.     {
  72.         std::cout << "Деструктор" << std::endl;
  73.         for (int i = 0; i < n; i++)
  74.             delete[] matrix[i];
  75.         delete[] matrix;
  76.     }
  77.  
  78.     Matrix* operator &(int value)
  79.     {
  80.         int i, j, k;
  81.         int temp;
  82.         Matrix* new_m = new Matrix(*this);
  83.         for (i = 0; i < this->n; i++)
  84.         {
  85.             for (j = 0; j < this->m; j++)
  86.             {
  87.                 new_m->matrix[i][j] *= value;
  88.             }
  89.         }
  90.         for (i = 0; i < value; i++)
  91.         {
  92.             for (j = new_m->m - 1; j > 0; j--)
  93.             {
  94.                 for (k = 0; k < new_m->m - 1; k++)
  95.                 {
  96.                     temp = new_m->matrix[k][j];
  97.                     new_m->matrix[k][j] = new_m->matrix[k][j - 1];
  98.                     new_m->matrix[k][j-1] = temp;
  99.                 }
  100.             }
  101.         }
  102.         return new_m;
  103.     }
  104.     Matrix* operator /(Matrix& m)
  105.     {
  106.         Matrix* X = new Matrix(this->n, this->m);
  107.         for (int i = 0; i<this->n; i++)
  108.             for (int j = 0; j < this->m; j++)
  109.             {
  110.                 if (this->matrix[i][j] >= -m.matrix[i][j]) X->matrix[i][j] = this->matrix[i][j];
  111.                 else X->matrix[i][j] = -m.matrix[i][j];
  112.             }
  113.         return X;
  114.     }
  115.     int operator +(Matrix& m)
  116.     {
  117.         double S = 0;
  118.         for (int i = 0; i<this->n; i++)
  119.             for (int j = 0; j<this->m; j++)
  120.             {
  121.                 S += this->matrix[i][j];
  122.                 S += m.matrix[i][j];
  123.             }
  124.         S /= (this->m*this->n) * 2;
  125.         int K = 0;
  126.         for (int i = 0; i<this->n; i++)
  127.             for (int j = 0; j< this->m; j++)
  128.             {
  129.                 if (S<this->matrix[i][j]) K += this->matrix[i][j];
  130.                 if (S<m.matrix[i][j]) K += m.matrix[i][j];
  131.             }
  132.         return K;
  133.     }
  134. };
  135.  
  136.  
  137. void GenElements(Matrix* matrix)
  138. {
  139.     for (int i = 0; i < matrix->n; i++)
  140.     {
  141.         for (int j = 0; j < matrix->m; j++)
  142.         {
  143.             matrix->matrix[i][j] = -5 + rand()%11;
  144.             if (matrix->matrix[i][j] == 0) j--;
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150. std::ostream& operator << (std::ostream& out, const Matrix& m)
  151. {
  152.     int i, j;
  153.     std::cout.setf(std::ios::right);
  154.     for (i = 0; i < m.n; i++)
  155.     {
  156.         for (j = 0; j < m.m; j++)
  157.             out << std::setw(4) << m.matrix[i][j] << "  ";
  158.         out << std::endl;
  159.     }
  160.     out << std::endl;
  161.     std::cout.unsetf(std::ios::left);
  162.     return out;
  163.  
  164. }
  165.  
  166.  
  167. std::ostream& operator << (std::ostream& out, OneD& v)
  168. {
  169.     std::cout.setf(std::ios::right);
  170.     for (int i = 0; i < v.GetN(); i++)
  171.         std::cout << v[i] << " ";
  172.     out << std::endl;
  173.     std::cout.unsetf(std::ios::left);
  174.     return out;
  175. }
  176.  
  177.  
  178. void main()
  179. {
  180.     SetConsoleCP(1251);
  181.     SetConsoleOutputCP(1251);
  182.     int N = 5, M = 5;
  183.     Matrix A(N, M), B(N, M), C(N, M);
  184.     std::cout << "Матрица А" << std::endl << A;
  185.     std::cout << "Матрица B" << std::endl << B;
  186.     std::cout << "Матрица C" << std::endl << C;
  187.     std::cout << "C + C" << std::endl;
  188.     int value = C + C;
  189.     std::cout << value << std::endl << std::endl;
  190.     std::cout << "B & (C + C)" << std::endl;
  191.     Matrix* D = B & value;
  192.     std::cout << *D;
  193.     std::cout << "A / B & (C + C)" << std::endl;
  194.     Matrix* E = A / *D;
  195.     std::cout << *E;
  196.     std::cout << "B / A / B & (C + C)" << std::endl;
  197.     Matrix* F = B / *E;
  198.     std::cout << *F;
  199.     std::cout << "C + (B / A / B & (C + C))" << std::endl;
  200.     std::cout << (C + *F) << std::endl;
  201.     system("pause");
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement