【殷人昆数据结构】第四章4.3 稀疏矩阵SparseMatrix代码的调试

本文详细介绍了稀疏矩阵SparseMatrix的实现,包括主函数、三元组类、构造函数、转置和快速转置函数、加法和乘法操作。在转置和加法函数中,探讨了如何通过三元组高效地操作矩阵元素,并解释了为何转置函数中要按列号确定新位置,以及加法中如何进行等效一维数组下标的计算。最后,文章强调了在遍历和操作稀疏矩阵时,理解元素顺序和遍历方法的重要性。
摘要由CSDN通过智能技术生成

稀疏矩阵SparseMatrix

主函数

#include <iostream>
#include "SparseMatrix.h"
#include "SparseMatrix.cpp"
using namespace std;

int main(){
   
	cout<<"+---------------------------------------------------------------+"<<endl;
	cout<<"#1 Construct the sparseMatrix :"<<endl;
	cout<<"pleae input the number of rows , columns and maxnonzero nodes : "<<endl;
	int r,c,t;
	cin>>r>>c>>t;
	SparseMatrix<int> m(t,r,c);
	cin>>m;
	cout<<"Construct Succeed!"<<endl;
	cout<<"The SparseMatrix you input is :"<<endl;
	cout<<m;
	cout<<"+---------------------------------------------------------------+"<<endl;
	cout<<"#2 Test void Transpose(SparseMatrix<E>& b)"<<endl;
	SparseMatrix<int> m1=m.Transpose();
	cout<<"New matrix is : "<<endl;
	cout<<m1<<endl;
	cout<<"+---------------------------------------------------------------+"<<endl;
	cout<<"#3 Test void FastTranspos(SparseMatrix<E>& b)"<<endl;
	SparseMatrix<int> m2=m.FastTranspos();
	cout<<"New matrix is : "<<endl;
	cout<<m2<<endl;
	cout<<"+---------------------------------------------------------------+"<<endl;
	cout<<"#4 Test SparseMatrix<E> Add(SparseMatrix<E>& b)"<<endl;
	cout<<"Input another matrix :"<<endl;
	cout<<"Rows = "<<m.getRows()<<" , Cols = "<<m.getCols()<<" Input the maxTerms : ";
	cin>>t;
	SparseMatrix<int> m3(t,r,c);
	cin>>m3;
	cout<<"The SparseMatrix you input is :"<<endl;
	cout<<m3;
	SparseMatrix<int> m4=m.Add(m3);
	cout<<"The Result is :"<<endl;
	cout<<m4;
	cout<<"+---------------------------------------------------------------+"<<endl;
	cout<<"#5 Test SparseMatrix<E> Multiply(SparseMatrix<E>& b)"<<endl;
	cout<<"Input another matrix :"<<endl;
	cout<<"Rows = "<<m.getCols()<<" , Cols = "<<m.getRows()<<" Input the maxTerms : ";
	cin>>t;
	SparseMatrix<int> m5(t,c,r);
	cin>>m5;
	cout<<"The SparseMatrix you input is :"<<endl;
	cout<<m5;
	SparseMatrix<int> m6=m.Multiply(m5);
	cout<<"The Result is :"<<endl;
	cout<<m6;
	return 0;
}

三元组类

const int drows = 6,  dcols = 7, dterms = 9;

template<typename E>
struct Triple		//三元组
{
   
    int row, col;	//非零元素行号、列号
    E value;		//非零元素的值
    void operator = (Triple<E> &R){
   
		row = R.row;
		col = R.col;
		value = R.value;
	}
	Triple(int r=-1, int c=-1, E v=0){
   
		row = r;
		col = c;
		value = v;
	}
};	

稀疏矩阵类

template <typename E>class SparseMatrix{
   
public: 
    SparseMatrix(int mT = dterms, int Rw = drows, int Cl = dcols);
	SparseMatrix(const SparseMatrix<E> &x);
	//有动态分配,必须定义复制构造函数和析构函数
	~SparseMatrix(){
   
		delete []smArray;
	}
    SparseMatrix<E> Transpose();
    SparseMatrix<E> FastTranspos();
    SparseMatrix<E> Add(SparseMatrix<E> &b);
    SparseMatrix<E> Multiply(SparseMatrix<E> &b);
	int getRows(){
   
		return Rows;
	}
	int getCols(){
   
		return Cols;
	}
	friend ostream& operator << (ostream &out, SparseMatrix<E> &M){
   
		out << "rows = " << M.Rows << endl;
		out << "cols = " << M.Cols << endl;
		out << "Nonzero terms = " << M.Terms << endl;
		for (int i = 0; i < M.Terms; i++)		{
   
			out << "M[" << M.smArray[i].row << "][" << M.smArray[i].col
				<< "]=" << M.smArray[i].value << endl;
		}
		return out;
	}
	friend istream& operator >> (istream &in, SparseMatrix<E> &M){
   
		cout<<"pleae input the number of nonzero nodes :";
		cin>>M.Terms;
		cout<<endl;
		for (int i = 0; i < M.Terms; i++){
   
			cout << "Enter row, column, and value of term:" << i+1 << endl;
			in >> M.smArray[i].row >> M.smArray[i].col>> M.smArray[i].value;
		}
		return in;
	}
	SparseMatrix<E> & operator = (SparseMatrix<E> &x){
    
		Rows = x.Rows;
		Cols = x.Cols;
		Terms = x.Terms;
		maxTerms = x.maxTerms;
		delete []smArray;                  //先清后建再复制
		smArray = new Triple<E>[maxTerms];
		assert(smArray);
		for(int i 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值