算法学习一:矩阵类

任务:

  实现矩阵的基本变换。

接口:

  结构体:Matrix

  成员变量:

    int n, m  矩阵大小

    int a[][]  矩阵内容

  重载运算符:+、-、*

  成员函数:

    void clear()  清空矩阵

代码:

  

const int MAXN = 1010;
const int MAXM = 1010;
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[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[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;
    }
}

 

posted @ 2013-09-24 12:45  chrisyehGone  阅读(192)  评论(0编辑  收藏  举报