Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <Windows.h>
- class OneD
- {
- friend class Matrix;
- private:
- int *one_d;
- int n;
- public:
- OneD(int n)
- {
- this->n = n;
- one_d = new int[n];
- }
- OneD(int* values, int n)
- {
- this->n = n;
- one_d = new int[n];
- for (int i = 0; i < this->n; i++)
- one_d[i] = values[i];
- }
- ~OneD(){delete[] one_d;}
- int GetN() {return n;}
- int& operator [](int i)
- {
- return one_d[i];
- }
- };
- class Matrix
- {
- private:
- int **matrix;
- int n, m;
- public:
- friend void GenElements(Matrix*);
- friend std::ostream& operator << (std::ostream&, const Matrix&);
- friend float CalcAvgSecondDiag(Matrix&);
- Matrix(int n, int m)
- {
- this->n = n;
- this->m = m;
- matrix = new int*[n];
- for (int i = 0; i < n; i++)
- {
- matrix[i] = new int[m];
- }
- GenElements(this);
- std::cout << "Конструктор с параметрами" << std::endl;
- }
- Matrix(const Matrix& copy)
- {
- n = copy.n;
- m = copy.m;
- matrix = new int*[n];
- for (int i = 0; i < n; i++)
- {
- matrix[i] = new int[m];
- for (int j = 0; j < m; j++)
- matrix[i][j] = copy.matrix[i][j];
- }
- std::cout << "Конструктор копии" << std::endl;
- }
- ~Matrix()
- {
- std::cout << "Деструктор" << std::endl;
- for (int i = 0; i < n; i++)
- delete[] matrix[i];
- delete[] matrix;
- }
- Matrix* operator &(int value)
- {
- int i, j, k;
- int temp;
- Matrix* new_m = new Matrix(*this);
- for (i = 0; i < this->n; i++)
- {
- for (j = 0; j < this->m; j++)
- {
- new_m->matrix[i][j] *= value;
- }
- }
- for (i = 0; i < value; i++)
- {
- for (j = new_m->m - 1; j > 0; j--)
- {
- for (k = 0; k < new_m->m - 1; k++)
- {
- temp = new_m->matrix[k][j];
- new_m->matrix[k][j] = new_m->matrix[k][j - 1];
- new_m->matrix[k][j-1] = temp;
- }
- }
- }
- return new_m;
- }
- Matrix* operator /(Matrix& m)
- {
- Matrix* X = new Matrix(this->n, this->m);
- for (int i = 0; i<this->n; i++)
- for (int j = 0; j < this->m; j++)
- {
- if (this->matrix[i][j] >= -m.matrix[i][j]) X->matrix[i][j] = this->matrix[i][j];
- else X->matrix[i][j] = -m.matrix[i][j];
- }
- return X;
- }
- int operator +(Matrix& m)
- {
- double S = 0;
- for (int i = 0; i<this->n; i++)
- for (int j = 0; j<this->m; j++)
- {
- S += this->matrix[i][j];
- S += m.matrix[i][j];
- }
- S /= (this->m*this->n) * 2;
- int K = 0;
- for (int i = 0; i<this->n; i++)
- for (int j = 0; j< this->m; j++)
- {
- if (S<this->matrix[i][j]) K += this->matrix[i][j];
- if (S<m.matrix[i][j]) K += m.matrix[i][j];
- }
- return K;
- }
- };
- void GenElements(Matrix* matrix)
- {
- for (int i = 0; i < matrix->n; i++)
- {
- for (int j = 0; j < matrix->m; j++)
- {
- matrix->matrix[i][j] = -5 + rand()%11;
- if (matrix->matrix[i][j] == 0) j--;
- }
- }
- }
- std::ostream& operator << (std::ostream& out, const Matrix& m)
- {
- int i, j;
- std::cout.setf(std::ios::right);
- for (i = 0; i < m.n; i++)
- {
- for (j = 0; j < m.m; j++)
- out << std::setw(4) << m.matrix[i][j] << " ";
- out << std::endl;
- }
- out << std::endl;
- std::cout.unsetf(std::ios::left);
- return out;
- }
- std::ostream& operator << (std::ostream& out, OneD& v)
- {
- std::cout.setf(std::ios::right);
- for (int i = 0; i < v.GetN(); i++)
- std::cout << v[i] << " ";
- out << std::endl;
- std::cout.unsetf(std::ios::left);
- return out;
- }
- void main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int N = 5, M = 5;
- Matrix A(N, M), B(N, M), C(N, M);
- std::cout << "Матрица А" << std::endl << A;
- std::cout << "Матрица B" << std::endl << B;
- std::cout << "Матрица C" << std::endl << C;
- std::cout << "C + C" << std::endl;
- int value = C + C;
- std::cout << value << std::endl << std::endl;
- std::cout << "B & (C + C)" << std::endl;
- Matrix* D = B & value;
- std::cout << *D;
- std::cout << "A / B & (C + C)" << std::endl;
- Matrix* E = A / *D;
- std::cout << *E;
- std::cout << "B / A / B & (C + C)" << std::endl;
- Matrix* F = B / *E;
- std::cout << *F;
- std::cout << "C + (B / A / B & (C + C))" << std::endl;
- std::cout << (C + *F) << std::endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement