Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Matrix{
- public:
- int N, M;
- int **Arr;
- Matrix(int N, int M){
- this -> N = N;
- this -> M = M;
- this -> Arr = new int* [N + 1];
- for(int i = 1 ; i <= N ; i++){
- this -> Arr[i] = new int[M + 1];
- memset(this -> Arr[i], 0, sizeof(this -> Arr[i]));
- }
- }
- friend ostream & operator << (ostream &out, const Matrix *c);
- friend istream & operator >> (istream &in, Matrix *c);
- Matrix operator = (Matrix other){
- for(int i = 1 ; i <= this -> N ; i++){
- for(int j = 1 ; j <= this -> M ; j++){
- this -> Arr[i][j] = other.Arr[i][j];
- }
- }
- }
- Matrix operator + (Matrix RHS){
- Matrix *temp = new Matrix(this -> N, RHS.M);
- for(int i = 1 ; i <= this -> N ; i++){
- for(int j = 1 ; j <= this -> M ; j++){
- temp -> Arr[i][j] = RHS.Arr[i][j] + Arr[i][j];
- }
- }
- return *temp;
- }
- Matrix operator - (Matrix RHS){
- Matrix *temp = new Matrix(this -> N, this -> M);
- for(int i = 1 ; i <= this -> N ; i++){
- for(int j = 1 ; j <= this -> M ; j++){
- temp -> Arr[i][j] = Arr[i][j] - RHS.Arr[i][j];
- }
- }
- return *temp;
- }
- Matrix operator * (Matrix RHS){
- Matrix *temp = new Matrix(this -> N, RHS.M);
- for(int i = 1 ; i <= this -> N ; i++){
- for(int j = 1 ; j <= RHS.M ; j++){
- for(int k = 1 ; k <= this -> M ; k++){
- temp -> Arr[i][j] += this -> Arr[i][k] * RHS.Arr[k][j];
- }
- }
- }
- return *temp;
- }
- Matrix operator / (Matrix RHS){
- Matrix *temp = new Matrix(this -> M, this -> N);
- for(int i = 1 ; i <= this -> N ; i++){
- for(int j = 1 ; j <= this -> M ; j++){
- temp -> Arr[j][i] = RHS.Arr[i][j];
- }
- }
- return *temp;
- }
- };
- ostream & operator << (ostream &out, Matrix &c)
- {
- for(int i = 1 ; i <= c.N ; i++){
- out << c.Arr[i][1];
- for(int j = 2 ; j <= c.M ; j++){
- out << ' ' << c.Arr[i][j];
- }
- out << endl;
- }
- return out;
- }
- istream & operator >> (istream &in, Matrix &c)
- {
- for(int i = 1 ; i <= c.N ; i++){
- for(int j = 1 ; j <= c.M ; j++){
- in >> c.Arr[i][j];
- }
- }
- return in;
- }
- class square: public Matrix{
- public:
- square(int n) : Matrix(n, n){}
- Matrix operator = (Matrix rhs)
- {
- return Matrix::operator=(rhs);
- }
- };
- int main()
- {
- int n;
- cin >> n;
- square A(n);
- cin >> A;
- cin >> n;
- square B(n);
- cin >> B;
- cin >> n;
- square C(n);
- cin >> C;
- square D(n);
- D = A + B;
- cout << D;
- square E(n);
- E = B - A;
- cout << E;
- square F(n);
- F = C * A;
- cout << F;
- square G(n);
- G = A / A;
- cout << G;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement