设计一个矩阵类Matrix,要求: 1)包含三个数据成员:data[32][32],行数row, 列数col;要求row和col都不大于32; 2)为Matrix类添加如下几个成员函数: Matrix(int r,int col) ,矩阵的构造函数; FillMatrix ,矩阵的输入函数,用于从键盘输入数据到矩阵中; TransMatrix ,矩阵的转置函数,用于对本矩阵进行转置; 3) 为Matrix重载运算符+和*,要求运算符“+”以成员函数的形式重载,运算符“*”以友元函数的形式重载; 4) 重载运算符>>和<<,完成矩阵对象的整体输入输出,并在主程序中测试矩阵的写入文件和从文件读出。
时间: 2024-04-13 14:28:28 浏览: 260
下面是一个实现上述要求的 Matrix 类的示例代码:
```cpp
#include <iostream>
#include <fstream>
using namespace std;
class Matrix {
private:
int data[32][32];
int row;
int col;
public:
Matrix(int r, int c) : row(r), col(c) {}
void FillMatrix() {
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> data[i][j];
}
}
}
void TransMatrix() {
Matrix transposed(col, row);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
transposed.data[j][i] = data[i][j];
}
}
cout << "Transposed matrix:" << endl;
for (int i = 0; i < transposed.row; i++) {
for (int j = 0; j < transposed.col; j++) {
cout << transposed.data[i][j] << " ";
}
cout << endl;
}
}
Matrix operator+(const Matrix& other) {
if (row != other.row || col != other.col) {
cout << "Error: The matrices should have the same dimensions for addition." << endl;
return *this;
}
Matrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
friend Matrix operator*(const Matrix& matrix1, const Matrix& matrix2);
friend ostream& operator<<(ostream& out, const Matrix& matrix);
friend istream& operator>>(istream& in, Matrix& matrix);
};
Matrix operator*(const Matrix& matrix1, const Matrix& matrix2) {
if (matrix1.col != matrix2.row) {
cout << "Error: The number of columns in the first matrix should be equal to the number of rows in the second matrix for multiplication." << endl;
return matrix1;
}
Matrix result(matrix1.row, matrix2.col);
for (int i = 0; i < matrix1.row; i++) {
for (int j = 0; j < matrix2.col; j++) {
for (int k = 0; k < matrix1.col; k++) {
result.data[i][j] += matrix1.data[i][k] * matrix2.data[k][j];
}
}
}
return result;
}
ostream& operator<<(ostream& out, const Matrix& matrix) {
out << "Matrix:" << endl;
for (int i = 0; i < matrix.row; i++) {
for (int j = 0; j < matrix.col; j++) {
out << matrix.data[i][j] << " ";
}
out << endl;
}
return out;
}
istream& operator>>(istream& in, Matrix& matrix) {
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < matrix.row; i++) {
for (int j = 0; j < matrix.col; j++) {
in >> matrix.data[i][j];
}
}
return in;
}
int main() {
int row, col;
cout << "Enter the number of rows and columns for the matrix: ";
cin >> row >> col;
Matrix matrix1(row, col);
cin >> matrix1;
Matrix matrix2(row, col);
cin >> matrix2;
Matrix matrix3 = matrix1 + matrix2;
cout << matrix3;
Matrix matrix4 = matrix1 * matrix2;
cout << matrix4;
matrix1.TransMatrix();
ofstream outputFile("matrix.txt");
outputFile << matrix1;
outputFile.close();
ifstream inputFile("matrix.txt");
Matrix matrix5(0, 0);
inputFile >> matrix5;
inputFile.close();
cout << "Matrix read from file:" << endl;
cout << matrix5;
return 0;
}
```
这个代码实现了 Matrix 类,包含了构造函数、输入函数、转置函数以及运算符重载。你可以在主程序中测试矩阵的输入、输出、转置、加法和乘法运算,并可以将矩阵写入文件和从文件中读出。
阅读全文