c++中矩阵相乘(对象数组与对象指针的应用)
- 设计一个矩阵类 Matrix(矩阵由二维数组实现),有分配空间和对矩阵赋值的功能。
- 将这个矩阵类的对象作为参数传送到函数 Mul()
用三种方式传递参数:
直接传送:Mul(Matrix a,Matrix b),实际上只是传送值,在函数中针对 对象的任何修改均不影响该对象本身
指针传送:Mul(Matrix *pa,Matrix *pb)
引用传送:Mul(Matrix &a,Matrix &b)将 Mul() 函数实现
- 定义一个友元函数实现转制功能。转制是指将数组中 a[i][j]与 a[j][i]的值对调。
下面给出代码
#include<iostream>
using namespace std;
class Matrix { //定义类
private:
int **pmatrix;
int imaxx,imaxy;
static int matrixnum; //计算有多少个矩阵
public:
Matrix(unsigned int x,unsigned int y);
Matrix(Matrix &m);
~Matrix();
void SetValue(unsigned int x,unsigned int y,int value);
void input();
void print() const;
friend Matrix Rotate(Matrix &m); //定义友元函数实现转置功能
friend Matrix Mul(Matrix a,Matrix b); //定义MUL函数,实现矩阵相乘的功能
unsigned int getx(){return imaxx;}
unsigned int gety(){return imaxy;}
int getvalue( int x, int y){
return pmatrix[x][y];
}
};
int Matrix::matrixnum=0; //静态变量初始化
Matrix::Matrix(unsigned int x,unsigned int y) { //直接传送
pmatrix=new int *[x]; //为二维数组分配空间
for(int i=0;i<x;i++)
pmatrix[i]=new int[y];
imaxx=x;
imaxy=y;
matrixnum++; }
Matrix::Matrix(Matrix &m){ //引用传送(需要具体)
imaxx=m.imaxx;
imaxy=m.imaxy;
pmatrix=new int *[imaxx];
for(int i=0;i<imaxx;i++)
pmatrix[i]=new int[imaxy];
for(i=0;i<imaxx;i++)
for(int j=0;j<imaxy;j++)
pmatrix[i][j]=m.pmatrix[i][j];
matrixnum++; }
Matrix::~Matrix() { //析构函数
for(int i=0;i<imaxx;i++)
delete[]pmatrix[i];
delete[]pmatrix;
matrixnum--; }
void Matrix::SetValue(unsigned int x,unsigned int y,int value) { //设置单个矩阵元素的值
pmatrix[x][y]=value;
}
void Matrix::input() { //设置整个矩阵的值
int x,y,value;
for(x=0;x<imaxx;x++)
for(y=0;y<imaxy;y++)
{
cin>>value;
SetValue(x,y,value); } }
void Matrix::print()const {
int x,y;
cout<<"共有"<<matrixnum<<"个矩阵"<<endl;
cout<<"Matrix ["<<imaxx<<","<<imaxy<<"]";
for(x=0;x<imaxx;x++)
{
cout<<endl;
for(y=0;y<imaxy;y++)
cout<<pmatrix[x][y]<<" ";
}
}
Matrix Rotate(Matrix &m) { //定义友元函数达到转置的目的
unsigned int i,j;
Matrix a(m.imaxy,m.imaxx);
for(i=0;i<m.imaxx;i++)
for(j=0;j<m.imaxy;j++)
a.pmatrix[j][i]=m.pmatrix[i][j];
return a; }
Matrix Mul(Matrix a,Matrix b) { //当以矩阵相乘函数 直接传递
int x,y,z;
Matrix c(a.imaxx,b.imaxy);
for(x=0;x<a.imaxx;x++)
for(y=0;y<b.imaxy;y++) {
c.pmatrix[x][y]=0;
for(z=0;z<a.imaxy;z++)
c.pmatrix[x][y]+=a.pmatrix[x][z]*b.pmatrix[z][y];
}
return c; }
Matrix Mul(Matrix* a,Matrix* b) { //当以矩阵相乘函数 指针传递
int x,y,z;
Matrix c(a->getx(),b->gety());
for(x=0;x<a->getx();x++)
for(y=0;y<b->gety();y++) {
int sum=0;
for(z=0;z<=a->getx();z++)
sum+=(a->getvalue(x,z)*(b->getvalue(z,y)));
c.SetValue(x,y,sum);
}
return c; }
Matrix Mul(Matrix& a,Matrix& b) { //当以矩阵相乘函数 引用传递
int x,y,z;
Matrix c(a.getx(),b.gety());
for(x=0;x<a.getx();x++)
for(y=0;y<b.gety();y++) {
int sum=0;
for(z=0;z<=a.getx();z++)
sum+=(a.getvalue(x,z)*(b.getvalue(z,y)));
c.SetValue(x,y,sum);
}
return c; }
int main() {
Matrix a(2,3);
cout<<"输入 a[2][3] :"<<endl;
a.input();
cout<<endl;
a.print();
cout<<endl;
Matrix b(3,3);
cout<<"输入 b[3][3] :"<<endl;
b.input();
cout<<endl;
b.print();
cout<<endl;
Matrix c=Mul(&a,&b);
cout<<"a,b矩阵相乘";
c.print()
cout<<e - 列表内容;
cout<<"将矩阵a[2][3]转置: "<<endl;
Matrix d=Rotate(a);
d.print();
cout<<endl;
return 0; }
注意点:
1) 在定义友元函数是不需要有类的限定
2) 引用函数时要记得()
3) 在通过引用和数组对矩阵进行操作时,是对矩阵本身操作,而直接传递相当于是对形参进行操作,不影响实参
4) 再给二重指针分配空间时,二级指针=指针数组,指针数组中的元素值=整形数组