主题:[讨论]大家来看看这个程序是什么意思
prog.cpp
#include<iostream.h>
#include"sparsematrix.h"
void main()
{
sparsematrix<int>A(20),B(20),C(20);
cin>>A;
cout<<"matrix A is"<<endl;
cout<<A<<endl;
cin>>B;
cout<<"matrix B is"<<endl;
cout<<B<<endl;
A.transpose();
cout<<"the transpose of A is"<<endl;
cout<<A<<endl;
A.add(B);
cout<<"the sum of A and B is"<<endl;
cout<<C<<endl;
}
sparsematrix.h
#include<iostream.h>
#include<stdlib.h>
template<class T>
struct Trituple
{
int row,col;
T value;
Trituple<T>& operator=(Trituple<T>& x)
{
row=x.row;col=x.col;
value=x.value;
return *this;
}
};
template<class T>
class sparsematrix
{
friend ostream& operator<<(ostream& out,sparsematrix<T>& M);
friend istream& operator>>(istream& in,sparsematrix<T>& M);
public:
sparsematrix(int maxsz=100);
sparsematrix(sparsematrix<T>&x);
~sparsematrix()
{
delete []smArray;
}
sparsematrix<T>& operator=(sparsematrix<T>& x);
sparsematrix<T>transpose();
sparsematrix<T>add(sparsematrix<T>& b);
sparsematrix<T>multiply(sparsematrix<T>& b);
private:
int Rows,Cols,Terms;
Trituple<T> *smArray;
int maxTerms;
};
template<class T>
sparsematrix<T>::sparsematrix(int maxsz):maxTerms(maxsz)
{
if(maxsz<1)
{cerr<<"矩阵初始化值错!"<<endl;exit(1);}
smArray=new Trituple<T>[maxsz];
if(smArray==NULL)
{cerr<<"存储分配错!"<<endl;exit(1);}
Rows=Cols=Terms=0;
}
template<class T>
sparsematrix<T>::sparsematrix(sparsematrix<T>& x)
{
Rows=x.Rows;
Cols=x.Cols;
Terms=x.Terms;
maxTerms=x.maxTerms;
smArray=new Trituple<T>[maxTerms];
if(smArray==NULL)
{cerr<<"存储分配错!"<<endl;exit(1);}
for(int i=0;i<Terms;i++)
smArray[i]=x.smArray[i];
}
template<class T>
ostream& operator<<(ostream& out,sparsematrix<T>& 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;
}
template<class T>
istream& operator>>(istream& in,sparsematrix<T>& M)
{
int flag=1;
cout<<"enter number of rows,columns,and terms"<<endl;
in>>M.Rows>>M.Cols>>M.Terms;
if(M.Terms>M.maxTerms)
{cerr<<"number of terms overflow!"<<endl;exit(1);}
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;
if((M.Rows<=M.smArray[i].row)||(M.Cols<=M.smArray[i].col)||
(M.smArray[i].row<0)||(M.smArray[i].col<0))
{cout<<"边界出错:"<<endl;exit(0);}
if(M.smArray[i].value==0)
cout<<"输入的元素为0!"<<endl;
if(i>0)
if(M.smArray[i].row<M.smArray[i-1].row)
flag=0;
}
if(flag)
cout<<"按行主次序输入:"<<endl;
else
cout<<"不是按行主次序输入的:"<<endl;
return in;
}
template<class T>
sparsematrix<T> sparsematrix<T>::transpose()
{
sparsematrix<T> b(maxTerms);
b.Rows=Cols;
b.Cols=Rows;
b.Terms=Terms;
if(Terms>0){
int k,i,currentb=0;
for(k=0;k<Cols;k++)
for(i=0;i<Terms;i++)
if(smArray[i].col==k){
b.smArray[currentb].row=k;
b.smArray[currentb].col=smArray[i].row;
b.smArray[currentb].value=smArray[i].value;
currentb++;
}
}
return b;
}
template<class T>
sparsematrix<T> sparsematrix<T>::add(sparsematrix<T>& b)
{
sparsematrix<T> result;
if(Rows!=b.Rows||Cols!=b.Cols){
cout<<"incompatible matrices"<<endl;
return result;
}
int i=0,j=0,index_a,index_b;
result.Terms=0;
while(i<Terms&&j<b.Terms){
index_a=Cols*smArray[i].row+smArray[i].col;
index_b=Cols*b.smArray[j].row+b.smArray[j].col;
if(index_a<index_b){
result.smArray[result.Terms]=smArray[i];
i++;
}
else if(index_a>index_b){
result.smArray[result.Terms]=b.smArray[j];
j++;
}
else{
result.smArray[result.Terms]=smArray[i];
result.smArray[result.Terms].value=smArray[i].value+b.smArray[j].value;
i++;j++;
}
result.Terms++;
}
for(;i<Terms;i++){
result.smArray[result.Terms]=smArray[i];
result.Terms++;
}
for(;j<b.Terms;j++){
result.smArray[result.Terms]=b.smArray[i];
result.Terms++;
}
return result;
}
这个程序是关于稀疏矩阵的 哪位高手给解读下啊 怎样才能正确输入并得到正确结果啊 ~~~~