模板-->Matrix重载运算符:+,-,x

如果有相应的OJ题目,欢迎同学们提供相应的链接

相关链接

简单的测试

INPUT:
1 2     3 1
3 4     3 -1
OUTPUT:
+ 4 3   
   6 3      
- -2 1   
   0 5   
x 8 -1
  18 -1

代码模板

const int MAXN=1;    #modify here
const int MAXM=1;    #modify here
struct Matrix{
    int n,m;
    int a[MAXN][MAXM];
    void clear(){
        n=m=0;
        memset(a,0,sizeof(a));
    }

    Matrix operator +(const Matrix &b) const{
        Matrix tmp;
        tmp.n=n;tmp.m=m;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                tmp.a[i][j]=a[i][j]+b.a[i][j];
        return tmp;
    }
    Matrix operator -(const Matrix &b) const{
        Matrix tmp;
        tmp.n=n;tmp.m=m;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                tmp.a[i][j]=a[i][j]-b.a[i][j];
        return tmp;
    }
    Matrix operator *(const Matrix &b) const{
        Matrix tmp;
        tmp.clear();
        tmp.n=n;tmp.m=b.m;
        for(int i=0;i<n;i++)
            for(int j=0;j<b.m;j++)
                for(int k=0;k<m;k++)
                    tmp.a[i][j]+=a[i][k]*b.a[k][j];
        return tmp;
    }
};
原文地址:https://www.cnblogs.com/mRRRR/p/5540214.html