Last active
June 23, 2018 13:24
-
-
Save friendleemachine/ae04d23ed8fdaa4bb3576748df879474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
class Matrix { | |
private: | |
int **arr; | |
int n , m; | |
public: | |
Matrix(int rows, int cols): n(rows), m(cols) { | |
allocatemtx(); | |
for (int i = 0; i < m; i++) | |
for (int j = 0; j < n; j++) | |
arr[i][j] = 0; | |
} | |
~Matrix() { | |
for (int i = 0; i < n; i++) | |
delete arr[i]; | |
delete arr; | |
} | |
friend istream& operator>>(istream& in, const Matrix& mtx) { | |
for (int i = 0; i < mtx.m; i++) | |
for (int j = 0; j < mtx.n; j++) | |
in >> mtx.arr[i][j]; | |
return in; | |
} | |
friend ostream& operator<<(ostream& out, const Matrix& mtx) { | |
for (int i = 0; i < mtx.m; i++) { | |
out << mtx.arr[i][0]; | |
for (int j = 1; j < mtx.n; j++) | |
out << " " << mtx.arr[i][j]; | |
out << endl; | |
} | |
return out; | |
} | |
int& operator()(const int a, const int b){ | |
if (a < n && b < m) | |
return arr[a][b]; | |
} | |
Matrix& operator=(const Matrix& mtx){ | |
if (this == &mtx) { | |
return *this; | |
} | |
if (m != mtx.m || n != mtx.n) { | |
for (int i = 0; i < m; i++) { | |
delete[] arr[i]; | |
} | |
delete[] arr; | |
m = mtx.m; | |
n = mtx.n; | |
allocatemtx(); | |
} | |
for (int i = 0; i < m; i++) { | |
for (int j = 0; j < n; j++) { | |
arr[i][j] = mtx.arr[i][j]; | |
} | |
} | |
return *this; | |
} | |
Matrix& operator+(const Matrix& mtx) { | |
Matrix temp(*this); | |
return temp += mtx; | |
} | |
Matrix& operator-(const Matrix& mtx) { | |
Matrix temp(*this); | |
return temp -= mtx; | |
} | |
Matrix& operator+=(const Matrix& mtx) { | |
if(n == mtx.n && m == mtx.m) { | |
for (int i = 0; i < n; i++) | |
for (int j = 0; j < m + 1; j++) | |
arr[i][j] += mtx.arr[i][j]; | |
return *this; | |
} | |
} | |
Matrix& operator-=(const Matrix& mtx) { | |
if(n == mtx.n && m == mtx.m) { | |
for (int i = 0; i < n; i++) | |
for (int j = 0; j < m + 1; j++) | |
arr[i][j] -= mtx.arr[i][j]; | |
return *this; | |
} | |
} | |
Matrix& operator*=(const int num) { | |
for (int i = 0; i < m; i++) | |
for (int j = 0; j < n; j++) | |
arr[i][j] *= num; | |
return *this; | |
} | |
void dot(Matrix& mtx) { | |
if(this->n == mtx.m && this->m == mtx.n) { | |
mtx.transpose(); | |
for (int i = 0; i < m; i++) | |
for (int j = 0; j < n; j++) | |
arr[i][j] *= mtx.arr[i][j]; | |
} | |
} | |
void transpose() { | |
Matrix trans(m, n); | |
for (int i = 0; i < n; i++) | |
for (int j = 0; j < m; j++) | |
trans.arr[i][j] = this->arr[j][i]; | |
cout << endl; | |
*this = trans; | |
} | |
void allocatemtx() { | |
arr = new int*[m]; | |
for (int i = 0; i < m; i++) { | |
arr[i] = new int[n]; | |
} | |
} | |
}; | |
int main() { | |
int n, m; | |
fstream fin("in.txt"); | |
fin >> n >> m; | |
Matrix a(n, m); | |
fin >> a; | |
fin.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment