#include <iostream.h>
#include <iomanip.h>
int ** create(int m,int n)
{
int i,j;
int **p = new int*[m];
cout <<"输入矩阵"<<endl;
for(i=0;i<m;i++)
{
p[i] =new int[n];
for(j=0;j<n;j++)
{
cin>>p[i][j];
}
}
return p;
}
void show(int **p,int m,int n)
{
int i,j;
cout <<"输出矩阵"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<p[i][j]<<" ";
}
cout <<endl;
}
}
void Mut(int **pa,int am,int an,int **pb,int bm,int bn) //乘函数
{
int i,j,k,s,cm,cn;
cm = am;
cn = bn;
cout <<"asdf"<<am<<bm;
int **pc = new int*[cm];
for(i=0;i<cm;i++)
{
pc[i] = new int[cn];
for (j=0;j<cm;j++)
{
s=0;
for(k=0;k<an;k++)
s+=pa[i][k]*pb[k][j];
pc[i][j]=s;
}
}
show(pc,cm,cn);
}
void main()
{
int am,an,bm,bn;
int **pa,**pb;
cout <<"创建矩阵A"<<endl;
cout <<"请输入矩阵行数"<<endl;
cin >>am;
cout <<"请输入矩阵列数"<<endl;
cin >>an;
pa = create(am,an);
show(pa,am,an);
cout <<"创建矩阵B"<<endl;
cout <<"请输入矩阵行数"<<endl;
cin >>bm;
cout <<"请输入矩阵列数"<<endl;
cin >>bn;
pb = create(bm,bn);
show(pb,bm,bn);
Mut(pa,am,an,pb,bm,bn);
}
C++动态建立矩阵及矩阵相乘
最新推荐文章于 2023-09-23 21:27:30 发布