yr131 于 2015.06.09 22:02 提问

``````#include<iostream.h>
#include<iomanip.h>
class operation
{public:
static void rank();  //输入阶数
void array();  //输入矩阵
void display();
friend operation operator +(operation &,operation &);
friend operation operator -(operation &,operation &);
friend operation operator *(operation &,operation &);
private:
int a[50][50];
static int n;
};
void operation::rank()
{cin>>n;}
void operation::array()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
operation operator +(operation &a1,operation &a2)
{
operation a3;
for(int i=0;i<operation::n;i++)
for(int j=0;j<operation::n;j++)
a3.a[i][j]=a1.a[i][j]+a2.a[i][j];
return a3;
}
operation operator -(operation &a1,operation &a2)
{
operation a3;
for(int i=0;i<operation::n;i++)
for(int j=0;j<operation::n;j++)
a3.a[i][j]=a1.a[i][j]-a2.a[i][j];
return a3;
}
operation operator *(operation &a1,operation &a2)
{
operation a3;
for(int k=0;k<operation::n;k++)
{
for(int i=0;i<operation::n;i++)
{
for(int j=0;j<operation::n;j++)
a3.a[k][i]+=a1.a[k][j]*a2.a[j][i];

}
}
return a3;
}
void operation::display()
{
for(int i=0;i<n;i++)
{   for(int j=0;j<n;j++)
cout<<setw(10)<<operation::a[i][j];
cout<<endl;
}
}
int operation::n;
#include"operation.h"

int main()
{
operation a1,a2,a3;
cout<<"piease input rank:";
operation::rank();
a1.array();
a2.array();
cout<<"a1+a2:"<<endl;
a3=a1+a2;
a3.display();
cout<<"a1-a2:"<<endl;
a3=a1-a2;
a3.display();
cout<<"a1*a2:"<<endl;
a3=a1*a2;
a3.display();
return 0;
}

``````

3个回答

fcxxxer   2015.06.09 22:18

yr131 乘法输出不对！

wsh1992o   2015.06.10 09:21

yr131 乘法输出不对！好像跟初始化有关

lzp_lrp      2015.06.10 09:31

yr131 一直没找到乘法有什么错，不知道有什么方法检验？