这是我修C++的第四次作业第一题,要我们从档案读进两个矩阵,最后相乘显示结果。
此程序主要展示了用OOP的方式计算矩阵,且用了STL的vector,而非传统的array。
Matrix.h 1 #ifndef IMATRIX_H 2 #define IMATRIX_H 3 4 #include < vector > 5 6 class Matrix { 7 8 // Constructor 9public :10 Matrix();11 Matrix(int, int, std::vector<std::vector<int> >);1213 // Member Function14public :15 // Set row1 16 void setRow(int);17 // Get row118 int getRow();19 // Set column120 void setColumn(int);21 // Get column122 int getColumn();23 // Set vector124 void setVector(std::vector<std::vector<int> >);25 // Get vector126 std::vector<std::vector<int> > getVector();2728 // Matrix Multiplication29 Matrix multiply(Matrix);30 // Print the matrix.31 void print();3233 // Data Member34private :35 // Row of matrix136 int row1;37 // Column of matrix138 int column1;39 // Vector of matrix140 std::vector<std::vector<int> > vector1;4142 // Row of matrix243 int row2;44 // Column of matrix245 int column2;46 // Vector of matrix247 std::vector<std::vector<int> > vector2;48} ; 49 50 #endif
Matrix.cpp
1 #include < vector > 2 #include < iostream > 3 #include " Matrix.h " 4 5 // Constructor 6 Matrix::Matrix() { 7 this->row1 = 0; 8 this->column1 = 0; 9 this->row2 = 0;10 this->column2 = 0;11} 12 13 Matrix::Matrix( int row, int column, std::vector < std::vector < int > > vector1) { 14 this->row1 = row;15 this->column1 = column;16 this->vector1 = vector1;17} 18 19 // Member Function 20 // Set row of matrix1 21 void Matrix::setRow( int row) { 22 this->row1 = row;23} 24 25 // Get row of matrix1 26 int Matrix::getRow() { 27 return this->row1;28} 29 30 // Set column of matrix1 31 void Matrix::setColumn( int column) { 32 this->column1 = column;33} 34 35 // Get column of matrix1 36 int Matrix::getColumn() { 37 return this->column1;38} 39 40 // Set vector of matrix1 41 void Matrix::setVector(std::vector < std::vector < int > > vector) { 42 this->vector1 = vector;43} 44 45 // Get vector of matrix2 46 std::vector < std::vector < int > > Matrix::getVector() { 47 return this->vector1;48} 49 50 // Matrix multiplication 51 Matrix Matrix::multiply(Matrix matrix2) { 52 // Vector of matrix153 std::vector<std::vector<int> > matrix1Vector = this->getVector();54 // Vector of matrix255 std::vector<std::vector<int> > matrix2Vector = matrix2.getVector();56 // Vector of matrix3, the result of multiplication57 std::vector<std::vector<int> > matrix3Vector;5859 // Matrix Multiplication 60 for(int i=0; i != this->getRow(); ++i) { 61 std::vector<int> rowVector;62 for(int j=0; j != matrix2.getColumn(); ++j) { 63 int sum = 0;64 for(int k=0; k != this->getColumn(); ++k) { 65 sum += (matrix1Vector[i][k]) * (matrix2Vector[k][j]);66 }6768 rowVector.push_back(sum);69 }70 matrix3Vector.push_back(rowVector);71 }7273 // Transfer matrix3Vector to matrix3 object.74 Matrix matrix3(this->getRow(), matrix2.getColumn(), matrix3Vector);7576 return matrix3;77} 78 79 // Print the matrix 80 void Matrix::print() { 81 // Print the row of matrix82 std::cout << "Row:" << this->row1 << std::endl;83 // Print the column of matrix84 std::cout << "Column:" << this->column1 << std::endl;85 86 // Print the matrix87 for(std::vector<std::vector<int> >::const_iterator iterRow = this->vector1.begin();88 iterRow != this->vector1.end(); ++iterRow) { 89 for(std::vector<int>::const_iterator iter = iterRow->begin();90 iter != iterRow->end(); ++iter) { 9192 std::cout << (*iter) << " ";93 }9495 std::cout << std::endl;96 }97} 98
Main.cpp
1 #include < iostream > 2 #include < fstream > 3 4 #include " Matrix.h " 5 6 // Read data from text file. 7 void readData(Matrix & , Matrix & , const char * ); 8 9 int main() { 10 Matrix matrix1;11 Matrix matrix2;12 std::string fileName = "../data.txt";1314 // Read data from text file.15 readData(matrix1, matrix2, fileName.c_str());1617 // matrix3 is the result of matrix1 multiply matrix218 Matrix matrix3 = matrix1.multiply(matrix2);19 // Print the result of matrix320 matrix3.print();2122 return 0;23} 24 25 // Read data from text file. 26 void readData(Matrix & matrix1, Matrix & matrix2, const char * fileName) { 2728 std::ifstream infile(fileName);2930 if (!infile) { 31 std::cout << "Read error!!" << std::endl;32 }33 else { 34 char str[100];3536 // Row for Matrix137 infile >> str;38 matrix1.setRow(atoi(str));3940 // Column for Matrix141 infile >> str;42 matrix1.setColumn(atoi(str));4344 // Data for Matrix145 std::vector<std::vector<int> > matrix1Vector;46 { 47 for(int i=0; i != matrix1.getRow(); ++i) { 48 std::vector<int> rowVector;49 for(int j=0; j != matrix1.getColumn(); ++j) { 50 infile >> str;51 rowVector.push_back(atoi(str));52 }53 matrix1Vector.push_back(rowVector);54 }55 }56 matrix1.setVector(matrix1Vector);5758 // Row for Matrix259 infile >> str;60 matrix2.setRow(atoi(str));6162 // Column for Matrix263 infile >> str;64 matrix2.setColumn(atoi(str));6566 // Data for Matrix267 std::vector<std::vector<int> > matrix2Vector;68 { 69 for(int i=0; i != matrix2.getRow(); ++i) { 70 std::vector<int> rowVector;71 for(int j=0; j != matrix2.getColumn(); ++j) { 72 infile >> str;73 rowVector.push_back(atoi(str));74 }75 matrix2Vector.push_back(rowVector);76 }77 }78 matrix2.setVector(matrix2Vector);7980 infile.close();81 }82} 83 84
data.txt 1 2 4 2 1 2 3 4 3 7 8 9 10 4 4 3 5 3 2 1 6 6 5 1 7 1 0 1 8 2 5 1 9
执行结果 1 Row: 2 2 Column: 3 3 26 32 10 4 98 104 34 5 請按任意鍵繼續 . . .