定义一个矩阵类 Matrix,均为 M 行 N 列,通过重载运算符“+”、“-”,“<<”,“>>”,“++”,“--”,“==”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。
时间: 2023-11-05 09:17:03 浏览: 73
下面是一个简单的 Matrix 类的定义和重载运算符的实现:
```c++
#include <iostream>
using namespace std;
class Matrix {
private:
int **data; // 矩阵数据
int rows, cols; // 行数和列数
public:
Matrix(int r, int c) : rows(r), cols(c) {
data = new int*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new int[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = 0;
}
}
}
Matrix(const Matrix &other) { // 拷贝构造函数
rows = other.rows;
cols = other.cols;
data = new int*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new int[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = other.data[i][j];
}
}
}
~Matrix() { // 析构函数
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
Matrix& operator=(const Matrix &other) { // 赋值运算符
if (this == &other) {
return *this;
}
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
rows = other.rows;
cols = other.cols;
data = new int*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new int[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = other.data[i][j];
}
}
return *this;
}
Matrix operator+(const Matrix &other) const { // 矩阵相加
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("Matrix sizes do not match.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix &other) const { // 矩阵相减
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("Matrix sizes do not match.");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] - other.data[i][j];
}
}
return result;
}
Matrix& operator++() { // 矩阵自增
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j]++;
}
}
return *this;
}
Matrix operator++(int) { // 矩阵后置自增
Matrix result(*this);
++(*this);
return result;
}
Matrix& operator--() { // 矩阵自减
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j]--;
}
}
return *this;
}
Matrix operator--(int) { // 矩阵后置自减
Matrix result(*this);
--(*this);
return result;
}
bool operator==(const Matrix &other) const { // 矩阵相等
if (rows != other.rows || cols != other.cols) {
return false;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] != other.data[i][j]) {
return false;
}
}
}
return true;
}
bool operator!=(const Matrix &other) const { // 矩阵不等
return !(*this == other);
}
friend ostream& operator<<(ostream &out, const Matrix &matrix) { // 矩阵输出
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++) {
out << matrix.data[i][j] << " ";
}
out << endl;
}
return out;
}
friend istream& operator>>(istream &in, Matrix &matrix) { // 矩阵输入
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++) {
in >> matrix.data[i][j];
}
}
return in;
}
};
```
使用示例:
```c++
int main() {
Matrix a(2, 2), b(2, 2);
cin >> a >> b;
cout << a + b << endl;
cout << a - b << endl;
cout << ++a << endl;
cout << b++ << endl;
cout << (a == b) << endl;
cout << (a != b) << endl;
return 0;
}
```