标签:
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 class Matrix 8 { 9 private: 10 int s; 11 int arr[110][110]; 12 public: 13 Matrix(int); 14 Matrix operator+(Matrix &it); 15 Matrix operator-(Matrix &it); 16 Matrix operator*(Matrix &it); 17 friend istream & operator>>(istream &is, Matrix &it); 18 friend ostream & operator<<(ostream &os,const Matrix &it); 19 }; 20 Matrix::Matrix(int n=0) 21 { 22 s = n; 23 } 24 Matrix Matrix::operator+(Matrix &it) 25 { 26 Matrix ans(s); 27 for (int i = 1; i <= it.s; i++) 28 for (int j = 1; j <= it.s; j++) 29 ans.arr[i][j] = arr[i][j] + it.arr[i][j]; 30 return ans; 31 } 32 Matrix Matrix::operator-(Matrix &it) 33 { 34 Matrix ans(s); 35 for (int i = 1; i <= it.s; i++) 36 for (int j = 1; j <= it.s; j++) 37 ans.arr[i][j] = arr[i][j] - it.arr[i][j]; 38 return ans; 39 } 40 Matrix Matrix::operator*(Matrix &it) 41 { 42 Matrix ans(s); 43 for (int i = 1; i <= it.s; i++) 44 for (int j = 1; j <= it.s; j++) 45 { 46 ans.arr[i][j] = 0; 47 for (int k = 1; k <= it.s; k++) 48 ans.arr[i][j] += arr[i][k] * it.arr[k][j]; 49 } 50 return ans; 51 } 52 istream & operator>>(istream &is, Matrix &it) 53 { 54 for (int i = 1; i <= it.s; i++) 55 for (int j = 1; j <= it.s; j++) 56 is >> it.arr[i][j]; 57 return is; 58 } 59 ostream & operator<<(ostream &os,const Matrix &it) 60 { 61 for (int i = 1; i <= it.s; i++) 62 { 63 for (int j = 1; j <= it.s; j++) 64 os << it.arr[i][j] << " "; 65 cout << endl; 66 } 67 return os; 68 } 69 int main() 70 { 71 int n; cin >> n; 72 Matrix a(n), b(n); 73 cin >> a >> b; 74 cout << a*b; 75 system("pause"); 76 return 0; 77 }
重复造了个矩阵加减乘的轮子,真是累
标签:
原文地址:http://www.cnblogs.com/anilop/p/5597041.html