添加减法。

This commit is contained in:
unlockable
2023-04-01 14:13:55 +08:00
parent e12c94fcec
commit 8ddf7735d1

View File

@@ -23,11 +23,12 @@ public:
Matrix &operator=(Matrix &otherMatrix);
Matrix &operator+(Matrix &otherMatrix);
bool operator==(Matrix &otherMatrix);
Matrix &operator-(Matrix &otherMatrix);
int getRowSize();
int getColumnSize();
Matrix &transformation();
Matrix &transpose();
};
Matrix::Matrix() {
@@ -159,7 +160,7 @@ int Matrix::getColumnSize() {
return this->columnSize;
}
Matrix &Matrix::transformation() {
Matrix &Matrix::transpose() {
Matrix *result = new Matrix(this->columnSize, this->rowSize);
for (int r = 0; r < this->rowSize; r++) {
for (int c = 0; c < this->columnSize; c++) {
@@ -213,6 +214,10 @@ Matrix& operator*(int lambda, Matrix& mat) {
return (*result);
}
Matrix &Matrix::operator-(Matrix &otherMatrix) {
return (*this) + (-1)*otherMatrix;
}
int main() {
int rowSize, columnSize;
std::cout << "Input the size of the matrix: m x n" << std::endl;
@@ -221,14 +226,15 @@ int main() {
Matrix b;
std::cout << a << b;
std::cin >> a;
b = a.transformation();
b = a.transpose();
Matrix c = a * b;
// clang-format off
std::cout << "a =\n" << a
<< "b =\n" << b
<< "a - a*3\n" << a - a*3
<< "a * 3 =\n" << a * 3
<< "a * b =\n"<< a * b
<< "a * aT =\n" << a * a.transformation()
<< "a * aT =\n" << a * a.transpose()
<< "a * a =\n" << a * a
<< "a * b == c: " << (a * b == c) << std::endl
<< "a == c: " << (a == c)