欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用指针数组实现的矩阵类

程序员文章站 2022-06-01 11:52:59
...

my_matrix.h

/*作业题目:
为矩阵设计操作符重载函数,包括(第三次作业):
    +
    *
    =
    >>
    <<
将类模板化(第四次作业)
作者:黄政明 21625080
时间:2018/03/29
*/
#ifndef MY_MATRIX_H
#define MY_MATRIX_H
#include <iostream>

using namespace std;

// Template matrix
//i.e.
//  my_matrix(int) mat(3,2);//creates a 3x2 dimension matrix and each entry is an integer.
//  my_matrix(int) mat;//a 1x1 matrix
template<typename Tdata>
class my_matrix
{
    private:
        int dim1;
        int dim2;
        //数据指针,类型为二重指针
        Tdata** m_data;
    public:
        my_matrix(int M = 1, int N = 1, Tdata entry = 0): dim1(M), dim2(N)
        {
            //初始化指针数组
            m_data = new Tdata*[M];
            //依次初始化指针数组的元素
            //指针数组,相邻指针指向的数组可以分散;数组指针,相邻指向的数组相邻
            for(int i = 0; i < M; i++)
            {
                m_data[i] = new Tdata[N];
                for(int j = 0; j < N; j++)
                    m_data[i][j] = entry;//初始化
            }
        }
        /*user defined copy function
        *param:
        *   src source instance to be copied
        */
        my_matrix(const my_matrix<Tdata>& src)
            : dim1(src.dim1), dim2(src.dim2)
        {
            m_data = new Tdata*[dim1];
            //依次初始化指针数组的元素
            for (int i = 0; i < dim1; i++)
            {
                m_data[i] = new Tdata[dim2];
                for (int j = 0; j < dim2; j++)
                    m_data[i][j] = src.m_data[i][j];//初始化
            }
        }
        ~my_matrix();
        //index an entry at row(i) column(j)
        //note i = 0, j = 0 point to first entry
        Tdata index(int i, int j)
        {
            return m_data[i][j];
        }

        /*************运算符重载*****************/
        //operator=
        void operator=(const my_matrix<Tdata>& mat)
        {
            if (dim1 != mat.dim1 || dim2 != mat.dim2)
            {
                cout << "Error: dimensions don't match! " << endl;
                exit(0);
            }
            for (int i = 0; i < dim1; i++)
            {
                for (int j = 0; j < dim2; j++)
                {
                    m_data[i][j] = mat.m_data[i][j];
                }
            }
        }
        //opeartor<<
        template<typename T>
        friend ostream& operator<<(ostream& output, my_matrix<T>& mat);
        //operator>>
        template<typename T>
        friend istream& operator>>(istream& input, my_matrix<T>& mat);
        //opearator+
        my_matrix<Tdata> operator+(my_matrix<Tdata>& mat2)
        {
            my_matrix<Tdata> ans(dim1, dim2, 100);
            for (int i = 0; i < dim1; i++)
            {
                for (int j = 0; j < dim2; j++)
                    ans.m_data[i][j] = m_data[i][j] + mat2.m_data[i][j];
            }
            return ans;
        };
        //operator-
        my_matrix operator-(my_matrix& mat2)
        {
            my_matrix ans(dim1, dim2);
            if((dim1 != mat2.dim1)||(dim2 != mat2.dim2))
            {
                cout << "Error: dimensions don't match! \n";
                return ans;
            }
            for(int i=0;i<dim1;i++)
            {
                for(int j=0;j<dim2;j++)
                {
                    ans.m_data[i][j] = m_data[i][j] - mat2.m_data[i][j];
                }
            }
            return ans;
        }
        //operator*
        my_matrix operator*(my_matrix& mat2)
        {
            my_matrix ans(dim1, mat2.dim2);
            int dim_m = dim1;
            int dim_k = dim2;
            int dim_n = mat2.dim2;
            if(dim2 != mat2.dim1)
            {
                cout << "Error: dimension dosen't match!" << endl;
                return ans;
            }
            for(int i = 0; i<dim_m; i++)
            {
                for(int j = 0; j < dim_n; j++)
                {
                    for(int k = 0; k < dim_k; k++)
                    {
                        ans.m_data[i][j] += m_data[i][k]*mat2.m_data[k][j];
                    }
                }
            }
            return ans;
        }

    protected:

    private:
};

template<typename T> my_matrix<T>::~my_matrix()
{
    //释放指针指向的内存
    for(int i = 0; i < dim1; i++)
    {
        delete[] m_data[i];
    }
}
/*************operator reload***********/
template<typename T>
ostream& operator<<(ostream& output, my_matrix<T>& mat)
{
    for(int i =0; i< mat.dim1; i++)
    {
        for(int j = 0; j < mat.dim2; j++)
        {
            output << mat.m_data[i][j] << ' ';
        }
        output << endl;
    }
    return output;
}
template<typename T>
istream& operator>>(istream& input, my_matrix<T>& mat)
{
    for(int i =0; i< mat.dim1; i++)
    {
        for(int j = 0; j < mat.dim2; j++)
        {
            input >> mat.m_data[i][j];
        }
    }
    return input;
}
#endif // my_matrix_H

work3.cpp

#include <iostream>
#include "my_matrix.h"

using namespace std;

//declare instance of template class classes
template my_matrix<int>;
template my_matrix<float>;

int main(void)
{
    my_matrix<int> mat1(3,4, 1);
    my_matrix<int> mat2(3,4, 2);
    my_matrix<int> mat(mat1);

    //检查拷贝构造函数
    cout << "mat1:\n" << mat1 << endl;
    cout << "mat2:\n" << mat2 << endl;
    cout << "mat(mat1): \n" << mat << endl;

    //检查赋值运算符
    cout << "mat = mat2: \n";
    mat = mat2;
    cout << mat;

    //检查加减法
    cout << "mat = mat1 + mat2: \n";
    mat = mat1 + mat2;
    cout << mat;
    //肩擦乘法
    cout << "mat = mat1 * mat2: \n";
    mat = mat1 * mat2;
    cout << mat;
    my_matrix<int> mat3(3, 3, 2);
    cout << "mat3: \n" << mat3;
    cout << "mat = mat3 * mat2: \n";
    mat = mat3*mat2;
    cout << mat;

    return 0;
}

运行结果
用指针数组实现的矩阵类