C++实验1 CMatrix类设计与实现

CMatrix类设计与实现

实验基本内容


CMatrix(): 不带参数的构造函数;
CMatrix(int nRow, int nCol, double *pData=NULL) : 带行、列及数据指针等参数的构造函数,并且参数带默认值;
CMatrix(const char * strPath): 带文件路径参数的构造函数;
CMatrix(const CMatrix& m): 拷贝构造函数
此外会用列表初始化成员变量:CMatrix(): m_nRow(0), m_nCol(0), m_pData(NULL);
bool Create(int nRow, int nCol, double *pData=NULL): 先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。
二、析构函数
~CMatrix(): 调用Release();
Release(): 将内存释放,并将行列设置为0;
三、运算符重载
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理
四、友元函数
输入和输出运输符:<<, >>
五、作业提交方式
写完请发表博客,上传博客链接

源码实现:

1.main.cpp

#include <iostream>
 /* run this program using the console pauser or add your own getch, system("pause")*/
#include <stdio.h>
#include "CMatrix.h"
#include "CMatrix.cpp"
#include <direct.h>
using namespace std;
int main(int argc, char** argv) {
	double pData[10] = { 2,3,4,5 };
	CMatrix m3("1.txt");
	CMatrix m1, m2(2, 5, pData),  m4(m2);
	
	cout << "输入矩阵行 列 数值:" << endl;
	cin >> m1;
	m2.Set(1, 3, 10);
	cout <<"m1="<< m1 <<"m2="<< m2 <<"m3="<< m3 <<"m4="<< m4;
	m4 = m3;
	m4[2] = m4 + 1;
	if (m4 == m3)
	{
		cout << "Error !" << endl;
	}
	m4 += m3;
	cout <<"\nafter:\n"<<endl;
	cout <<"m1="<< m1 <<"m2="<< m2 <<"m3="<< m3 <<"m4="<< m4;
	cout << "sum of m4 = " << (double)m4 << endl;

	return 0;
}

2.CMATRIX.H

#ifndef CMATRIX_H
#define CMATRIX_H
#include <iostream>
using namespace std;
class CMatrix
{
public:
	CMatrix(); //构造函数
	CMatrix(int nRow, int nCol, double* pData = NULL);
	CMatrix(const CMatrix& m);
	CMatrix(const char* strPath);
	~CMatrix(); //析构函数
	bool Create(int nRow, int nCol, double* pData = NULL); //初始化
	void Set(int nRow, int nCol, double dVale);
	void Release(); 
	friend istream& operator>>(istream& is, CMatrix& m); //友元函数
	friend ostream& operator<<(ostream& os, const CMatrix& m);
	CMatrix& operator=(const CMatrix& m);
	CMatrix& operator+=(const CMatrix& m);
	double& operator[](int nIndex);
	double& operator()(int nRow, int nCol);
	bool operator ==(const CMatrix& m);
	bool operator !=(const CMatrix& m);
	operator double();
private:
	int m_nRow; 
	int m_nCol;
	double* m_pData;
};
CMatrix operator+(const CMatrix& m1, const CMatrix& m2); 

inline void CMatrix::Set(int nRow, int nCol, double dVal)
{
	m_pData[nRow * m_nCol + nCol] = dVal;
}
#endif

3. CMATRIX.CPP

#include "CMatrix.h"
#include <fstream>
#include <assert.h>
CMatrix::CMatrix() : m_nRow(0), m_nCol(0), m_pData(0) 
{
	/*m_nRow = 0;
	m_nCol = 0;
	m_pData = 0;*/
}
CMatrix::CMatrix(int nRow, int nCol, double* pData) : m_pData(0)
{
	Create(nRow, nCol, pData);
}
CMatrix::CMatrix(const CMatrix& m) : m_pData(0)
{
	*this = m; 
}
CMatrix::CMatrix(const char* strPath)
{
	m_pData = 0;
	m_nRow = m_nCol = 0;
	ifstream cin(strPath); // 输入文件流
	cin >> *this;
	//cout << *this;
}
CMatrix::~CMatrix()
{
	Release();
}
bool CMatrix::Create(int nRow, int nCol, double* pData)
{
	Release();
	m_pData = new double[nRow * nCol];
	m_nRow = nRow;
	m_nCol = nCol;
	if (pData)
	{
		memcpy(m_pData, pData, nRow * nCol * sizeof(double));
		return true;
	}
	return false;
}
void CMatrix::Release()
{
	if (m_pData)
	{
		delete[]m_pData;
		m_pData = NULL;
	}
	m_nRow = m_nCol = 0;
}
CMatrix& CMatrix::operator=(const CMatrix& m)
{
	if (this != &m) { 
		Create(m.m_nRow, m.m_nCol, m.m_pData);

	}
	return *this;
}
CMatrix& CMatrix::operator+=(const CMatrix& m)
{
	assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		m_pData[i] += m.m_pData[i];
	}
	return *this;
}

CMatrix operator+(const CMatrix& m1, const CMatrix& m2)
{
	CMatrix m3(m1);
	m3 += m2;
	return m3;
}
double& CMatrix::operator[](int nIndex)
{
	assert(nIndex < m_nRow * m_nCol);
	return m_pData[nIndex];
}
double& CMatrix::operator()(int nRow, int nCol)
{
	assert(nRow * m_nCol + nCol < m_nRow * m_nCol);
	return m_pData[nRow * m_nCol + nCol];
}
bool CMatrix::operator == (const CMatrix& m)
{
	if (!(m_nRow == m.m_nRow && m_nCol == m.m_nCol))
	{
		return false;
	}
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		if (m_pData[i] != m.m_pData[i])
		{
			return false;
		}
	}
	return true;
}
bool CMatrix::operator !=(const CMatrix& m)
{
	return !((*this) == m);
}
CMatrix::operator double()
{
	double dS = 0;
	for (int i = 0; i < m_nRow * m_nCol; i++)
	{
		dS += m_pData[i];
	}
	return dS;
}
// 对CMatrix的cin,cout
istream& operator>>(istream& is, CMatrix& m)
{
	is >> m.m_nRow >> m.m_nCol;
	m.Create(m.m_nRow, m.m_nCol);
	for (int i = 0; i < m.m_nRow * m.m_nCol; i++)
	{
		is >> m.m_pData[i];
	}
	return is;
}
ostream& operator<<(ostream& os, const CMatrix& m)
{
	os << m.m_nRow << " " << m.m_nCol << endl;
	double* pData = m.m_pData;
	for (int i = 0; i < m.m_nRow; i++)
	{
		for (int j = 0; j < m.m_nCol; j++)
		{
			os << *pData++ << " ";
		}
		os << endl;
	}
	return os;
}

实验过程中遇到的问题:

1、调试过程中出现:Assertion failed!的问题,上网一查我以为是文件没读出来的问题,在读入文件流函数加了cout << *this,发现能正常输出内容,再次编译运行竟然通过了,不知道哪里出的错。

2、在写代码的时候遇到问题:[Error] ...is private within this context 发现大部分函数和变量为私有的,返回看源代码,发现忘记加上public:,添加后问题解决

实验结果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的CMatrix类的实现,包含了加、减、点乘、点除运算的成员函数和运算符重载: ```c++ #include <iostream> #include <vector> using namespace std; class CMatrix { private: vector<vector<double>> mat; int rows, cols; public: // 构造函数 CMatrix(int r, int c) : rows(r), cols(c) { mat.resize(rows); for (int i = 0; i < rows; i++) { mat[i].resize(cols); } } // 获取矩阵元素 double& operator()(int i, int j) { return mat[i][j]; } // 矩阵加法 CMatrix operator+(CMatrix& other) { CMatrix res(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { res(i, j) = mat[i][j] + other(i, j); } } return res; } // 矩阵减法 CMatrix operator-(CMatrix& other) { CMatrix res(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { res(i, j) = mat[i][j] - other(i, j); } } return res; } // 矩阵点乘 CMatrix operator*(CMatrix& other) { if (cols != other.rows) { throw "Invalid matrix dimensions!"; } CMatrix res(rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < other.cols; j++) { double sum = 0; for (int k = 0; k < cols; k++) { sum += mat[i][k] * other(k, j); } res(i, j) = sum; } } return res; } // 矩阵点除 CMatrix operator/(CMatrix& other) { if (cols != other.cols || rows != other.rows) { throw "Invalid matrix dimensions!"; } CMatrix res(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { res(i, j) = mat[i][j] / other(i, j); } } return res; } }; int main() { CMatrix a(2, 2); a(0, 0) = 1; a(0, 1) = 2; a(1, 0) = 3; a(1, 1) = 4; CMatrix b(2, 2); b(0, 0) = 5; b(0, 1) = 6; b(1, 0) = 7; b(1, 1) = 8; // 矩阵加法 CMatrix c = a + b; cout << "加法结果:" << endl; for (int i = 0; i < c.rows; i++) { for (int j = 0; j < c.cols; j++) { cout << c(i, j) << " "; } cout << endl; } // 矩阵减法 CMatrix d = a - b; cout << "减法结果:" << endl; for (int i = 0; i < d.rows; i++) { for (int j = 0; j < d.cols; j++) { cout << d(i, j) << " "; } cout << endl; } // 矩阵点乘 CMatrix e = a * b; cout << "点乘结果:" << endl; for (int i = 0; i < e.rows; i++) { for (int j = 0; j < e.cols; j++) { cout << e(i, j) << " "; } cout << endl; } // 矩阵点除 CMatrix f = a / b; cout << "点除结果:" << endl; for (int i = 0; i < f.rows; i++) { for (int j = 0; j < f.cols; j++) { cout << f(i, j) << " "; } cout << endl; } return 0; } ``` 运行结果: ``` 加法结果: 6 8 10 12 减法结果: -4 -4 -4 -4 点乘结果: 19 22 43 50 点除结果: 0.2 0.333333 0.428571 0.5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值