第1个回答 2011-11-11
#include <iomanip>
class Arr
{
public:
Arr(){x=0;y=0;p=NULL;}
~Arr()
{
for(int i=0;i<x;i++)
delete[] p[i];
delete[] p;
}
int getX();
int getY();
int **getP();
void inputArr(int x,int y);
private:
int x,y;
int **p;
};
void Arr::inputArr(int m,int n)
{
int i,j;
x=m;
y=n;
p=new int*[m];
for (i=0;i<m;i++)
p[i]=new int[n];
for (i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>p[i][j];
}
}
int Arr::getX()
{
return x;
}
int Arr::getY()
{
return y;
}
int** Arr::getP()
{
return p;
}
int** multiArr(Arr a,Arr b)
{
int i,j,k;
int **a1,**b1,**c1;
c1=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c1[i]=new int[b.getY()];
a1=a.getP();
b1=b.getP();
for (i=0;i<a.getX();i++)
{
for (j=0;j<b.getY();j++)
{
for (k=0;k<a.getY();k++)
{
c1[i][j]=a1[i][k]*b1[k][j];
}
}
}
return c1;
}
int main(int argc, char* argv[])
{
Arr a,b;
int **c;
int x1,y1,x2,y2,i;
cout<<"input the row and column of first array:"<<endl;
cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"<<endl;
cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c[i]=new int[b.getY()];
c=multiArr(a,b);
cout<<"the result is:"<<endl;
for (i=0;i<a.getX();i++)
{
for(int j=0;j<b.getY();j++)
cout<<setw(6)<<c[i][j];
cout<<endl;
}
}
else
cout<<"the two array don't multiplicate!"<<endl;
return 0;
}
第2个回答 2011-11-11
#include <iomanip>
class Arr
{
public:
Arr(){x=0;y=0;p=NULL;}
int getX();
int getY();
int **getP();
void inputArr(int x,int y);
private:
int x,y;
int **p;
};
void Arr::inputArr(int m,int n)
{
int i,j;
x=m;
y=n;
p=new int*[m];
for (i=0;i<m;i++)
p[i]=new int[n];
for (i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>p[i][j];
}
}
int Arr::getX()
{
return x;
}
int Arr::getY()
{
return y;
}
int** Arr::getP()
{
return p;
}
int** multiArr(Arr a,Arr b)
{
int i,j,k;
int **a1,**b1,**c1;
c1=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c1[i]=new int[b.getY()];
a1=a.getP();
b1=b.getP();
for (i=0;i<a.getX();i++)
{
for (j=0;j<b.getY();j+)
{
for (k=0;k<a.getY();k++)
{
c1[i][j]=a1[i][k]*b1[k][j];
}
}
}
return c1;
}
int main(int argc, char* argv[])
{
Arr a,b;
int **c;
int x1,y1,x2,y2,i;
cout<<"input the row and column of first array:"<<endl;
cin>>x1>>y1;
a.inputArr(x1,y1);
cout<<"input the row and column of second array:"<<endl;
cin>>x2>>y2;
b.inputArr(x2,y2);
if(a.getY()==b.getX())
{
c=new int*[a.getX()];
for (i=0;i<a.getX();i++)
c[i]=new int[b.getY()];
c=multiArr(a,b);
cout<<"the result is:"<<endl;
for (i=0;i<a.getX();i++)
{
for(int j=0;j<b.getY();j++)
cout<<setw(6)<<c[i][j];
cout<<endl;
}
}
else
cout<<"the two array don't multiplicate!"<<endl;
return 0;
}