#include<iostream>
using namespace std;
template<class Type> class TripleList;
template<class Type>class Triple;
template<class Type>istream&operator >>(istream&in,TripleList<Type>&);
template<class Type>ostream&operator <<(ostream&out,TripleList<Type>&);
template<class Type>int CompareTriple(const Triple<Type>&a,const Triple<Type>&b);
template<class Type>TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
template<class Type>class Triple
{
friend class TripleList<Type>;
private:
int Row,Col;
Type Value;
friend istream&operator >>(istream&in,TripleList<Type>&);
friend ostream&operator <<(ostream&out,TripleList<Type>&);
friend int CompareTriple(const Triple<Type>&a,const Triple<Type>&b);
friend TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
};
template<class Type>class TripleList
{
private:
int Rows,Cols,Terms;
Triple<Type>*Array;
int ArraySize;
public:
TripleList(int MaxSize);
friend istream&operator >>(istream&in,TripleList<Type>&);
friend ostream&operator <<(ostream&out,TripleList<Type>&);
friend TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
};
int main()
{
TripleList<int> a(500);//构造A矩阵
TripleList<int> b(500);//构造B矩阵
cin>>a;
cout<<"矩阵A="<<endl;
cout<<a;
cin>>b;
cout<<"矩阵B="<<endl;
cout<<b;
cout<<"A+B="<<endl;
cout<<a+b;
system("pause");
return 0;
}
template<class Type>
TripleList<Type>::TripleList(int MaxSize)
{
if(MaxSize<1){
cout<<"初始化错误";
exit(0);
}
ArraySize=MaxSize;
Array=new Triple<Type>[MaxSize];
Terms=Rows=Cols=0;
}
template<class Type>
istream&operator >>(istream&in,TripleList<Type>&a)
{
int rs,cs,ts;
cout<<"请输入行数、列数、非零元个数"<<endl;
cin>>rs>>cs>>ts;
a.Rows=rs;
a.Cols=cs;
a.Terms=ts;
for(int i=0;i<a.Terms;i++)
{
int r,c,v;
cout<<""<<endl;
cin>>r>>c>>v;
a.Array[i].Row=r;
a.Array[i].Col=c;
a.Array[i].Value=v;
}
return in;
}
template<class Type>
ostream&operator <<(ostream&out,TripleList<Type>&a)
{
int n=0;
for(int i=1;i<=a.Rows;i++)
{
for(int j=1;j<=a.Cols;j++)
{
if(a.Array[n].Row==i&&a.Array[n].Col==j)
{
cout<<a.Array[n].Value<<' ';
n++;
}
else
{
cout<<'0'<<' ';
}
}
cout<<endl;
}
cout<<endl;
return out;
}
template<class Type>int CompareTriple<>(const Triple<Type>&a,const Triple<Type>&b)
{
if(a.Row==b.Row&&a.Col==b.Col)return 0;
else if(a.Row<b.Row||(a.Row==b.Row&&a.Col<b.Col))return -1;
else return 1;
}
template <class Type>TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b)
{
if(a.Rows!=b.Rows||a.Cols!=b.Cols)throw domain_error("非同类型不可相加");
TripleList<Type> c(500);
c.Rows=a.Rows;
c.Cols=a.Cols;
int i=0,j=0,k=0;
while(i<a.Terms&&j<b.Terms)
{
switch(CompareTriple(a.Array[i],b.Array[j])){
case -1:c.Array[k++]=a.Array[i++];break;
case 1:c.Array[k++]=b.Array[j++];break;
default:c.Array[k]=a.Array[i];
c.Array[k].Value+=b.Array[j].Value;
if(c.Array[k].Value!=0)k++;
i++;j++;break;
}
}
while(i<a.Terms)c.Array[k++]=a.Array[i++];
while(j<b.Terms)c.Array[k++]=b.Array[j++];
c.Terms=k;
return c;
}
using namespace std;
template<class Type> class TripleList;
template<class Type>class Triple;
template<class Type>istream&operator >>(istream&in,TripleList<Type>&);
template<class Type>ostream&operator <<(ostream&out,TripleList<Type>&);
template<class Type>int CompareTriple(const Triple<Type>&a,const Triple<Type>&b);
template<class Type>TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
template<class Type>class Triple
{
friend class TripleList<Type>;
private:
int Row,Col;
Type Value;
friend istream&operator >>(istream&in,TripleList<Type>&);
friend ostream&operator <<(ostream&out,TripleList<Type>&);
friend int CompareTriple(const Triple<Type>&a,const Triple<Type>&b);
friend TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
};
template<class Type>class TripleList
{
private:
int Rows,Cols,Terms;
Triple<Type>*Array;
int ArraySize;
public:
TripleList(int MaxSize);
friend istream&operator >>(istream&in,TripleList<Type>&);
friend ostream&operator <<(ostream&out,TripleList<Type>&);
friend TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b);
};
int main()
{
TripleList<int> a(500);//构造A矩阵
TripleList<int> b(500);//构造B矩阵
cin>>a;
cout<<"矩阵A="<<endl;
cout<<a;
cin>>b;
cout<<"矩阵B="<<endl;
cout<<b;
cout<<"A+B="<<endl;
cout<<a+b;
system("pause");
return 0;
}
template<class Type>
TripleList<Type>::TripleList(int MaxSize)
{
if(MaxSize<1){
cout<<"初始化错误";
exit(0);
}
ArraySize=MaxSize;
Array=new Triple<Type>[MaxSize];
Terms=Rows=Cols=0;
}
template<class Type>
istream&operator >>(istream&in,TripleList<Type>&a)
{
int rs,cs,ts;
cout<<"请输入行数、列数、非零元个数"<<endl;
cin>>rs>>cs>>ts;
a.Rows=rs;
a.Cols=cs;
a.Terms=ts;
for(int i=0;i<a.Terms;i++)
{
int r,c,v;
cout<<""<<endl;
cin>>r>>c>>v;
a.Array[i].Row=r;
a.Array[i].Col=c;
a.Array[i].Value=v;
}
return in;
}
template<class Type>
ostream&operator <<(ostream&out,TripleList<Type>&a)
{
int n=0;
for(int i=1;i<=a.Rows;i++)
{
for(int j=1;j<=a.Cols;j++)
{
if(a.Array[n].Row==i&&a.Array[n].Col==j)
{
cout<<a.Array[n].Value<<' ';
n++;
}
else
{
cout<<'0'<<' ';
}
}
cout<<endl;
}
cout<<endl;
return out;
}
template<class Type>int CompareTriple<>(const Triple<Type>&a,const Triple<Type>&b)
{
if(a.Row==b.Row&&a.Col==b.Col)return 0;
else if(a.Row<b.Row||(a.Row==b.Row&&a.Col<b.Col))return -1;
else return 1;
}
template <class Type>TripleList<Type> operator+(const TripleList<Type>&a,const TripleList<Type>&b)
{
if(a.Rows!=b.Rows||a.Cols!=b.Cols)throw domain_error("非同类型不可相加");
TripleList<Type> c(500);
c.Rows=a.Rows;
c.Cols=a.Cols;
int i=0,j=0,k=0;
while(i<a.Terms&&j<b.Terms)
{
switch(CompareTriple(a.Array[i],b.Array[j])){
case -1:c.Array[k++]=a.Array[i++];break;
case 1:c.Array[k++]=b.Array[j++];break;
default:c.Array[k]=a.Array[i];
c.Array[k].Value+=b.Array[j].Value;
if(c.Array[k].Value!=0)k++;
i++;j++;break;
}
}
while(i<a.Terms)c.Array[k++]=a.Array[i++];
while(j<b.Terms)c.Array[k++]=b.Array[j++];
c.Terms=k;
return c;
}