(原創) 用OOP实作矩阵相乘 (C/C++)

这是我修C++的第四次作业第一题,要我们从档案读进两个矩阵,最后相乘显示结果。

此程序主要展示了用OOP的方式计算矩阵,且用了STL的vector,而非传统的array。

Matrix.h

 1 None.gif #ifndef IMATRIX_H
 2 None.gif #define  IMATRIX_H
 3 None.gif
 4 None.gif#include  < vector >
 5 None.gif
 6 ExpandedBlockStart.gifContractedBlock.gif class  Matrix  dot.gif {
 7InBlock.gif
 8InBlock.gif  // Constructor
 9InBlock.gifpublic :
10InBlock.gif  Matrix();
11InBlock.gif  Matrix(intint, std::vector<std::vector<int> >);
12InBlock.gif
13InBlock.gif  // Member Function
14InBlock.gifpublic :
15InBlock.gif  // Set row1 
16InBlock.gif  void setRow(int);
17InBlock.gif  // Get row1
18InBlock.gif  int  getRow();
19InBlock.gif  // Set column1
20InBlock.gif  void setColumn(int);
21InBlock.gif  // Get column1
22InBlock.gif  int  getColumn();
23InBlock.gif  // Set vector1
24InBlock.gif  void setVector(std::vector<std::vector<int> >);
25InBlock.gif  // Get vector1
26InBlock.gif  std::vector<std::vector<int> > getVector();
27InBlock.gif
28InBlock.gif  // Matrix Multiplication
29InBlock.gif  Matrix multiply(Matrix);
30InBlock.gif  // Print the matrix.
31InBlock.gif  void print();
32InBlock.gif
33InBlock.gif  // Data Member
34InBlock.gifprivate :
35InBlock.gif  // Row of matrix1
36InBlock.gif  int row1;
37InBlock.gif  // Column of matrix1
38InBlock.gif  int column1;
39InBlock.gif  // Vector of matrix1
40InBlock.gif  std::vector<std::vector<int> > vector1;
41InBlock.gif
42InBlock.gif  // Row of matrix2
43InBlock.gif  int row2;
44InBlock.gif  // Column of matrix2
45InBlock.gif  int column2;
46InBlock.gif  // Vector of matrix2
47InBlock.gif  std::vector<std::vector<int> > vector2;
48ExpandedBlockEnd.gif}
;
49 None.gif
50 None.gif #endif

Matrix.cpp

 1 None.gif #include  < vector >
 2 None.gif#include  < iostream >
 3 None.gif#include  " Matrix.h "
 4 None.gif
 5 None.gif //  Constructor
 6 ExpandedBlockStart.gifContractedBlock.gif Matrix::Matrix()  dot.gif {
 7InBlock.gif  this->row1 = 0;
 8InBlock.gif  this->column1 = 0;
 9InBlock.gif  this->row2 = 0;
10InBlock.gif  this->column2 = 0;
11ExpandedBlockEnd.gif}

12 None.gif
13 ExpandedBlockStart.gifContractedBlock.gifMatrix::Matrix( int  row,  int  column, std::vector < std::vector < int >   >  vector1)  dot.gif {
14InBlock.gif  this->row1 = row;
15InBlock.gif  this->column1 = column;
16InBlock.gif  this->vector1 = vector1;
17ExpandedBlockEnd.gif}

18 None.gif
19 None.gif //  Member Function
20 None.gif //  Set row of matrix1
21 ExpandedBlockStart.gifContractedBlock.gif void  Matrix::setRow( int  row)  dot.gif {
22InBlock.gif  this->row1 = row;
23ExpandedBlockEnd.gif}

24 None.gif
25 None.gif //  Get row of matrix1
26 ExpandedBlockStart.gifContractedBlock.gif int  Matrix::getRow()  dot.gif {
27InBlock.gif  return this->row1;
28ExpandedBlockEnd.gif}

29 None.gif
30 None.gif //  Set column of matrix1
31 ExpandedBlockStart.gifContractedBlock.gif void  Matrix::setColumn( int  column)  dot.gif {
32InBlock.gif  this->column1 = column;
33ExpandedBlockEnd.gif}

34 None.gif
35 None.gif //  Get column of matrix1
36 ExpandedBlockStart.gifContractedBlock.gif int  Matrix::getColumn()  dot.gif {
37InBlock.gif  return this->column1;
38ExpandedBlockEnd.gif}

39 None.gif
40 None.gif //  Set vector of matrix1
41 ExpandedBlockStart.gifContractedBlock.gif void  Matrix::setVector(std::vector < std::vector < int >   >  vector)  dot.gif {
42InBlock.gif  this->vector1 = vector;
43ExpandedBlockEnd.gif}

44 None.gif
45 None.gif //  Get vector of matrix2
46 ExpandedBlockStart.gifContractedBlock.gif std::vector < std::vector < int >   >  Matrix::getVector()  dot.gif {
47InBlock.gif  return this->vector1;
48ExpandedBlockEnd.gif}

49 None.gif
50 None.gif //  Matrix multiplication
51 ExpandedBlockStart.gifContractedBlock.gif Matrix Matrix::multiply(Matrix matrix2)  dot.gif {
52InBlock.gif  // Vector of matrix1
53InBlock.gif  std::vector<std::vector<int> > matrix1Vector = this->getVector();
54InBlock.gif  // Vector of matrix2
55InBlock.gif  std::vector<std::vector<int> > matrix2Vector = matrix2.getVector();
56InBlock.gif  // Vector of matrix3, the result of multiplication
57InBlock.gif  std::vector<std::vector<int> > matrix3Vector;
58InBlock.gif
59InBlock.gif  // Matrix Multiplication 
60ExpandedSubBlockStart.gifContractedSubBlock.gif  for(int i=0; i != this->getRow(); ++i) dot.gif{
61InBlock.gif    std::vector<int> rowVector;
62ExpandedSubBlockStart.gifContractedSubBlock.gif    for(int j=0; j != matrix2.getColumn(); ++j) dot.gif{
63InBlock.gif      int sum = 0;
64ExpandedSubBlockStart.gifContractedSubBlock.gif      for(int k=0; k != this->getColumn(); ++k) dot.gif{
65InBlock.gif        sum += (matrix1Vector[i][k]) * (matrix2Vector[k][j]);
66ExpandedSubBlockEnd.gif      }

67InBlock.gif
68InBlock.gif      rowVector.push_back(sum);
69ExpandedSubBlockEnd.gif    }

70InBlock.gif    matrix3Vector.push_back(rowVector);
71ExpandedSubBlockEnd.gif  }

72InBlock.gif
73InBlock.gif  // Transfer matrix3Vector to matrix3 object.
74InBlock.gif  Matrix matrix3(this->getRow(), matrix2.getColumn(), matrix3Vector);
75InBlock.gif
76InBlock.gif  return matrix3;
77ExpandedBlockEnd.gif}

78 None.gif
79 None.gif //  Print the matrix
80 ExpandedBlockStart.gifContractedBlock.gif void  Matrix::print()  dot.gif {
81InBlock.gif  // Print the row of matrix
82InBlock.gif  std::cout << "Row:" << this->row1 << std::endl;
83InBlock.gif  // Print the column of matrix
84InBlock.gif  std::cout << "Column:" << this->column1 << std::endl;
85InBlock.gif 
86InBlock.gif  // Print the matrix
87InBlock.gif  for(std::vector<std::vector<int> >::const_iterator iterRow = this->vector1.begin();
88ExpandedSubBlockStart.gifContractedSubBlock.gif      iterRow != this->vector1.end(); ++iterRow) dot.gif{
89InBlock.gif        for(std::vector<int>::const_iterator iter = iterRow->begin();
90ExpandedSubBlockStart.gifContractedSubBlock.gif          iter != iterRow->end(); ++iter) dot.gif{
91InBlock.gif
92InBlock.gif            std::cout << (*iter) << " ";
93ExpandedSubBlockEnd.gif        }

94InBlock.gif
95InBlock.gif        std::cout << std::endl;
96ExpandedSubBlockEnd.gif  }

97ExpandedBlockEnd.gif}

98 None.gif

Main.cpp

 1 None.gif #include  < iostream >
 2 None.gif#include  < fstream >
 3 None.gif
 4 None.gif#include  " Matrix.h "
 5 None.gif
 6 None.gif //  Read data from text file.
 7 None.gif void  readData(Matrix & , Matrix & const   char * );
 8 None.gif
 9 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
10InBlock.gif  Matrix matrix1;
11InBlock.gif  Matrix matrix2;
12InBlock.gif  std::string fileName = "../data.txt";
13InBlock.gif
14InBlock.gif  // Read data from text file.
15InBlock.gif  readData(matrix1, matrix2, fileName.c_str());
16InBlock.gif
17InBlock.gif  // matrix3 is the result of matrix1 multiply matrix2
18InBlock.gif  Matrix matrix3 = matrix1.multiply(matrix2);
19InBlock.gif  // Print the result of matrix3
20InBlock.gif  matrix3.print();
21InBlock.gif
22InBlock.gif  return 0;
23ExpandedBlockEnd.gif}

24 None.gif
25 None.gif //  Read data from text file.
26 ExpandedBlockStart.gifContractedBlock.gif void  readData(Matrix &  matrix1, Matrix &  matrix2, const   char *  fileName)  dot.gif {
27InBlock.gif
28InBlock.gif  std::ifstream infile(fileName);
29InBlock.gif
30ExpandedSubBlockStart.gifContractedSubBlock.gif  if (!infile) dot.gif{
31InBlock.gif    std::cout << "Read error!!" << std::endl;
32ExpandedSubBlockEnd.gif  }

33ExpandedSubBlockStart.gifContractedSubBlock.gif  else dot.gif{
34InBlock.gif    char str[100];
35InBlock.gif
36InBlock.gif    // Row for Matrix1
37InBlock.gif    infile >> str;
38InBlock.gif    matrix1.setRow(atoi(str));
39InBlock.gif
40InBlock.gif    // Column for Matrix1
41InBlock.gif    infile >> str;
42InBlock.gif    matrix1.setColumn(atoi(str));
43InBlock.gif
44InBlock.gif    // Data for Matrix1
45InBlock.gif    std::vector<std::vector<int> > matrix1Vector;
46ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
47ExpandedSubBlockStart.gifContractedSubBlock.gif      for(int i=0; i != matrix1.getRow(); ++i) dot.gif{
48InBlock.gif        std::vector<int> rowVector;
49ExpandedSubBlockStart.gifContractedSubBlock.gif        for(int j=0; j != matrix1.getColumn(); ++j) dot.gif{
50InBlock.gif          infile >> str;
51InBlock.gif          rowVector.push_back(atoi(str));
52ExpandedSubBlockEnd.gif        }

53InBlock.gif        matrix1Vector.push_back(rowVector);
54ExpandedSubBlockEnd.gif      }

55ExpandedSubBlockEnd.gif    }

56InBlock.gif    matrix1.setVector(matrix1Vector);
57InBlock.gif
58InBlock.gif    // Row for Matrix2
59InBlock.gif    infile >> str;
60InBlock.gif    matrix2.setRow(atoi(str));
61InBlock.gif
62InBlock.gif    // Column for Matrix2
63InBlock.gif    infile >> str;
64InBlock.gif    matrix2.setColumn(atoi(str));
65InBlock.gif
66InBlock.gif    // Data for Matrix2
67InBlock.gif    std::vector<std::vector<int> > matrix2Vector;
68ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
69ExpandedSubBlockStart.gifContractedSubBlock.gif      for(int i=0; i != matrix2.getRow(); ++i) dot.gif{
70InBlock.gif        std::vector<int> rowVector;
71ExpandedSubBlockStart.gifContractedSubBlock.gif        for(int j=0; j != matrix2.getColumn(); ++j) dot.gif{
72InBlock.gif          infile >> str;
73InBlock.gif          rowVector.push_back(atoi(str));
74ExpandedSubBlockEnd.gif        }

75InBlock.gif        matrix2Vector.push_back(rowVector);
76ExpandedSubBlockEnd.gif      }

77ExpandedSubBlockEnd.gif    }

78InBlock.gif    matrix2.setVector(matrix2Vector);
79InBlock.gif
80InBlock.gif    infile.close();
81ExpandedSubBlockEnd.gif  }

82ExpandedBlockEnd.gif}

83 None.gif
84 None.gif

data.txt

1 None.gif 2   4  
2 None.gif 1   2   3   4  
3 None.gif 7   8   9   10
4 None.gif 4   3
5 None.gif 3   2   1
6 None.gif 6   5   1
7 None.gif 1   0   1
8 None.gif 2   5   1
9 None.gif

执行结果
1 None.gif Row: 2
2 None.gifColumn: 3
3 None.gif 26   32   10
4 None.gif 98   104   34
5 None.gif請按任意鍵繼續 . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值