设计一个矩阵类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 10:28:38 浏览: 144
根据您的要求,我设计了一个满足条件的Matrix类,具体实现如下:
```cpp
#include <iostream>
#include <fstream>
class Matrix {
private:
int data[32][32];
int row;
int col;
public:
Matrix(int r, int c) : row(r), col(c) {}
void FillMatrix() {
std::cout << "Enter the elements of the matrix:" << std::endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
std::cin >> data[i][j];
}
}
}
void TransMatrix() {
Matrix transposedMatrix(col, row);
for (int i = 0; i < col; ++i) {
for (int j = 0; j < row; ++j) {
transposedMatrix.data[i][j] = data[j][i];
}
}
std::cout << "Transposed Matrix:" << std::endl;
transposedMatrix.DisplayMatrix();
}
Matrix operator+(const Matrix& other) {
if (row != other.row || col != other.col) {
std::cerr << "Error: Matrices must have the same dimensions for addition!" << std::endl;
exit(1);
}
Matrix sumMatrix(row, col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sumMatrix.data[i][j] = data[i][j] + other.data[i][j];
}
}
return sumMatrix;
}
friend Matrix operator*(const Matrix& matrix1, const Matrix& matrix2);
friend std::ostream& operator<<(std::ostream& os, const Matrix& matrix);
friend std::istream& operator>>(std::istream& is, Matrix& matrix);
void DisplayMatrix() const {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
};
Matrix operator*(const Matrix& matrix1, const Matrix& matrix2) {
if (matrix1.col != matrix2.row) {
std::cerr << "Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication!" << std::endl;
exit(1);
}
Matrix productMatrix(matrix1.row, matrix2.col);
for (int i = 0; i < matrix1.row; ++i) {
for (int j = 0; j < matrix2.col; ++j) {
int sum = 0;
for (int k = 0; k < matrix1.col; ++k) {
sum += matrix1.data[i][k] * matrix2.data[k][j];
}
productMatrix.data[i][j] = sum;
}
}
return productMatrix;
}
std::ostream& operator<<(std::ostream& os, const Matrix& matrix) {
for (int i = 0; i < matrix.row; ++i) {
for (int j = 0; j < matrix.col; ++j) {
os << matrix.data[i][j] << " ";
}
os << std::endl;
}
return os;
}
std::istream& operator>>(std::istream& is, Matrix& matrix) {
std::cout << "Enter the elements of the matrix:" << std::endl;
for (int i = 0; i < matrix.row; ++i) {
for (int j = 0; j < matrix.col; ++j) {
is >> matrix.data[i][j];
}
}
return is;
}
int main() {
// Test FillMatrix, TransMatrix, operator+, operator*
Matrix matrix1(3, 3);
matrix1.FillMatrix();
std::cout << "Matrix 1:" << std::endl;
matrix1.DisplayMatrix();
std::cout << "Transposing Matrix 1..." << std::endl;
matrix1.TransMatrix();
Matrix matrix2(3, 3);
matrix2.FillMatrix();
std::cout << "Matrix 2:" << std::endl;
matrix2.DisplayMatrix();
Matrix sumMatrix = matrix1 + matrix2;
std::cout << "Sum of Matrix 1 and Matrix 2:" << std::endl;
sumMatrix.DisplayMatrix();
Matrix productMatrix = matrix1 * matrix2;
std::cout << "Product of Matrix 1 and Matrix 2:" << std::endl;
productMatrix.DisplayMatrix();
// Test operator<< and operator>>
std::ofstream outFile("matrix.txt");
outFile << matrix1;
outFile.close();
std::ifstream inFile("matrix.txt");
Matrix readMatrix(3, 3);
inFile >> readMatrix;
inFile.close();
std::cout << "Read Matrix:" << std::endl;
readMatrix.DisplayMatrix();
return 0;
}
```
在上述代码中,我们定义了一个Matrix类,它包含了要求的数据成员和成员函数。其中,构造函数用于初始化矩阵的行数和列数,FillMatrix函数用于从键盘输入矩阵的元素,TransMatrix函数用于对矩阵进行转置。
我们重载了运算符+和*,其中运算符+以成员函数的形式重载,运算符*以友元函数的形式重载。另外,我们还重载了运算符>>和<<,用于实现矩阵对象的整体输入输出。
在主程序中,我们进行了各个成员函数和运算符的测试,并且演示了将矩阵写入文件和从文件读出的操作。
请注意,我们假设矩阵的行数和列数均不大于32,并在设计中进行了相应限制。
阅读全文