(原創) 用OOP实作矩阵相乘 (C/C++)
这是我修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
9
public :
10
Matrix();
11
Matrix(int, int, std::vector<std::vector<int> >);
12
13
// Member Function
14
public :
15
// Set row1
16
void setRow(int);
17
// Get row1
18
int getRow();
19
// Set column1
20
void setColumn(int);
21
// Get column1
22
int getColumn();
23
// Set vector1
24
void setVector(std::vector<std::vector<int> >);
25
// Get vector1
26
std::vector<std::vector<int> > getVector();
27
28
// Matrix Multiplication
29
Matrix multiply(Matrix);
30
// Print the matrix.
31
void print();
32
33
// Data Member
34
private :
35
// Row of matrix1
36
int row1;
37
// Column of matrix1
38
int column1;
39
// Vector of matrix1
40
std::vector<std::vector<int> > vector1;
41
42
// Row of matrix2
43
int row2;
44
// Column of matrix2
45
int column2;
46
// Vector of matrix2
47
std::vector<std::vector<int> > vector2;
48
};
49
50
#endif
#ifndef IMATRIX_H2
#define IMATRIX_H3

4
#include <vector>5

6
class Matrix {7

8
// Constructor9
public :10
Matrix();11
Matrix(int, int, std::vector<std::vector<int> >);12

13
// Member Function14
public :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();27

28
// Matrix Multiplication29
Matrix multiply(Matrix);30
// Print the matrix.31
void print();32

33
// Data Member34
private :35
// Row of matrix136
int row1;37
// Column of matrix138
int column1;39
// Vector of matrix140
std::vector<std::vector<int> > vector1;41

42
// Row of matrix243
int row2;44
// Column of matrix245
int column2;46
// Vector of matrix247
std::vector<std::vector<int> > vector2;48
};49

50
#endifMatrix.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 matrix1
53
std::vector<std::vector<int> > matrix1Vector = this->getVector();
54
// Vector of matrix2
55
std::vector<std::vector<int> > matrix2Vector = matrix2.getVector();
56
// Vector of matrix3, the result of multiplication
57
std::vector<std::vector<int> > matrix3Vector;
58
59
// 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
}
67
68
rowVector.push_back(sum);
69
}
70
matrix3Vector.push_back(rowVector);
71
}
72
73
// Transfer matrix3Vector to matrix3 object.
74
Matrix matrix3(this->getRow(), matrix2.getColumn(), matrix3Vector);
75
76
return matrix3;
77
}
78
79
// Print the matrix
80
void Matrix::print() {
81
// Print the row of matrix
82
std::cout << "Row:" << this->row1 << std::endl;
83
// Print the column of matrix
84
std::cout << "Column:" << this->column1 << std::endl;
85
86
// Print the matrix
87
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) {
91
92
std::cout << (*iter) << " ";
93
}
94
95
std::cout << std::endl;
96
}
97
}
98
#include <vector>2
#include <iostream>3
#include "Matrix.h"4

5
// Constructor6
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 Function20
// Set row of matrix121
void Matrix::setRow(int row) {22
this->row1 = row;23
}24

25
// Get row of matrix126
int Matrix::getRow() {27
return this->row1;28
}29

30
// Set column of matrix131
void Matrix::setColumn(int column) {32
this->column1 = column;33
}34

35
// Get column of matrix136
int Matrix::getColumn() {37
return this->column1;38
}39

40
// Set vector of matrix141
void Matrix::setVector(std::vector<std::vector<int> > vector) {42
this->vector1 = vector;43
}44

45
// Get vector of matrix246
std::vector<std::vector<int> > Matrix::getVector() {47
return this->vector1;48
}49

50
// Matrix multiplication51
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;58

59
// 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
}67

68
rowVector.push_back(sum);69
}70
matrix3Vector.push_back(rowVector);71
}72

73
// Transfer matrix3Vector to matrix3 object.74
Matrix matrix3(this->getRow(), matrix2.getColumn(), matrix3Vector);75

76
return matrix3;77
}78

79
// Print the matrix80
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) {91

92
std::cout << (*iter) << " ";93
}94

95
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";
13
14
// Read data from text file.
15
readData(matrix1, matrix2, fileName.c_str());
16
17
// matrix3 is the result of matrix1 multiply matrix2
18
Matrix matrix3 = matrix1.multiply(matrix2);
19
// Print the result of matrix3
20
matrix3.print();
21
22
return 0;
23
}
24
25
// Read data from text file.
26
void readData(Matrix& matrix1, Matrix& matrix2,const char* fileName) {
27
28
std::ifstream infile(fileName);
29
30
if (!infile) {
31
std::cout << "Read error!!" << std::endl;
32
}
33
else {
34
char str[100];
35
36
// Row for Matrix1
37
infile >> str;
38
matrix1.setRow(atoi(str));
39
40
// Column for Matrix1
41
infile >> str;
42
matrix1.setColumn(atoi(str));
43
44
// Data for Matrix1
45
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);
57
58
// Row for Matrix2
59
infile >> str;
60
matrix2.setRow(atoi(str));
61
62
// Column for Matrix2
63
infile >> str;
64
matrix2.setColumn(atoi(str));
65
66
// Data for Matrix2
67
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);
79
80
infile.close();
81
}
82
}
83
84
#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";13

14
// Read data from text file.15
readData(matrix1, matrix2, fileName.c_str());16

17
// matrix3 is the result of matrix1 multiply matrix218
Matrix matrix3 = matrix1.multiply(matrix2);19
// Print the result of matrix320
matrix3.print();21

22
return 0;23
}24

25
// Read data from text file.26
void readData(Matrix& matrix1, Matrix& matrix2,const char* fileName) {27

28
std::ifstream infile(fileName);29

30
if (!infile) {31
std::cout << "Read error!!" << std::endl;32
}33
else {34
char str[100];35

36
// Row for Matrix137
infile >> str;38
matrix1.setRow(atoi(str));39

40
// Column for Matrix141
infile >> str;42
matrix1.setColumn(atoi(str));43

44
// 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);57

58
// Row for Matrix259
infile >> str;60
matrix2.setRow(atoi(str));61

62
// Column for Matrix263
infile >> str;64
matrix2.setColumn(atoi(str));65

66
// 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);79

80
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
2 4 2
1 2 3 4 3
7 8 9 104
4 35
3 2 16
6 5 17
1 0 18
2 5 19

执行结果
1
Row:2
2
Column:3
3
26 32 10
4
98 104 34
5
請按任意鍵繼續 . . .
Row:22
Column:33
26 32 104
98 104 345
請按任意鍵繼續 . . .



浙公网安备 33010602011771号