Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- #include <stdlib.h>
- #include <conio.h>
- #include <time.h>
- #include <string>
- using namespace std;
- struct cmplx
- {
- double r;
- double i;
- };
- class Matrix
- {
- private:
- cmplx** a;
- int x;
- int y;
- string name;
- friend void cmPrint(cmplx a);
- public:
- //static int n;
- int n;
- Matrix()
- {
- x=3;
- y=3;
- cmplx** a=new cmplx*[y];
- for (int i = 0; i < y; i++)
- a[i] = new cmplx[x];
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- {
- am(i, j).r = 0;
- am(i, j).i = 0;
- }
- name = "Матрица";
- name += n;
- n++;
- cout << endl << name;
- cout<<endl<<"DefaultConstructor";
- }
- Matrix(int i, int j)
- {
- x=j;
- y=i;
- cmplx** a = new cmplx*[y];
- for (int i = 0; i < y; i++)
- a[i] = new cmplx[x];
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- {
- am(i, j).r = 0;
- am(i, j).i = 0;
- }
- name = "Матрица";
- name += n;
- n++;
- cout << endl << name;
- cout<<endl<<"CustomConstructor";
- }
- Matrix(Matrix &b)
- {
- x=b.x;
- y=b.y;
- cmplx** a=new cmplx*[y];
- for (int i = 0; i < y; i++)
- a[i]=new cmplx[x];
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- am(i, j) = b.am(i, j);
- name = "Матрица";
- name += n;
- n++;
- cout << endl << name;
- cout<<endl<<"CopyConstructor";
- }
- ~Matrix()
- {
- for (int i = 0; i < y; i++)
- delete [] a[i];
- delete [] a;
- cout<<endl<<name<<" destructor";
- }
- void matrixIn()
- {
- cout << endl << "Ready to enter matrix. It is COMPLEX, don't forget Press any button to continue" << endl;
- _getch();
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- {
- cin >> am(i, j).r;
- cin >> am(i, j).i;
- }
- }
- void print()
- {
- cout << endl << "PrintingOnScreen:" <<name<< endl;
- for (int i = 0; i < y; i++)
- {
- cout << endl;
- for (int j = 0; j < x; j++)
- cmPrint(am(i, j));
- }
- }
- void print(int y1, int x1)
- {
- cout << endl << "PrintingOnScreen:" <<name<< endl;
- for (int i = 0; i < y1; i++)
- {
- cout << endl;
- for (int j = 0; j < x1; j++)
- cmPrint(am(i, j));
- }
- }
- bool isEqual(Matrix b)
- {
- if(x!=b.x) return false;
- if(y!=b.y) return false;
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- {
- if (am(i, j).r != b.am(i, j).r)return false;
- if (am(i, j).i != b.am(i, j).i)return false;
- }
- return true;
- }
- Matrix matrixMp(Matrix t)
- {
- int y1 = y;
- int x1 = t.x;
- Matrix b(y1, x1);
- for (int i = 0; i < y1; i++)
- for (int j = 0; j < x1; j++)
- for (int k = 0; k < x; k++)
- {
- b.am(i,j).r += t.am(i,k).r * am(k,j).r - t.am(i,k).i * am(k,j).i;
- b.am(i,j).i += t.am(i, k).r * am(k,j).i + t.am(i,k).i * am(k,j).r;
- }
- return b;
- }
- Matrix matrixSum(Matrix t)
- {
- //size
- int y1 = y;
- int x1 = x;
- Matrix b(y1, x1);
- for (int i = 0; i < y1; i++)
- for (int j = 0; j < x1; j++)
- {
- b.am(i, j).r += am(i, j).r + t.am(i, j).r;
- b.am(i, j).i += am(i, j).i + t.am(i, j).i;
- }
- return b;
- }
- Matrix matrixMin(Matrix t)
- {
- //size
- int y1 = y;
- int x1 = x;
- Matrix b(y1, x1);
- for (int i = 0; i < y1; i++)
- for (int j = 0; j < x1; j++)
- {
- b.am(i, j).r += am(i, j).r - t.am(i, j).r;
- b.am(i, j).i += am(i, j).i - t.am(i, j).i;
- }
- return b;
- }
- void intSum(cmplx t)
- {
- int min;
- if (x < y) min = x;
- else min = y;
- for (int i = 0; i < min; i++)
- {
- am(i, i).r = am(i, i).r + t.i;
- am(i, i).i = am(i, i).i + t.r;
- }
- }
- void intMin(cmplx t)
- {
- t.r = t.r*(-1);
- t.i = t.i*(-1);
- intSum(t);
- }
- void intMp(cmplx t)
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- {
- am(i, j).r = am(i, j).r * t.r;
- am(i, j).i = am(i, j).i * t.i;
- }
- }
- void intDiv(cmplx t)
- {
- t.r = 1 / t.r;
- t.i = 1 / t.i;
- intMp(t);
- }
- Matrix transp()
- {
- Matrix t;
- t.x = y;
- t.y = x;
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- t.am(j, i) = am(i, j);
- return t;
- }
- void selfTr()
- {
- Matrix t = transp();
- *this = t;
- }
- void matrixPow(int p)
- {
- Matrix d = (*this);
- int i = 0;
- while (i < p)
- {
- Matrix t = matrixMp(d);
- i++;
- (*this) = t;
- }
- }
- bool isSqua()
- {
- if (x == y) return true;
- else return false;
- }
- bool isDig()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if ((am(i, j).r != 0) && (am(i, j).i != 0) && (i != j))
- return false;
- return true;
- }
- bool isNull()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if ((am(i, j).r != 0) && (am(i, j).i != 0))
- return false;
- return true;
- }
- bool isUn()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if (((am(i, j).r != 0) && (am(i, j).i != 0) && (i != j)) || ((i == j) && ((am(i, j).r != 1) && (am(i, j).i != 0))))
- return false;
- return true;
- }
- bool isSim()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if ((am(i, j).r != am(j, i).r) && (am(i, j).i != am(j, i).i) && (i != j))
- return false;
- return true;
- }
- bool isUpTr()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if ((j<i) && (am(i, j).r != 0) && (am(i, j).i != 0))
- return false;
- return true;
- }
- bool isDwTr()
- {
- for (int i = 0; i < y; i++)
- for (int j = 0; j < x; j++)
- if ((j>i) && (am(i, j).r != 0) && (am(i, j).i != 0))
- return false;
- return true;
- }
- cmplx& am(int i, int j)
- {
- if (i >= y)
- {
- if (j >= x)
- {
- cout << endl << "Out of range...";
- return a[y][x];
- }
- if (j < 0)
- {
- cout << endl << "Out of range...";
- return a[y][0];
- }
- }
- if (i < 0)
- {
- if (j >= x)
- {
- cout << endl << "Out of range...";
- return a[0][x];
- }
- if (j < 0)
- {
- cout << endl << "Out of range...";
- return a[0][0];
- }
- }
- return a[i][j];
- }
- void operator=(Matrix &b)
- {
- x=b.x;
- y=b.y;
- for (int i = 0; i < x; i++)
- for (int j = 0; j < y; j++)
- am(i,j)=b.am(i,j);
- }
- void operator+=(Matrix &b)
- {
- *this=matrixSum(b);
- }
- void operator-=(Matrix &b)
- {
- *this=matrixMin(b);
- }
- void operator*=(Matrix &b)
- {
- *this=matrixMp(b);
- }
- void operator+=(cmplx b)
- {
- intSum(b);
- }
- void operator-=(cmplx b)
- {
- intMin(b);
- }
- void operator*=(cmplx b)
- {
- intMp(b);
- }
- void operator/=(cmplx b)
- {
- intDiv(b);
- }
- };
- void cmPrint(cmplx a)
- {
- cout << a.r;
- cout << " ";
- cout << a.i;
- cout << "i ";
- }
- int main()
- {
- //Matrix::n = 0;
- cout << "Push the temple" << endl;
- _getch();
- cout << "Hello! Please, enter dimensions ";
- int y, x;
- cin >> y;
- cin >> x;
- Matrix a(y, x);
- a.matrixIn();
- cout << endl;
- a.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement