1、实验报告课程名:数据结构(C 语言版)实验名:矩阵的压缩存储姓 名: 班 级: 学 号: 时 间:2014.11.23一 实验目的与要求1. 掌握并实现稀疏矩阵的压缩存储的方法2. 在该存储方法上实现矩阵的操作二 实验内容 判断一个用二维数组存储的矩阵是不是稀疏矩阵 将其转化为压缩存储的形式 在压缩存储上实现矩阵的乘法和转置操作三 实验结果与分析压缩转置程序:#include/判断该矩阵是否为稀疏矩阵#define m 10#define n 10int amn=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,0,0,1,0,0,
2、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,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,0,0,0,0,0,0,0,0,0,0,;struct threeint i,j;int value;struct three stu100;struct three1int i,j;int value;struct three1 stu1100;int jiance()int x=0;/赋初值为 0for(x=0;x#define m1 3#define n1 4#d
3、efine m2 4#define n2 2int a1m1n1=3,0,0,5,0,-1,0,0,2,0,0,0,;int a2m2n2=0,2,1,0,-2,4,0,0,;struct three1int i,j;int value;struct three1 stu1100;struct three2int i,j;int value;struct three2 stu2100;struct three3int i,j;int value;struct three3 stu3100;int ar1posm1=0;int ar2posm2=0;int Qrposm1;int yasuo1(
4、)int t=0;ar1pos0=0;for(int r=0;rm1;r+)for(int c=0;cn1;c+)if(a1rc!=0)stu1t.i=r;stu1t.j=c;stu1t.value=a1rc;t+;ar1posr+1=t;return t;int yasuo2()int t=0;ar2pos0=0;for(int r=0;rm2;r+)for(int c=0;cn2;c+)if(a2rc!=0)stu2t.i=r;stu2t.j=c;stu2t.value=a2rc;t+;ar2posr+1=t;return t;void chengfa(int x1,int x2)int
5、a1m=0;int a2m=0;int tp,p,br,t,q,ccol;int qtu=0;for(a1m=0;a1mm1;a1m+)int ctempm1=0;if(a1m(m1-1)tp=ar1posa1m+1;elsetp=x1;for(p=ar1posa1m;ptp;+p)br=stu1p.j;if(br(m2-1)t=ar2posbr+1;elset=x2;for(q=ar2posbr;qt;+q)ccol=stu2q.j;ctempccol+=stu1p.value*stu2q.value;for(ccol=0;ccoln2;+ccol)if(ctempccol!=0)stu3q
6、tu.i=a1m;stu3qtu.j=ccol;stu3qtu.value=ctempccol;+qtu;void display()int x=0;printf(“a1 与 a2 乘积之后的三元组是:n“);for(x=0;x=99;x+)if(stu3x.value=0) break;printf(“%d,%d,%d “,stu3x.i,stu3x.j,stu3x.value);printf(“n“);void display1()int m,n;printf(“a1 矩阵为:n“);for(m=0;mm1;m+)for(n=0;nn1;n+)printf(“%d “,a1mn);printf(“n“);printf(“a2 矩阵为:n“);for(m=0;mm2;m+)for(n=0;nn2;n+)printf(“%d “,a2mn);printf(“n“);void main()int a;int x1,x2;display1();x1=yasuo1();x2=yasuo2();chengfa(x1,x2);display();图 2:矩阵的乘法程序程序运行结果