稀疏矩阵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