重载矩阵加法,减法,乘法;
#include<cstdio>
#include<cstring>
struct node
{
int matrix[50][50];
};
int n;
inline node operator +(const node &y,const node &x){
node xx;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
xx.matrix[i][j]=y.matrix[i][j]+x.matrix[i][j];
}
}
return xx;
}
inline node operator -(const node &y,const node &x){
node xx;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
xx.matrix[i][j]=y.matrix[i][j]-x.matrix[i][j];
}
}
return xx;
}
inline node operator *(const node &y,const node &x){
node xx;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
xx.matrix[i][j]=0;
for(int k=0;k<n;k++)
{
xx.matrix[i][j]+=y.matrix[i][k]+x.matrix[k][j];
}
}
}
return xx;
}
int main()
{
int i,j,k;
node mm1,mm2,mm3;
while(scanf("%d",&n)!=EOF)
{
memset(mm1.matrix,0,sizeof(mm1.matrix));
memset(mm2.matrix,0,sizeof(mm2.matrix));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mm1.matrix[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mm2.matrix[i][j]);
}
}
mm3=mm1+mm2;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",mm3.matrix[i][j]);
}printf("\n");
}
}
return 0;
}