编写复数矩阵类,利用C++运算符重载机制实现二维复数矩阵(非方阵)的相关运算,并在主函数测试重载后的运算符功能。具体要求如下: (1) 使用+与-号实现两个复数矩阵的加减法; (2) 使用*实现两个复数矩阵的乘法; (3) 使用!实现复数矩阵的共轭转置操作; (4) 使用<<与>>实现复数矩阵的输出与输入; (5) 要求兼容实数矩阵运算; (6) 使用动态数组生成方式(使用new运算符创建数组,矩阵维度可通过变量赋值/用户交互输入)。
时间: 2024-03-19 20:46:06 浏览: 62
C++矩阵类的编写,基于运算符重载
下面是一个简单的复数矩阵类的实现,其中重载了 +、-、*、!、<<、>> 运算符,可以进行矩阵的加减乘、共轭转置、输入输出等操作。
```c++
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Complex {
public:
double real, imag;
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex &c) const {
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex &c) const {
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex &c) const {
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
Complex operator!() const {
return Complex(real, -imag);
}
friend ostream &operator<<(ostream &os, const Complex &c) {
os << c.real << (c.imag > 0 ? "+" : "-") << abs(c.imag) << "i";
return os;
}
friend istream &operator>>(istream &is, Complex &c) {
is >> c.real >> c.imag;
return is;
}
};
class Matrix {
private:
int m, n;
Complex **data;
public:
Matrix(int m = 0, int n = 0) : m(m), n(n) {
data = new Complex *[m];
for (int i = 0; i < m; ++i) {
data[i] = new Complex[n];
}
}
Matrix(const Matrix &mat) : m(mat.m), n(mat.n) {
data = new Complex *[m];
for (int i = 0; i < m; ++i) {
data[i] = new Complex[n];
for (int j = 0; j < n; ++j) {
data[i][j] = mat.data[i][j];
}
}
}
~Matrix() {
for (int i = 0; i < m; ++i) {
delete[] data[i];
}
delete[] data;
}
friend Matrix operator+(const Matrix &a, const Matrix &b) {
if (a.m != b.m || a.n != b.n) {
throw runtime_error("the dimension of the two matrices is not the same");
}
Matrix res(a.m, a.n);
for (int i = 0; i < a.m; ++i) {
for (int j = 0; j < a.n; ++j) {
res.data[i][j] = a.data[i][j] + b.data[i][j];
}
}
return res;
}
friend Matrix operator-(const Matrix &a, const Matrix &b) {
if (a.m != b.m || a.n != b.n) {
throw runtime_error("the dimension of the two matrices is not the same");
}
Matrix res(a.m, a.n);
for (int i = 0; i < a.m; ++i) {
for (int j = 0; j < a.n; ++j) {
res.data[i][j] = a.data[i][j] - b.data[i][j];
}
}
return res;
}
friend Matrix operator*(const Matrix &a, const Matrix &b) {
if (a.n != b.m) {
throw runtime_error("the number of columns of the first matrix is not equal to the number of rows of the second matrix");
}
Matrix res(a.m, b.n);
for (int i = 0; i < a.m; ++i) {
for (int j = 0; j < b.n; ++j) {
res.data[i][j] = 0;
for (int k = 0; k < a.n; ++k) {
res.data[i][j] = res.data[i][j] + a.data[i][k] * b.data[k][j];
}
}
}
return res;
}
Matrix operator!() const {
Matrix res(n, m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
res.data[i][j] = !data[j][i];
}
}
return res;
}
friend ostream &operator<<(ostream &os, const Matrix &mat) {
for (int i = 0; i < mat.m; ++i) {
for (int j = 0; j < mat.n; ++j) {
os << setw(8) << mat.data[i][j];
}
os << endl;
}
return os;
}
friend istream &operator>>(istream &is, Matrix &mat) {
for (int i = 0; i < mat.m; ++i) {
for (int j = 0; j < mat.n; ++j) {
is >> mat.data[i][j];
}
}
return is;
}
};
int main() {
int m, n;
cout << "input the dimension of the matrix: ";
cin >> m >> n;
Matrix a(m, n), b(m, n);
cout << "input the elements of the first matrix: " << endl;
cin >> a;
cout << "input the elements of the second matrix: " << endl;
cin >> b;
cout << "the first matrix:" << endl << a;
cout << "the second matrix:" << endl << b;
cout << "the sum of the two matrices:" << endl << a + b;
cout << "the difference of the two matrices:" << endl << a - b;
cout << "the product of the two matrices:" << endl << a * b;
cout << "the conjugate transpose of the first matrix:" << endl << !a;
return 0;
}
```
在这个实现中,我们首先定义了一个复数类 `Complex`,其中重载了 +、-、*、!、<<、>> 运算符,可以进行复数的加减乘、共轭转置、输入输出等操作。然后我们定义了一个矩阵类 `Matrix`,其中重载了 +、-、*、!、<<、>> 运算符,可以进行矩阵的加减乘、共轭转置、输入输出等操作。在实现中,我们使用动态数组生成方式(使用new运算符创建数组,矩阵维度可通过变量赋值/用户交互输入)来生成矩阵,并通过重载运算符实现了各种矩阵运算。
在主函数中,我们首先通过用户输入生成两个矩阵,然后分别输出它们、计算它们的和、差、积和共轭转置,并将结果输出到标准输出流中。
阅读全文