数据结构矩阵的压缩存储程序文档格式.docx
《数据结构矩阵的压缩存储程序文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构矩阵的压缩存储程序文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
![数据结构矩阵的压缩存储程序文档格式.docx](https://file1.bdocx.com/fileroot1/2022-10/11/04055318-2fbf-4daa-a09b-ebd2923a37a9/04055318-2fbf-4daa-a09b-ebd2923a37a91.gif)
#definem10
#definen10
inta[m][n]={
{1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,7,0},
{0,0,0,0,0,0,8,0,0,0},
};
structthree
{
inti,j;
intvalue;
};
structthreestu[100];
structthree1
structthree1stu1[100];
intjiance()
intx=0;
//赋初值为0
for(x=0;
x<
=99;
x++)
{
stu[x].value=0;
}
floatt=0;
floatv;
for(inti=0;
i<
m;
i++)
for(intj=0;
j<
n;
j++)
{
if(a[i][j]!
=0)
t++;
}
if((v=t/(m*n))<
=0.05)
printf("
该矩阵为稀疏矩阵%f\n"
v);
return1;
else
该矩阵不是稀疏矩阵\n"
);
return0;
}
voidyasuo()
intt=0;
for(intr=0;
r<
r++)
for(intc=0;
c<
c++)
if(a[r][c]!
{
stu[t].i=r;
stu[t].j=c;
stu[t].value=a[r][c];
}
voiddisplay()
intx=0;
printf("
压缩矩阵的三元组为:
\n"
for(x=0;
{
if(stu[x].value==0)break;
printf("
{%d,%d,%d}"
stu[x].i,stu[x].j,stu[x].value);
}
voidzhuanzhi()
intt=0;
intnum[10]={0,0,0,0,0,0,0,0,0,0};
//每一列非0的数目
stu1[x].value=0;
for(intj=0;
if(a[i][j]!
num[j]++;
t++;
}
intcpot[10]={0,0,0,0,0,0,0,0,0,0};
cpot[0]=0;
for(j=1;
cpot[j]=cpot[j-1]+num[j-1];
intcol=0;
intq=0;
for(intk=0;
k<
t;
k++)
col=stu[k].j;
q=cpot[col];
stu1[q].i=stu[k].j;
stu1[q].j=stu[k].i;
stu1[q].value=stu[k].value;
++cpot[col];
voiddisplay1()
转置以后的三元组为:
if(stu1[x].value==0)break;
stu1[x].i,stu1[x].j,stu1[x].value);
voiddisplay2()
intd,b;
for(d=0;
d<
d++)
for(b=0;
b<
b++)
%d"
a[d][b]);
voidmain()
display2();
if(jiance()==1)
yasuo();
display();
zhuanzhi();
display1();
}
图1:
压缩转置程序运行结果
矩阵的乘法程序:
#definem13
#definen14
#definem24
#definen22
inta1[m1][n1]={
{3,0,0,5},
{0,-1,0,0},
{2,0,0,0},
};
inta2[m2][n2]={
{0,2},
{1,0},
{-2,4},
{0,0},
structthree2
structthree2stu2[100];
structthree3
structthree3stu3[100];
intar1pos[m1]={0};
intar2pos[m2]={0};
intQrpos[m1];
intyasuo1()
ar1pos[0]=0;
m1;
n1;
if(a1[r][c]!
stu1[t].i=r;
stu1[t].j=c;
stu1[t].value=a1[r][c];
ar1pos[r+1]=t;
returnt;
intyasuo2()
ar2pos[0]=0;
m2;
n2;
if(a2[r][c]!
stu2[t].i=r;
stu2[t].j=c;
stu2[t].value=a2[r][c];
ar2pos[r+1]=t;
voidchengfa(intx1,intx2)
{
inta1m=0;
inta2m=0;
inttp,p,br,t,q,ccol;
intqtu=0;
for(a1m=0;
a1m<
a1m++)
intctemp[m1]={0};
if(a1m<
(m1-1))
tp=ar1pos[a1m+1];
else{tp=x1;
for(p=ar1pos[a1m];
p<
tp;
++p)
br=stu1[p].j;
if(br<
(m2-1))
t=ar2pos[br+1];
else{t=x2;
for(q=ar2pos[br];
q<
++q)
ccol=stu2[q].j;
ctemp[ccol]+=stu1[p].value*stu2[q].value;
}
for(ccol=0;
ccol<
++ccol)
if(ctemp[ccol]!
stu3[qtu].i=a1m;
stu3[qtu].j=ccol;
stu3[qtu].value=ctemp[ccol];
++qtu;
a1与a2乘积之后的三元组是:
if(stu3[x].value==0)break;
stu3[x].i,stu3[x].j,stu3[x].value);
intm,n;
a1矩阵为:
for(m=0;
m<
m++)
for(n=0;
n<
n++)
a1[m][n]);
a2矩阵为:
a2[m][n]);
inta;
intx1,x2;
display1();
x1=yasuo1();
x2=yasuo2();
chengfa(x1,x2);
display();
图2:
矩阵的乘法程序程序运行结果
WelcomeTo
Download!
!
欢迎您的下载,资料仅供参考!