IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++ 基本语法 test1 -> 正文阅读

[C++知识库]c++ 基本语法 test1

基本内容

本文用c++实现矩阵基本操作
一、构造函数
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特殊情况的处理
四、友元函数
输入和输出运输符:<<, >>
五、作业提交方式
写完请发表博客,上传博客链接

代码实现

该矩阵的声明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);
	//  CMatrix& operator+(const CMatrix& m);
//  CMatrix operator+(const CMatrix& m1,const CMatrix& m2);
	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

矩阵实现 CMatrix.cpp

#include "CMatrix.h"
#include <fstream>
#include <assert.h>
CMatrix::CMatrix() : m_nRow(0), m_nCol(0), m_pData(0) // c++里初始化方式相当于括号内
{
	/*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; // this指向当前类
}
CMatrix::CMatrix(const char* strPath)
{
	m_pData = 0;
	m_nRow = m_nCol = 0;
	ifstream cin(strPath); // 输入文件流
	cin >> *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& 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 CMatrix::operator+(const CMatrix& m1,const CMatrix& m2)
	 //{
	 //  CMatrix m3(m1);
	 //  m3+=m2;
	 //  return m3;
	 //}
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;
}

主函数矩阵的简单操作示例

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

	return 0;
}

运行演示

在这里插入图片描述

reference

代码大多为老师课上代码,因为仅涉及c++的基本操作,且觉得没多少可以增加的,于是对代码加了部分觉得比较有用的注释。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-08 11:36:46  更:2021-10-08 11:38:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 2:05:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码