C++大作业--模板类Matrix with 加法、乘法

题目:

编写模板类matrix,支持任意数据类型的矩阵,重载+,-,*,=,==,[]等操作。利用int, double, 类complex(包含实数real,虚数virt两部分)等数据类型测试所写matrix类接口

题目:

编写模板类matrix,支持任意数据类型的矩阵,重载+,-,*,=,==,[]等操作。利用int, double, 类complex(包含实数real,虚数virt两部分)等数据类型测试所写matrix类接口

题目:

编写模板类matrix,支持任意数据类型的矩阵,重载+,-,*,=,==,[]等操作。利用int, double, 类complex(包含实数real,虚数virt两部分)等数据类型测试所写matrix类接口

h

/********************************************
CopyRight 2007 北京交通大学
程序名称: Matrix模板类
文件名: Matrix.h
修改日期: 2007-4-14 19:51:59
描述: 定义、实现了Matrix模板类
********************************************/

#include <iostream>

template <class T>
class Matrix
{
public:
    //构造函数、拷贝函数、赋值
    Matrix(int r, int c);
    Matrix(const Matrix<T> &M);
    Matrix();
    Matrix<T> operator=(const Matrix<T> &rMatrix);

    ~Matrix() { delete m_elem; }

    //输出矩阵
    void Print();

    int GetRow() { return m_row; }
    int GetCol() { return m_col; }

    //操作符的重载
    T &operator[](int pos);
    Matrix<T> operator+(const Matrix<T> &rMatrix);
    Matrix<T> operator*(const Matrix<T> &rMatrix);
    bool operator==(const Matrix<T> &rMatrix);

private:
    //指向数据区的指针
    T *m_elem;

    //记录行列
    int m_row, m_col;
};

template <class T>
Matrix<T>::Matrix(int r, int c)
    : m_row(r), m_col(c)
{
    if (m_col == 0 || m_row == 0)
        m_elem = NULL;
    else
    {
        m_elem = new T[m_row * m_col];
        if (!m_elem)
        {
            throw Error("无法分配内存");
        }

        cout << "创建一个" << m_row << "×" << m_col << "的矩阵..." << endl;
        for (int irow = 0; irow != m_row; ++irow)
        {
            cout << "请输入第" << irow + 1 << "行数据(每行" << m_col << "个)";
            for (int icol = 0; icol != m_col; ++icol)
            {
                cin >> m_elem[irow * m_col + icol];
            }
        }
    }
}

template <class T>
T &Matrix<T>::operator[](int pos)
{
    return m_elem[pos];
}

template <class T>
Matrix<T> Matrix<T>::operator+(const Matrix<T> &rMatrix)
{
    if (this->m_row != rMatrix.m_row || this->m_col != rMatrix.m_col)
        throw Error("不同型矩阵无法相加!");
    else
    {
        Matrix<T> mResult(*this);

        for (int i = 0; i != m_row * m_col; ++i)
        {
            mResult.m_elem[i] += rMatrix.m_elem[i];
        }
        return mResult;
    }
}

template <class T>
Matrix<T>::Matrix(const Matrix<T> &M)
{
    this->m_elem = new T[M.m_row * M.m_col];

    if (!m_elem)
    {
        throw Error("无法分配内存");
    }

    m_row = M.m_row;
    m_col = M.m_col;
    memcpy(m_elem, M.m_elem, sizeof(T) * m_row * m_col);
}

template <class T>
Matrix<T> Matrix<T>::operator=(const Matrix<T> &M)
{
    this->m_elem = new T[M.m_row * M.m_col];

    if (!m_elem)
    {
        throw Error("无法分配内存");
    }

    m_row = M.m_row;
    m_col = M.m_col;
    memcpy(m_elem, M.m_elem, sizeof(T) * m_row * m_col);

    return *this;
}

template <class T>
Matrix<T> Matrix<T>::operator*(const Matrix<T> &mRight)
{

    if (this->m_col != mRight.m_row)
        throw Error("两个矩阵不满足相乘条件!");
    else
    {
        Matrix<T> Result(0, 0);

        Result.m_elem = new T[m_row * mRight.m_col];
        Result.m_row = m_row;
        Result.m_col = mRight.m_col;
        memset(Result.m_elem, 0, sizeof(T) * m_row * mRight.m_col);

        for (int irow = 0; irow != Result.m_row; ++irow)
        {
            for (int icol = 0; icol != Result.m_col; ++icol)
            {
                //上述两层循环,确定要算出的Result中元素
                T sum = 0;

                //i,j为左右矩阵在各自数组中下标

                for (int i = irow * m_col, j = icol, cnt = 0; cnt != m_col; ++i, j += mRight.m_col, ++cnt)
                    sum += this->m_elem[i] * mRight.m_elem[j];

                Result.m_elem[irow * Result.m_col + icol] = sum;
            }
        }
        return Result;
    }
}

template <class T>
void Matrix<T>::Print()
{
    for (int i = 0; i != m_row * m_col; ++i)
    {

        cout << setw(4) << m_elem[i] << " ";

        if (!((i + 1) % m_col))
            cout << endl;
    }
}
template <class T>
Matrix<T>::Matrix()
{
    cout << "\n请输入行数 列数:";
    cin >> m_row >> m_col;

    m_elem = new T[m_row * m_col];
    if (!m_elem)
    {
        throw Error("无法分配内存");
    }

    cout << "创建一个" << m_row << "×" << m_col << "的矩阵..." << endl;
    for (int irow = 0; irow != m_row; ++irow)
    {
        cout << "请输入第" << irow << "行数据(每行" << m_col << "个)";
        for (int icol = 0; icol != m_col; ++icol)
        {

            cin >> m_elem[irow * m_col + icol];
        }
    }
}

template <class T>
bool Matrix<T>::operator==(const Matrix<T> &rMatrix)
{
    if (m_col != rMatrix.m_col || m_col != rMatrix.m_row)
    {
        return false;
    }
    else
    {
        for (int i = 0; i != m_row * m_col; ++i)
            if (m_elem[i] != rMatrix.m_elem[i])
            {
                return false;
            }
        return true;
    }
}

error

/********************************************
CopyRight 2007 北京交通大学
工程名称: Matrix模板类
文件名: Error.h
修改日期: 2007-4-14 20:04:07
描述: 描述和定义了错误类
********************************************/

#include <iostream>
#include <string>
using namespace std;

class Error
{
public:
    Error(const string &str) : m_err_info(str) {}
    void ShowErr() { cout << m_err_info; };

private:
    string m_err_info;
};

main

/********************************************
CopyRight 2007 北京交通大学
程序名称: Matrix模板类
文件名: main.cpp
修改日期: 2007-4-14 19:23:08
描述: main函数,测试加法,乘法,==等
********************************************/

#include <iostream>
#include <iomanip>
#include "Error.h"
#include "Matrix.h"
using namespace std;

int main()
{
    try
    {
        cout << "创建矩阵m1:";
        Matrix<double> m1;
        cout << "\n创建矩阵m2:";
        Matrix<double> m2;
        Matrix<double> m3(0, 0);

        cout << "计算m3=m1*m2\n";
        m3 = m1 * m2;

        cout << "\n输出矩阵m3:" << endl;
        m3.Print();

        cout << "\n创建矩阵m4:";
        Matrix<double> m4;
        Matrix<double> m5(0, 0);
        m5 = m3 + m4;

        cout << "\n计算m5=m3+m4:\n输出m5:" << endl;
        m5.Print();

        cout << "\n创建矩阵m6:";
        Matrix<double> m6;
        cout << "\nm5==m6:" << (m5 == m6);
    }
    catch (Error err)
    {
        cout << endl;
        err.ShowErr();
    }
    return 0;
}

 

2 thoughts on “C++大作业--模板类Matrix with 加法、乘法

  1. Anonymous

    感谢学长分享,10年过去,没想到王涛老师的作业都没更新过。。

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *