编写一个Matrix类,实现下列要求:能够输入、输出矩阵,执行矩阵转置,加减法(包括负数)、乘法,将矩阵输出;根据主函数的要求编写;保证数据合法; 备注:(752)
时间: 2023-06-14 07:04:06 浏览: 96
编写代表矩阵的Matrix类
4星 · 用户满意度95%
以下是Matrix类的实现:
```
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
int rows;
int cols;
vector<vector<int>> data;
public:
// 构造函数
Matrix(int r, int c) {
rows = r;
cols = c;
data.resize(rows);
for (int i = 0; i < rows; i++) {
data[i].resize(cols);
}
}
// 输入矩阵
void input() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> data[i][j];
}
}
}
// 输出矩阵
void output() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
// 矩阵转置
void transpose() {
vector<vector<int>> newData(cols, vector<int>(rows));
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
newData[i][j] = data[j][i];
}
}
data = newData;
swap(rows, cols);
}
// 矩阵加法
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("Invalid matrix size for addition!");
}
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("Invalid matrix size for subtraction!");
}
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 (cols != other.rows) {
throw invalid_argument("Invalid matrix size for multiplication!");
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
// 取负
Matrix operator-() const {
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];
}
}
return result;
}
};
```
下面是一个示例主函数,用于测试Matrix类的各种功能:
```
int main() {
int r1, c1;
cout << "Enter the size of the first matrix (rows columns): ";
cin >> r1 >> c1;
Matrix m1(r1, c1);
cout << "Enter the first matrix:" << endl;
m1.input();
int r2, c2;
cout << "Enter the size of the second matrix (rows columns): ";
cin >> r2 >> c2;
Matrix m2(r2, c2);
cout << "Enter the second matrix:" << endl;
m2.input();
cout << "The first matrix is:" << endl;
m1.output();
cout << "The second matrix is:" << endl;
m2.output();
cout << "The transpose of the first matrix is:" << endl;
m1.transpose().output();
try {
Matrix m3 = m1 + m2;
cout << "The sum of the two matrices is:" << endl;
m3.output();
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
try {
Matrix m4 = m1 - m2;
cout << "The difference of the two matrices is:" << endl;
m4.output();
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
try {
Matrix m5 = m1 * m2;
cout << "The product of the two matrices is:" << endl;
m5.output();
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
cout << "The negative of the first matrix is:" << endl;
(-m1).output();
return 0;
}
```
阅读全文