1 #include<stdio.h>
2 #include<iostream>
3 using namespace std;
4 #define size 5
5 int a[][2]={{2,3},{1,3},{3,1}};
6 int b[][3]={{1,-2,-3},{2,-1,0}};
7
8 int r[][3]={{0,0,0},{0,0,0},{0,0,0}};
9
10 void f(int (*)[2],int (*)[3] )
11 {
12 for(int i=0;i<3;i++)
13 {
14 for(int j=0;j<3;j++)
15 {
16 r[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j];
17 }
18 }
19 }
20
21 void main()
22 {
23 f(a,b);
24 for(int i=0;i<3;i++)
25 {
26 for(int j=0;j<3;j++)
27 cout<<r[i][j]<<" ";
28 cout<<endl;
29 }
30 }
1 //有2个矩阵a和b,均为3行3列, 求2个矩阵之积。重载运算符“*”,使之能用于矩阵相乘,如c=a*b。运算符重载函数作为类的成员函数。(最后一题有修改)
2 #include<iostream>
3 #include<iomanip>
4 using namespace std;
5 class Matrix
6 {
7 int a[3][3];
8 public:
9 Matrix(){};
10 void setMatrixa();
11 void setMatrixb();
12 void getMatrix();
13 friend Matrix operator*(Matrix& t,Matrix s);
14 };
15 void Matrix::setMatrixa()
16 {
17 cout<<"Please input 9 integers:"<<endl;
18 for(int i=0;i<3;i++)
19 for(int j=0;j<3;j++)
20 cin>>a[i][j];
21 }
22 void Matrix::setMatrixb()
23 {
24 cout<<"Please input 9 integers:"<<endl;
25 for(int i=0;i<3;i++)
26 for(int j=0;j<3;j++)
27 cin>>a[i][j];
28 }
29 Matrix operator*(Matrix&t,Matrix s)//特别注意,矩阵算法
30 {
31 Matrix temp;
32 for(int i=0;i<3;i++)
33 for(int j=0;j<3;j++)
34 { temp.a[i][j]=0;
35 for(int k=0;k<3;k++)
36 temp.a[i][j]=t.a[i][k]*s.a[k][j]+temp.a[i][j];
37 }
38 return temp;
39 }
40 void Matrix::getMatrix()
41 {
42 for(int i=0;i<3;i++)
43 {for(int j=0;j<3;j++)
44 cout<<setw(6)<<a[i][j];
45 cout<<endl;}
46 }
47 int main()
48 {
49 Matrix m,n,o;
50 m.setMatrixa();
51 cout<<"The Matrixa is:"<<endl;
52 m.getMatrix();
53 n.setMatrixb();
54 cout<<"The Matrixb is:"<<endl;
55 n.getMatrix();
56 o=m*n;
57 cout<<"The add of Matrix is:"<<endl;
58 o.getMatrix();
59 system("pause");
60 }