// 左移运算符重载 std::ostream &operator<<(std::ostream &out, const matrix &p) { out << "The order of a matrix is " << p.N << std::endl; out << "The matrix is as follows " << std::endl; for (int i = 1; i <= p.N; i++) { out << "|" << ' '; for (int j = 1; j <= p.N; j++) { out << p.a[i][j] << " "; } out << "|" << " "; out << std::endl; } out << "----------------------------------------------------" << std::endl; //分隔符 return out; //返回out以便实现无限追加 }
// 右移运算符重载 std::istream &operator>>(std::istream &cin, matrix &p) { std::cout << "please input your matrix order :" << std::endl; cin >> p.N; int n = p.N; p.a = std::vector<std::vector<int>>(n + 1, std::vector<int>(n + 1, 0)); std::cout << "plaese input your matrix :" << std::endl; for (int i = 1; i <= n; i++) { std::cout << "No." << i << ' ' << "row is:" << std::endl; for (int j = 1; j <= n; j++) { cin >> p.a[i][j]; } } return cin; }
matrix::matrix(int n) { this->N = n; this->a = std::vector<std::vector<int>>(n + 1, std::vector<int>(n + 1, 0)); for (int i = 1; i <= n; i++) { this->a[i][i] = 1; } } // 重载等于运算符 bool matrix::operator==(const matrix& A) { int n = this->N; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (this->a[i][j] != A.a[i][j]) { return0; } } } return1; }
// 重载不等于号 bool matrix::operator!=(const matrix& A) { int n = this->N; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (this->a[i][j] != A.a[i][j]) { return1; } } } return0; }
// 重载赋值运算符 matrix& matrix::operator=(const matrix& A) { this->N = A.N; int n = this->N; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { this->a[i][j] = A.a[i][j]; } } return *this; }
// 重载+=运算符 matrix& matrix::operator+=(const matrix& B) { int n = this->N; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { this->a[i][j] = this->a[i][j] + B.a[i][j]; } } return *this; }
// 重载-=运算符 matrix& matrix::operator-=(const matrix& B) { int n = this->N; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { this->a[i][j] = this->a[i][j] - B.a[i][j]; } } return *this; }
// 重载*=运算符 matrix& matrix::operator*=(const matrix& B) { matrix temp(B.N); int n = this->N; for (int i = 1; i <= n; i++) { temp.a[i][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { temp.a[i][j] += this->a[i][k] * B.a[k][j]; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { this->a[i][j] = temp.a[i][j]; } } return *this; }
// 矩阵加法实现 matrix operator+(const matrix& A, const matrix& B) { matrix temp(A.N); for (int i = 1; i <= A.N; i++) { for (int j = 1; j <= A.N; j++) { temp.a[i][j] = A.a[i][j] + B.a[i][j]; } } return temp; }
// 矩阵减法实现 matrix operator-(const matrix& A, const matrix& B) { matrix temp(A.N); for (int i = 1; i <= A.N; i++) { for (int j = 1; j <= A.N; j++) { temp.a[i][j] = A.a[i][j] - B.a[i][j]; } } return temp; }
// 矩阵乘法实现 matrix operator*(const matrix& A, const matrix& B) { matrix temp(A.N); for (int i = 1; i <= A.N; i++) { temp.a[i][i] = 0; } for (int i = 1; i <= A.N; i++) { for (int j = 1; j <= A.N; j++) { for (int k = 1; k <= A.N; k++) { temp.a[i][j] += A.a[i][k] * B.a[k][j]; } } } return temp; }
// 矩阵数乘实现 matrix operator*(const matrix& A, constint& B) { matrix temp(A.N); for (int i = 1; i <= A.N; i++) { for (int j = 1; j <= A.N; j++) { temp.a[i][j] = B * A.a[i][j]; } } return temp; }
// 左移运算符重载 std::ostream& operator<<(std::ostream& out, const matrix& p) { out << "The order of a matrix is " << p.N << std::endl; out << "The matrix is as follows " << std::endl; for (int i = 1; i <= p.N; i++) { out << "|" << ' '; for (int j = 1; j <= p.N; j++) { out << p.a[i][j] << " "; } out << "|" << " "; out << std::endl; } out << "----------------------------------------------------" << std::endl; return out; }
// 右移运算符重载 std::istream& operator>>(std::istream& cin, matrix& p) { std::cout << "please input your matrix order :" << std::endl; cin >> p.N; int n = p.N; p.a = std::vector<std::vector<int>>(n + 1, std::vector<int>(n + 1, 0)); std::cout << "plaese input your matrix :" << std::endl; for (int i = 1; i <= n; i++) { std::cout << "No." << i << ' ' << "row is:" << std::endl; for (int j = 1; j <= n; j++) { cin >> p.a[i][j]; } } return cin; }
#include<iostream> #include<string> #include"Matrix.h" intmain() { std::cout << "Now let us start our testing phase" << std::endl; std::cout << "ouput an identity matrix" << std::endl; matrix a(2); std::cout << a; std::cout << "Enter two test matrices" << std::endl; std::cin >> a; std::cout << a; matrix b(2); std::cin >> b; std::cout << b; std::cout << "Enter the phase of checking addition, subtraction and multiplication of the two matrices" << std::endl; matrix c = a + b; std::cout << c << std::endl; c = a - b; std::cout << c << std::endl; c = a * b; std::cout << c << std::endl; std::cout << "Next is the scalar multiplication"; int x = 2; c = a * x; std::cout << c << std::endl; std::cout << "Enter the phase of comparing whether the two matrices are equal or not " << std::endl; if (a == b) { std::cout << "Matrix a is equal to matrix b" << std::endl; } else { std::cout << "Matrix a is unequal to matrix b" << std::endl; } if (a != b) { std::cout << "Matrix a is unequal to matrix b" << std::endl; } else { std::cout << "Matrix a is equal to matrix b" << std::endl; } std::cout << "Proceed to +=, -=, *= part" << std::endl; a += b; std::cout << a << std::endl; a -= b; std::cout << a << std::endl; std::cout << b << std::endl; a *= b; std::cout << a << std::endl; std::cout << "Finally, verify if chain addition is possible" << std::endl; std::cout << a + b + a << std::endl; return0; }
Now let us start our testing phase ouput an identity matrix The order of a matrix is 2 The matrix is as follows | 10 | | 01 | ---------------------------------------------------- Enter two test matrices please input your matrix order : 3 plaese input your matrix : No.1 row is: 123 No.2 row is: 456 No.3 row is: 789 The order of a matrix is 3 The matrix is as follows | 123 | | 456 | | 789 | ---------------------------------------------------- please input your matrix order : 3 plaese input your matrix : No.1 row is: 456 No.2 row is: 123 No.3 row is: 367 The order of a matrix is 3 The matrix is as follows | 456 | | 123 | | 367 | ---------------------------------------------------- Enter the phase of checking addition, subtraction and multiplication of the two matrices The order of a matrix is 3 The matrix is as follows | 579 | | 579 | | 101416 | ----------------------------------------------------
The order of a matrix is 3 The matrix is as follows | -3-3-3 | | 333 | | 422 | ----------------------------------------------------
The order of a matrix is 3 The matrix is as follows | 152733 | | 396681 | | 63105129 | ----------------------------------------------------
Next is the scalar multiplicationThe order of a matrix is 3 The matrix is as follows | 246 | | 81012 | | 141618 | ----------------------------------------------------
Enter the phase of comparing whether the two matrices are equal ornot Matrix a is unequal to matrix b Matrix a is unequal to matrix b Proceed to +=, -=, *= part The order of a matrix is 3 The matrix is as follows | 579 | | 579 | | 101416 | ----------------------------------------------------
The order of a matrix is 3 The matrix is as follows | 123 | | 456 | | 789 | ----------------------------------------------------
The order of a matrix is 3 The matrix is as follows | 456 | | 123 | | 367 | ----------------------------------------------------
The order of a matrix is 3 The matrix is as follows | 152733 | | 396681 | | 63105129 | ----------------------------------------------------
Finally, verify if chain addition is possible The order of a matrix is 3 The matrix is as follows | 345972 | | 79134165 | | 129216265 | ----------------------------------------------------