给定如下的三个运算符重载函数,正确的陈述有()。 CMatrix operator *(const CMatrix &, double); CMatrix operator *( double, const CMatrix &); CMatrix operator *(const CMatrix &, const CMatrix &); double m[][3] = {{1, 2, 3}, {1, 2, 7}, {4, 9, 2}, {6, 1, 5}}; CMatrix mx1(4, 3, &m[0][0]); //根据二维数组产生矩阵 CMatrix mx2(3, 2.0); //对角线元素都是2.0的对角阵 CMatrix mx3 = mx1 * mx2; //绑定矩阵乘法版本:A*B cout<<mx3*2<<endl; //绑定数乘版本:A*2 cout<<2*mx3<<endl; //绑定数乘版本:2*A A 三个函数之间是重载关系。 B mx1 * mx2,调用CMatrix operator *(const CMatrix &, const CMatrix &); C mx3*2,调用CMatrix operator *(const CMatrix &, double); D 2*mx3,调用CMatrix operator *( double, const CMatrix &);
时间: 2024-03-08 16:45:16 浏览: 27
正确的陈述是:B mx1 * mx2,调用CMatrix operator *(const CMatrix &, const CMatrix &); C mx3*2,调用CMatrix operator *(double, const CMatrix &); D 2*mx3,调用CMatrix operator *(const CMatrix &, double);
相关问题
1.设计CMatrix类,通过成员函数实现矩阵加、减、点乘、点除运算;2.通过运算符重载实现矩阵加、减、点乘、点除运算;
1. 设计CMatrix类,成员函数实现矩阵加、减、点乘、点除运算:
```C++
class CMatrix {
private:
int row;
int col;
double **data;
public:
CMatrix(int r, int c);
CMatrix(const CMatrix& matrix);
~CMatrix();
CMatrix operator+(const CMatrix& matrix) const; // 矩阵加法
CMatrix operator-(const CMatrix& matrix) const; // 矩阵减法
CMatrix operator*(const CMatrix& matrix) const; // 矩阵点乘
CMatrix operator/(const CMatrix& matrix) const; // 矩阵点除
void setValue(int r, int c, double value); // 设置矩阵元素
double getValue(int r, int c) const; // 获取矩阵元素
int getRow() const; // 获取矩阵行数
int getCol() const; // 获取矩阵列数
void print() const; // 输出矩阵
};
CMatrix::CMatrix(int r, int c) {
row = r;
col = c;
data = new double*[row];
for (int i = 0; i < row; i++) {
data[i] = new double[col];
for (int j = 0; j < col; j++) {
data[i][j] = 0;
}
}
}
CMatrix::CMatrix(const CMatrix& matrix) {
row = matrix.row;
col = matrix.col;
data = new double*[row];
for (int i = 0; i < row; i++) {
data[i] = new double[col];
for (int j = 0; j < col; j++) {
data[i][j] = matrix.data[i][j];
}
}
}
CMatrix::~CMatrix() {
for (int i = 0; i < row; i++) {
delete[] data[i];
}
delete[] data;
}
CMatrix CMatrix::operator+(const CMatrix& matrix) const {
if (row != matrix.row || col != matrix.col) {
cerr << "Error: the size of two matrixes are different." << endl;
exit(1);
}
CMatrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] + matrix.data[i][j];
}
}
return result;
}
CMatrix CMatrix::operator-(const CMatrix& matrix) const {
if (row != matrix.row || col != matrix.col) {
cerr << "Error: the size of two matrixes are different." << endl;
exit(1);
}
CMatrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] - matrix.data[i][j];
}
}
return result;
}
CMatrix CMatrix::operator*(const CMatrix& matrix) const {
if (col != matrix.row) {
cerr << "Error: the size of two matrixes are not valid for dot multiplication." << endl;
exit(1);
}
CMatrix result(row, matrix.col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < matrix.col; j++) {
result.data[i][j] = 0;
for (int k = 0; k < col; k++) {
result.data[i][j] += data[i][k] * matrix.data[k][j];
}
}
}
return result;
}
CMatrix CMatrix::operator/(const CMatrix& matrix) const {
if (row != matrix.row || col != matrix.col) {
cerr << "Error: the size of two matrixes are different." << endl;
exit(1);
}
CMatrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] / matrix.data[i][j];
}
}
return result;
}
void CMatrix::setValue(int r, int c, double value) {
if (r < 0 || r >= row || c < 0 || c >= col) {
cerr << "Error: the index is out of range." << endl;
exit(1);
}
data[r][c] = value;
}
double CMatrix::getValue(int r, int c) const {
if (r < 0 || r >= row || c < 0 || c >= col) {
cerr << "Error: the index is out of range." << endl;
exit(1);
}
return data[r][c];
}
int CMatrix::getRow() const {
return row;
}
int CMatrix::getCol() const {
return col;
}
void CMatrix::print() const {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
```
2. 运算符重载实现矩阵加、减、点乘、点除运算:
```C++
CMatrix operator+(const CMatrix& matrix1, const CMatrix& matrix2) {
return matrix1.operator+(matrix2);
}
CMatrix operator-(const CMatrix& matrix1, const CMatrix& matrix2) {
return matrix1.operator-(matrix2);
}
CMatrix operator*(const CMatrix& matrix1, const CMatrix& matrix2) {
return matrix1.operator*(matrix2);
}
CMatrix operator/(const CMatrix& matrix1, const CMatrix& matrix2) {
return matrix1.operator/(matrix2);
}
```
这样就可以通过运算符重载实现矩阵加、减、点乘、点除运算了。
CMatrix operator + (const CMatrix& m1, const CMatrix& m2) { if ((m1.Col() != m2.Col()) || (m1.Row() != m2.Row())) { throw("CMatrix::operator+: The two matrix have different size!"); } CMatrix matTmp(m1.Row(), m1.Col()); for (int i = 0; i < m1.Row(); i++) { for (int j = 0; j < m1.Col(); j++) { matTmp(i, j) = m1(i, j) + m2(i, j); } } return matTmp; }
这段代码是矩阵类 `CMatrix` 的加法运算符重载函数,用于计算两个矩阵对象 `m1` 和 `m2` 的和。
重载的加法运算符接受两个参数:矩阵对象 `m1` 和矩阵对象 `m2`。
在函数内部,首先进行矩阵大小的检查,判断两个矩阵的行数和列数是否相等。如果不相等,则抛出异常并给出错误信息。
如果两个矩阵大小相等,则创建一个临时的矩阵对象 `matTmp`,其行数和列数与输入的矩阵对象相同。
接下来,使用两个嵌套的循环遍历每一个元素,将对应位置的两个矩阵元素相加,并将结果赋值给临时矩阵对象 `matTmp` 的对应位置。
最后,返回临时矩阵对象 `matTmp`。
通过重载加法运算符,可以直接使用 `m1 + m2` 的形式来计算两个矩阵对象的和。
请注意,在使用加法运算符时,要确保参与运算的两个矩阵大小相等,否则会抛出异常。另外,返回的是一个新创建的矩阵对象,不会修改原始的输入矩阵对象。
相关推荐
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)