CSparseMatrix.h文件
/*
*Copyright? 中国地质大学(武汉) 信息工程学院
*All right reserved.
*
*文件名称:CSparseMatrix.h
*摘 要:用三元组实现稀疏矩阵的表示及运算
*
*当前版本:1.0
*作 者:———
*完成日期:2018-04-29
*/
#pragma once
#ifndef CSPARSEMATRIX
#define CSPARSEMATRIX
#include<iostream>
using namespace std;
template<class _Ty>
struct CTrituple //三元组结构体定义
{
int m_nRowIndex; //行下标
int m_nColIndex; //列下标
_Ty m_tData; //数据
};
#define MAXSIZE 55
template<class _Ty>
class CSparseMatrix {
public:
CSparseMatrix(int size = MAXSIZE); //构造函数
~CSparseMatrix(); //析构函数
CSparseMatrix(CSparseMatrix<_Ty>& S); //复制构造函数
//赋值运算符
CSparseMatrix<_Ty>& operator = (CSparseMatrix<_Ty>& S);
void Transpose(CSparseMatrix<_Ty>& S); //转置函数-快速转置
// -重载加法运算符
CSparseMatrix<_Ty> operator + (CSparseMatrix<_Ty>& S);
// -重载乘法运算符
CSparseMatrix<_Ty> operator * (CSparseMatrix<_Ty>& S1);
//重载输入运算符
friend istream& operator >>(istream& in,
CSparseMatrix<_Ty>& S) {
int term, row, col;
int rindex, cindex, data;
cout << "依次输入矩阵的行与列大小:";
while (in >> row >> col) {
if (row < 1 || col < 1) {
cout << "行号或者列号个数小于1,请重新输入!" << endl;
continue;
}
else {
S.m_nRowSize = row;
S.m_nColSize = col;
break;
}
}
cout << "输入三元组的个数:";
while (in >> term) {
if (term > S.m_nMaxTerm) {
cout << "Term > m_nMaxTerm,请重新输入三元组的个数:" << endl;
continue;
}
else if (term < 1) {
cout << "Term < 1,请重新输入三元组的个数:" << endl;
continue;
}
else {
S.m_nTerm = term;
break;
}
}
cout << "依次输入三元组,格式为:行号 列号 数值" << endl;
int count = 0;
while (count < S.m_nTerm //应先判断是否count < S.m_nTerm
&& in >> rindex >> cindex >> data) { //不然会多输入一次无用的三元组
if (rindex < 0
|| rindex >= S.m_nRowSize
|| cindex < 0
|| cindex >= S.m_nColSize) {
cout << "元素下标越界,请重新输入该三元组:" << endl;
continue;
}
else {
S.m_tmElement[count].m_nRowIndex = rindex;
S.m_tmElement[count].m_nColIndex = cindex;
S.m_tmElement[count].m_tData = data;
c