#include
#define EPS 1.0E-8 //很小的数认为是零元素
class elem{ //定义矩阵元素类
int col;
float data;
public:
elem * next; //行链接
elem(int c,float d,elem * nx=NULL){col=c;data=d;next=nx;};
elem(){col=0;data=0;next=NULL;};
~elem(){cout<<"del "<
int getcol(){return col;}
int setcol(int c){return(col=c);};
float setdata(float d){return(data=d);};
};
class coeffmatrix{ //稀疏矩阵类
int N; //行数
int M; /颂橘/列数
elem ** Row; //行链接点列表 全零行指针为NULL
public:
coeffmatrix(int n,int m){
N=n;
M=m;
Row=new elem * [n];
for(int i=0;i
}
};
coeffmatrix(){ //无参构造函数 建立空矩阵
N=0;M=0;Row=NULL;
};
coeffmatrix(coeffmatrix & m){ //拷贝构造函数
N=m.N;M=m.M;Row=new elem * [N];
for(int i=0;i
Row[i]=NULL;
}else{
elem * p=m.Row[i];
elem * q=new elem(p->getcol(),p->getdata());
elem * s=q;
Row[i]=q;
p=p->next;
while(p){
q=new elem(p->getcol(),p->getdata());
s->next=q;
s=q;
p=p->next;
}
}
}
};
~coeffmatrix(){
if(N!=0){
if(Row!=NULL){
for(int i=0;i
delete Row[i];
Row[i]=NULL;
}
}
delete * Row;
Row=NULL;
}
}
}
bool reset(int n,int m){ //扩展行列数,原历樱族内容不破坏,
if(n==N && m==M)return true;
if(n
elem ** Rx=new elem * [n];
for(int i=0;i
Rx[i]=Row[i];
}else{
Rx[i]=NULL;
}
}
delete * p ;
Row=Rx;
N=n;M=m;
return true;
};
float set_elem(int r,int c,float d){ //元素赋值
if ((r>N || c>M)||(r<1||c<1))return 0;
if(Row==NULL)return 0;
if(d
elem * p;
p=Row[r-1];
if(p==NULL){
Row[r-1]=new elem(c,d);
return d;
}
do{
if(p->getcol()==c){
p->setdata(d);
return d;
}
if(p->next != NULL){
if(p->next->getcol()>c){
p->next=new elem(c,d,p->next);
return d;
}
}else{
p->next=new elem (c,d);
return d;
}
p=p->next;
}while(p);
};
float add_elem(int r,int c,float d){ //元素+数值
if(r>N || c>M)return 0;
if(d
p=Row[r-1];
if(p==NULL){
Row[r-1]=new elem(c,d);
return d;
}
while(p){
if(p->getcol()==c){
p->setdata(p->getdata()+d);
return p->getdata();
}
if(p->next != NULL){
if(p->next->getcol()>c){
p->next=new elem(c,d,p->next);
return d;
}else{
p=p->next;
}
}else{
p->next=new elem (c,d);
return d;
}
};
};
void add(coeffmatrix * mat){ //加 矩阵 ;可以行列数不同 ,
//如果需要必须行列数相同才能加,则将
//以下三行改为,行列数验证
int Nx=this->N>mat->NX()?this->N:mat->NX();
int Mx=this->M>mat->MX()?this->M:mat->MX();
reset(Nx,Mx);
//----
for(int i=1;i<=mat->NX();i++){
for(int j=1;j<=mat->MX();j++){
float d=mat->ELEM(i,j);
add_elem(i,j,d);
}
}
};
void sub(coeffmatrix * mat){ //减 矩阵
int Nx=this->N>mat->NX()?this->N:mat->NX();
int Mx=this->M>mat->MX()?this->M:mat->MX();
reset(Nx,Mx);
for(int i=1;i<=mat->NX();i++){
for(int j=1;j<=mat->MX();j++){
float d=mat->ELEM(i,j);
add_elem(i,j,-d);
}
}
};
int NX(){return N;};
int MX(){return M;};
float ELEM(int n,int m){ //取矩阵元素值 A[n,m]
float a=0;
if(n<=N && n>=1){
if(m<=M && m>=1){
elem * p=Row[n-1];
while(p){
if(p->getcol()==m){a=p->getdata();break; }
p=p->next;
}
}
}
return a;
};
void fset(int n,int m){ //强行重置矩阵行列,并所有行为空,即矩阵为全零阵
if(N!=0){
if(Row!=NULL){
for(int i=0;i
delete Row[i];
Row[i]=NULL;
}
}
delete * Row;
}
}
Row=new elem * [n];
for(int i=0;i
}
bool friend mul(coeffmatrix * result,coeffmatrix *m1,coeffmatrix *m2) //友元函数 矩阵乘法
{
if(m1->MX()!=m2->NX())return false;
int Mp=m1->MX();
int Nx=m1->NX();
int Mx=m2->MX();
result->fset(Nx,Mx);
for(int i=1;i<=Nx;i++){
for(int j=1;j<=Mx;j++){
float s=0;
for(int k=1;k<=Mp;k++){
s+=m1->ELEM(i,k)*m2->ELEM(k,j);
}
result->set_elem(i,j,s);
}
}
return true;
}
void show(){ //矩阵显示
cout<<"Matrix "<
for(int j=1;j<=M;j++){
cout<
cout<
}
};
int main()
{
int NN=5;
coeffmatrix m1(NN,NN);
coeffmatrix m2(NN,NN);
cout<<"setup the matrix"<
for(int j=i;j<=NN;j++){
m1.set_elem(i,j,i*j);
}
m2.set_elem(i,NN-i+1,1);
}
coeffmatrix m3;
m1.show();
m2.show();
mul(&m3,&m2,&m1);
m3.show();
m3.add(&m1)
m3.show();
}
(这几天事多点,才编出来调试通过,还不大完善,搂主自己再完善一些。希望对已有帮助)