矩阵类

来源:互联网 发布:淘宝网2017女装冬装 编辑:程序博客网 时间:2024/05/17 16:46

将矩阵matrix用一个一维数组element储存(涉及了很多运算符重载的方法)

#include"expection.h"using namespace std;template <class T>class matrix{    friend ostream& operator<<(ostream &, const matrix<T>&);public:    matrix(int theRows = 0, int theColumns = 0); //构造函数    matrix(const matrix<T>&); //复制构造函数    ~matrix(){ delete[] element; }    int rows() const { return theRows; }    int columns() const { return theColumns; }    T &operator()(int i, int j) const;    matrix<T>& operator=(const matrix<T>&);    matrix<T> operator+() const; //单目运算符    matrix<T> operator+(const matrix<T>&) const;//双目运算符    matrix<T> operator-() const;//单目运算符    matrix<T> operator-(const matrix<T>&) const;//双目    matrix<T> operator*(const matrix<T>&) const;    matrix<T>& operator+=(const T&);private:    int theRows;//行数    int theColumns;//列数    T *element;//数组};template<class T>matrix<T>::matrix(int theRows , int theColumns){//构造函数    if (theColumns < 0 || theRows < 0)    {        throw illegalParameterValue("Rows and columns must be >=0");    }    if ((theColumns == 0 || theRows == 0) && (theColumns != 0 || theRows != 0))    {        throw illegalParameterValue("Either both or neither rows and columns shoud be zero");    }    //创建矩阵    this->theColumns = theColumns;    this->theRows = theRows;    element = new T[theColumns*theRows];}template<class T>matrix<T>::matrix(const matrix<T>&M){//复制构造函数     //创建矩阵    theColumns = M.theColumns;    theRows = M.theRows;    element = new T[theColumns*theRows];    copy(M.element, M.element + theColumns*theRows, element);}template<class T>//重载赋值操作=matrix<T>& matrix<T>::operator=(const matrix<T>& M){//赋值 (*this)=M    if (this != &M)    {//不能复制自己        delete[] element;        theColumns = M.theColumns;        theRows = M.theRows;        element = new T[theColumns*theRows];        copy(M.element, M.element + theColumns*theRows, element);    }    return *this;}template<class T>//括号运算符重载,用左右括号来表示矩阵的索引a(i,j)T& matrix<T>::operator()(int i, int j) const{    if (i<1 || i>theRows || j<1 || j>theColumns)    {        throw matrixIndexOutOfBounds();    }    return element[(i - 1)*theColumns + j - 1];}template<class T>//加号运算符重载matrix<T> matrix<T>::operator+(const matrix<T>&M) const{//返回矩阵w=*this+m    if (theColumns != M.theColumns || theRows != M.theRows)    {        throw matrixSizeMismatch();    }    //生成相加后的矩阵    matrix<T> w(theColumns, theRows);    for (int i = 0; i < theColumns*theRows; i++)    {        w.element[i] = this->element[i] + M.element[i];    }    return w;}template<class T>matrix<T> matrix<T>::operator-(const matrix<T>&M) const{//返回矩阵w=*this+m    if (theColumns != M.theColumns || theRows != M.theRows)    {        throw matrixSizeMismatch();    }    //生成相加后的矩阵    matrix<T> w(theColumns, theRows);    for (int i = 0; i < theColumns*theRows; i++)    {        w.element[i] = this->element[i] - M.element[i];    }    return w;}template<class T>//乘号运算符重载matrix<T> matrix<T>::operator*(const matrix<T>&M) const{//矩阵乘法 返回结果矩阵w=(*this) * M    matrix<T> w(theRows, theColumns);; //结果矩阵    int ct = 0, cm = 0, cw = 0;    for (int i = 1; i <= theRows; i++)    {        for (int j = 1; j <= theColumns; j++)        {            T sum = element[ct] * M.element[cm];            for (int k = 2; k <= theColumns; k++)            {                ct++;//i行的下一项                cm += theColumns;//j列的下一项                sum += element[ct] * M.element[cm];            }            w.element[cw++] = sum;            //定位到该行起点与该列的下一列            cm = j;            ct -= theColumns - 1;        }        ct += theColumns;        cm = 0;    }    return w;}template<class T>matrix<T> matrix<T>:: operator+() const{    matrix<T> w(theRows, theColumns);    for (int i = 0; i < theColumns*theRows; i++)    {        w.element[i] = +element[i];    }    return w;}template<class T>matrix<T> matrix<T>:: operator-() const{    matrix<T> w(theRows, theColumns);    for (int i = 0; i < theColumns*theRows; i++)    {        w.element[i] = -element[i];    }    return w;}template<class T>matrix<T>& matrix<T>::operator+=(const T&x){    for (int i = 0; i < theColumns*theRows; i++)    {        element[i] += x;    }    return *this;}//template<class T>ostream &operator <<(ostream& out, const matrix<int>& M){    int k = 0;    for (int i = 0; i < M.theRows; i++)    {        for (int j = 0; j < M.theColumns; j++)        {            out << M.element[k++]<<" ";        }        out << endl;    }    return out;}
0 0
原创粉丝点击