C++面向对象程序设计之矩阵的相关运算

C++面向对象程序设计之矩阵的相关运算

一、实验任务及要求:

1)编写一个表示矩阵的类Matrix。矩阵元素为double类型。
2)改写默认构造函数,生成一个空矩阵,(不分配内存,矩阵大小为0*0)。
3)添加一个带参数的构造函数,传入参数为矩阵的大小,该构造分配矩阵内存,并初始化矩阵元素为0。矩阵元素按行序存储。
4)在析构函数中释放内存。
5)编写拷贝构造函数和拷贝赋值函数(operator=)。(注意用深拷贝)
6)编写成员函数SetValue,实现对矩阵的第i行第j列赋值。
7)编写成员函数GetValue,返回矩阵的第i行第j列的值。
8)编写成员函数GetRowCount,GetColCount,分别获取矩阵的行数和列数。
9)编写成员函数Multiply,实现矩阵的乘法运算,该函数的参数是一个Matrix对象的引用,返回一个新的Matrix对象,该对象是两个矩阵相乘的结果。如果矩阵维度不匹配,返回一个空矩阵。
10)编写成员函数Transpose,该函数返回一个新的矩阵,该矩阵是原矩阵的转置。
11)要求:
在指定的testMatrix.cpp中填写你的代码,不要修改main()函数。
12)提交内容包括:
A.本实验报告,请把实验报告文件名中的########替换为你的学号,XXX替换为你的姓名。
B.源代码。上述函数编写在同一个源文件中,源文件命名规则为CPP-3-########.cpp,其中########替换为你的学号。

二、把程序运行结果截图粘贴在下方:
在这里插入图片描述
三、把你完成的源文件插入到下方:

#include <bits/stdc++.h>
using namespace std;

/*请把下面的字符串分别改为你的姓名和学号*/
const char *name = " ";
const char *ID = " ";

/*在下面添加你的代码
类的声明和实现代码都写在下方*/
class Matrix {
private:
    int rows;//矩阵的行数
    int cols;//矩阵的列数
    double *pValue;//矩阵的数值

public:
    /*改写默认构造函数*/
    Matrix()
    {
        rows = 0;
        cols = 0;
        pValue = NULL;
    }
    /*构造函数*/
    Matrix(const int numRows, const int numCols)
    {
        rows = numRows;
		cols = numCols;
		pValue = new double[rows*cols];
		memset(pValue, 0, sizeof(double)*rows*cols);
    }
    /*在下方编写拷贝构造函数*/
    Matrix(const Matrix &is)
    {
		rows = is.rows;
		cols = is.cols;
		pValue = new double[rows*cols];
		memset(pValue, 0, sizeof(double)*rows*cols); 
		int i = 0;
		for (int r = 0; r < rows; ++r) {
			for (int c = 0; c < cols; ++c) {
				pValue[i] = is.pValue[i];
				i++;
			}
		}
	}
    /*拷贝赋值函数*/
    Matrix(int i, int j, double *value)
    {
		rows = i;
		cols = j;
		pValue = new double[i*j];
		int t = 0;
		for (int r = 0; r < rows; ++r) {
			for (int c = 0; c < cols; ++c) {
				pValue[t] = value[t];
				t++;
			}
		}
	}
    /*析构函数*/
    ~Matrix()
    {
        rows = 0;
		cols = 0;
		delete[]pValue;

    }
    /*在下方编写其它成员函数*/
    void SetValue(int i, int j, double num)
    {
		pValue[i*cols + j] = num;
	}

	double GetValue(int i, int j)
	{
		return pValue[i*cols + j];
	}

	int GetRowCount()
	{
		return rows;
	}

	int GetColCount()
	{
		return cols;
	}
    Matrix Multiply(const Matrix is2)
    {//矩阵的乘法运算;
		if (cols != is2.rows)
		{
			Matrix p;
			return p;
		}
		Matrix p(rows, is2.cols);
		for (int i = 0;i < rows;i++)
		{
			for (int j = 0;j < is2.cols;j++)
			{
				for (int k = 0;k < is2.rows;k++)
					p.pValue[i*is2.cols + j]+= pValue[i*cols + k] * is2.pValue[k*is2.cols + j];
			}
		}
		return p;
	}
    Matrix Transpose()
    {//矩阵的转置;
		Matrix p(cols, rows);
		int t = 0;
		for (int i = 0;i < rows;i++)
		{
			for (int j = 0;j < cols;j++)
			{
				p.pValue[t++] = pValue[j*rows + i];
			}
		}
		return p;
	}
    /*Display函数不用改写*/
    void Display() const
    {
        if (0 == rows || 0 == cols || NULL == pValue) {
            cout << "Empty Matrix" << endl;
        }
        else {
            int i = 0;
            for (int r = 0; r < rows; ++r) {
                for (int c = 0; c < cols; ++c) {
                    cout << pValue[i++] << ' ';
                }
                cout << endl;
            }
        }
    }
};


/*上述代码编写完成之后,请把下面的宏改为:
#define TEST 1
*/
#define TEST    1

/*=======================================================*/
/*以下代码不要修改*/
/*=======================================================*/
#define PI  3.1415926
int main(void) {

    cout << name<<" "<<ID << endl;

#if TEST
    Matrix Non;
    cout << "Non rows = " << Non.GetRowCount() << ", cols=" << Non.GetColCount() << endl;

    Matrix R(2, 2);
    double theta = PI / 4;
    R.SetValue(0, 0, cos(theta));
    R.SetValue(1, 0, -sin(theta));
    R.SetValue(0, 1, -R.GetValue(0,1));
    R.SetValue(1, 1, R.GetValue(0,0));
    R.Display();

    Matrix v(2, 1);
    v.SetValue(0, 0, sqrt(2.0)/2);
    v.SetValue(1, 0, sqrt(2.0)/2);

    Matrix w = R.Multiply(v);
    w.Display();

    Matrix vt = v.Transpose();
    Matrix wn = R.Multiply(vt);
    wn.Display();

#endif // TEST

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值