数据结构作业稀疏矩阵运算器.docx
《数据结构作业稀疏矩阵运算器.docx》由会员分享,可在线阅读,更多相关《数据结构作业稀疏矩阵运算器.docx(24页珍藏版)》请在冰豆网上搜索。
数据结构作业稀疏矩阵运算器
实习报告
题目:
稀疏矩阵运算器
编译环境:
MicrosoftVisualStudio2010
功能实现:
以三元组顺序表输入稀疏矩阵
实现两个稀疏矩阵相加、相减和相乘的运算
运算结果的矩阵以阵列形式列出
以十字链表表示稀疏矩阵
一、需求分析
1.稀疏矩阵是指那些多数元素为零的矩阵,利用“稀疏”的特点进行存储和计算可以大大节省存储空间,提高计算效率。
而本程序是以“带行逻辑链接信息”的三元顺序表表示稀疏矩阵,实现两个矩阵的相加、相减和相乘的运算。
稀疏矩阵的输入形式采用三元组表示,而运算结果的矩阵则以通常的阵列形式列出。
除此之外,程序还采用十字链表表示稀疏矩阵的。
2.程序的测试数据:
二、概要设计
1.本程序的基本算法设计:
a)首先输入矩阵的行数和列数,并判断给出的两个矩阵的行、列对于所要求的运算是否匹配。
b)对于三元组的输入顺序是按照行优先的限制进行矩阵输入的。
2.数据结构设计:
typedefstructNode{
inti,j;//非零元素的行和列
doublee;//非零元素的值
Node*right;//非零元素行表的后继
Node*down;//非零元素列表的后继
}Node,*NLink;
classCList
{
private:
NLink*rhead;//行的头指针
NLink*dhead;//列的头指针
intrNum;//行数
intdNum;//列数
public:
CList();
virtual~CList();
boolInsertNode(inti,intj,doublee);
boolCreateMatrix();
voidShowMatrix();
boolAddMatrix(CList&Matrix);
boolSubMatrix(CList&Matrix);
boolMultiMatrix(CList&Matrix,CList*&Result);
};
三、详细设计
1.主要操作函数的实现:
(1)插入函数:
在已经初始化的稀疏矩阵中插入某个元素。
boolCList:
:
InsertNode(inti,intj,doublee)
{
Node*p,*q;
if(i>rNum||j>dNum||i<0||j<0)
{
cout<<"\tInvalidrownumber!
InsertNodeFailed!
"<returnTRUE;
}
if(i==0)
{
cout<<"\tEndInputting!
"<returnTRUE;
}
if(e==0)
{
returnTRUE;
}
//创建矩阵元素
p=newNode;
if(!
p)
{
cout<<"\tInsertNodeError!
"<returnFALSE;
}
p->i=i;
p->j=j;
p->e=e;
//在行表中寻找插入位置
if(rhead[i]==NULL||rhead[i]->j>j)
{
p->right=rhead[i];
rhead[i]=p;
}
else
{
q=rhead[i];
while((q->right)&&q->right->j{
q=q->right;
}
p->right=q->right;
q->right=p;
}
//在列表中寻找插入位置
if(dhead[j]==NULL||dhead[j]->i>i)
{
p->down=dhead[j];
dhead[j]=p;
}
else
{
q=dhead[j];
while((q->down)&&q->down->i
{
q=q->down;
}
p->down=q->down;
q->down=p;
}
returnTRUE;
}
(2)矩阵创建函数:
根据用户的输入,创建稀疏矩阵。
boolCList:
:
CreateMatrix()
{
intm,n;
inti,j;
doublee;
cout<<"\tPleaseinputthenumberofrow:
";
cin>>m;
cout<cout<<"\tPleaseinputthenumberofcol:
";
cin>>n;
cout<rNum=m;
dNum=n;
rhead=newNLink[rNum+1];
dhead=newNLink[dNum+1];
for(i=0;i{
rhead[i]=NULL;
}
for(i=0;i{
dhead[i]=NULL;
}
cout<<"\tPleaseinputnodeas(i,j,e),endwith(i=0):
";
cin>>i>>j>>e;
cout<while(i!
=0)
{
InsertNode(i,j,e);
cout<<"\tPleaseinputnodeas(i,j,e),endwith(i=0):
";
cin>>i>>j>>e;
cout<}
returnTRUE;
}
(3)矩阵相加函数:
实现两个稀疏矩阵的相加运算。
boolCList:
:
AddMatrix(CList&Matrix)
{
if(Matrix.rNum!
=rNum||Matrix.dNum!
=dNum)
{
cout<<"ErrorMatrixdimension!
"<returnFALSE;
}
Node*pa,*pb,*pre;
NLink*h1=newNLink[dNum+1];
inti,j;
for(i=1;i<=rNum;i++)
{
for(j=1;j{
h1[j]=dhead[j];
}
pre=NULL;
pa=rhead[i];
pb=Matrix.rhead[i];
while(pb!
=NULL)
{
if(pa==NULL||pa->j>pb->j)
{
Node*p=newNode;
p->i=pb->i;
p->j=pb->j;
p->e=pb->e;
//更新行指针
if(pre==NULL)
{
rhead[p->i]=p;
}
else
{
pre->right=p;
}
p->right=pa;
pre=p;
//更新列指针
if(dhead[p->j]==NULL||dhead[p->j]->i>p->i)
{
p->down=dhead[p->j];
dhead[p->j]=p;
}
else
{
while(h1[p->j]->down&&h1[p->j]->down->ii)
{
h1[p->j]=h1[p->j]->down;
}
if(h1[p->j]->down==NULL)
{
h1[p->j]->down=p;
}
else
{
p->down=h1[p->j]->down;
h1[p->j]->down=p;
}
}
pb=pb->right;
}
elseif(pa!
=NULL&&pa->jj)
{
pre=pa;
pa=pa->right;
}
elseif(pa->j==pb->j)
{
pa->e+=pb->e;
if(pa->e==0)
{
Node*p=newNode;
//更新行指针
if(pre==NULL)
{
rhead[pa->i]=pa->right;
}
else
{
pre->right=pa->right;
}
p=pa;
pa=pa->right;
//更新列指针
if(dhead[p->j]==p)
{
dhead[p->j]=h1[p->j]=p->down;
}
else
{
while(h1[p->j]->down&&h1[p->j]->down->i!
=p->i)
{
h1[p->j]=h1[p->j]->down;
}
h1[p->j]->down=p->down;
}
}
pb=pb->right;
}
}
}
returnTRUE;
}
(4)矩阵相减函数:
实现两个稀疏矩阵的相减运算。
boolCList:
:
SubMatrix(CList&Matrix)//Sub
{
if(Matrix.rNum!
=rNum||Matrix.dNum!
=dNum)
{
cout<<"ErrorMatrixdimension!
"<returnFALSE;
}
Node*pa,*pb,*pre;
NLink*h1=newNLink[dNum+1];
inti,j;
for(i=1;i<=rNum;i++)
{
for(j=1;j{
h1[j]=dhead[j];
}
pre=NULL;
pa=rhead[i];
pb=Matrix.rhead[i];
while(pb!
=NULL)
{
if(pa==NULL||pa->j>pb->j)
{
Node*p=newNode;
p->i=pb->i;
p->j=pb->j;
p->e=-(pb->e);
//更新行指针
if(pre==NULL)
{
rhead[p->i]=p;
}
else
{
pre->right=p;
}
p->right=pa;
pre=p;
//更新列指针
if(dhead[p->j]==NULL||dhead[p->j]->i>p->i)
{
p->down=dhead[p->j];
dhead[p->j]=p;
}
else
{
while(h1[p->j]->down&&h1[p->j]->down->ii)
{
h1[p->j]=h1[p->j]->down;
}
if(h1[p->j]->down==NULL)
{
h1[p->j]->down=p;
}
else
{
p->down=h1[p->j]->down;
h1[p->j]->down=p;
}
}
pb=pb->right;
}
elseif(pa!
=NULL&&pa->jj)
{
pre=pa;
pa=pa->right;
}
elseif(pa->j==pb->j)
{
pa->e-=pb->e;
if(pa->e==0)
{
Node*p=newNode;
//更新行指针
if(pre==NULL)
{
rhead[pa->i]=pa->right;
}
else
{
pre->right=pa->right;
}
p=pa;
pa=pa->right;
//更新列指针
if(dhead[p->j]==p)
{
dhead[p->j]=h1[p->j]=p->down;
}
else
{
while(h1[p->j]->down&&h1[p->j]->down->i!
=p->i)
{
h1[p->j]=h1[p->j]->down;
}
h1[p->j]->down=p->down;
}
}
pb=pb->right;
}
}
}
returnTRUE;
}
(5)矩阵相乘函数:
实现两个稀疏矩阵的相乘运算。
boolCList:
:
MultiMatrix(CList&Matrix,CList*&Result)
{
inti,j;
if(dNum!
=Matrix.rNum)
{
cout<<"Matrixdimensionsdon'tagree!
"<returnFALSE;
}
Result=newCList;
Result->rNum=rNum;
Result->dNum=Matrix.dNum;
Result->dhead=newNLink[Matrix.dNum+1];
Result->rhead=newNLink[rNum+1];
for(i=0;i{
Result->rhead[i]=NULL;
}
for(i=0;i{
Result->dhead[i]=NULL;
}
double*tmp=newdouble[Matrix.dNum+1];
Node*acur,*bcur;
for(i=1;i<=rNum;i++)
{
for(j=1;j<=Matrix.dNum;j++)
{
tmp[j]=0;
}
acur=rhead[i];
while(acur)
{
j=acur->j;
bcur=Matrix.rhead[j];
while(bcur){
tmp[bcur->j]+=acur->e*bcur->e;
bcur=bcur->right;
}
acur=acur->right;
}
for(j=1;j<=Matrix.dNum;j++)
{
if(tmp[j]!
=0)
{
Result->InsertNode(i,j,tmp[j]);
}
}
}
delete[]tmp;
returntrue;
returnTRUE;
}
2.主函数的实现:
voidmain()
{
charOperation;
do
{
cout<<"Pleaseselect('+,'-','*'),Quit(Q):
";
cin>>Operation;
cout<if(Operation=='Q')
break;
if(Operation!
='+'&&Operation!
='-'&&Operation!
='*')
continue;
CLista;
CListb;
CList*c=NULL;
cout<<"PleaseEnterMatrixA:
"<a.CreateMatrix();
a.ShowMatrix();
cout<<"PleaseEnterMatrixB:
"<b.CreateMatrix();
cout<<"B="<b.ShowMatrix();
switch(Operation)
{
case'+':
if(a.AddMatrix(b))
{
cout<a.ShowMatrix();
}
break;
case'-':
if(a.SubMatrix(b))
{
cout<<"A-B="<a.ShowMatrix();
}
break;
case'*':
if(a.MultiMatrix(b,c))
{
cout<<"A*B="<c->ShowMatrix();
}
break;
}
}while
(1);
}
四、用户手册
1.本程序的执行文件为:
SparseMatrix.exe
2.进入程序的用户界面,并根据程序提示,输入需要的运算:
3.根据程序提示,输入运算矩阵(其中,输入行数为零的三元组值,表示该矩阵输入完成):
4.显示运算结果:
五、测试结果
程序的主要测试结果如下:
1.矩阵相加:
2.矩阵相减:
3.矩阵相乘:
六、附录
源程序文件清单:
CList.h//十字链表类以及主要函数的声明
CList.cpp//十字链表类成员函数的实现
SparseMatrix.cpp//主程序