数据结构-矩阵的压缩存储程序

数据结构-矩阵的压缩存储程序
数据结构-矩阵的压缩存储程序

实验报告

课程名:数据结构(C语言版)实验名:矩阵的压缩存储

姓名:

班级:

学号:

时间:2014.11.23

一实验目的与要求

1.掌握并实现稀疏矩阵的压缩存储的方法

2.在该存储方法上实现矩阵的操作

二实验容

?判断一个用二维数组存储的矩阵是不是稀疏矩阵?将其转化为压缩存储的形式

?在压缩存储上实现矩阵的乘法和转置操作

三实验结果与分析

压缩转置程序:

#include

//判断该矩阵是否为稀疏矩阵#define m 10

#define n 10

int a[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,0,0},

{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},

{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 three

{

int i,j;

int value;

};

struct three stu[100];

struct three1

{

int i,j;

int value;

};

struct three1 stu1[100];

int jiance()

{

int x=0;//赋初值为0

for(x=0;x<=99;x++)

{

.

stu[x].value=0;

}

float t=0;

float v;

for(int i=0;i

{

for(int j=0;j

{

if(a[i][j]!=0)

t++;

}

}

if((v=t/(m*n))<=0.05)

{

printf("该矩阵为稀疏矩阵%f\n",v);

return 1;

}

else

{

printf("该矩阵不是稀疏矩阵\n");

return 0;

}

}

void yasuo()

{

int t=0;

for(int r=0;r

{

for(int c=0;c

{

if(a[r][c]!=0)

{

stu[t].i=r;

stu[t].j=c;

stu[t].value=a[r][c];

t++;

}

}

}

}

void display()

{

int x=0;

printf("压缩矩阵的三元组为:\n");

for(x=0;x<=99;x++)

{

if(stu[x].value==0) break;

printf("{%d,%d,%d} ",stu[x].i,stu[x].j,stu[x].value);

}

printf("\n");

}

void zhuanzhi()

{

int x=0;//赋初值为0

int t=0;

int num[10]={0,0,0,0,0,0,0,0,0,0};//每一列非0的数目for(x=0;x<=99;x++)

{

stu1[x].value=0;

}

for(int j=0;j

{

for(int i=0;i

{

if(a[i][j]!=0)

{

num[j]++;

t++;

}

}

}

int cpot[10]={0,0,0,0,0,0,0,0,0,0}; cpot[0]=0;

for(j=1;j

{

cpot[j]=cpot[j-1]+num[j-1]; }

int col=0;

int q=0;

for(int k=0;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];

}

}

void display1()

{

int x=0;

printf("转置以后的三元组为:\n");

for(x=0;x<=99;x++)

{

if(stu1[x].value==0) break;

printf("{%d,%d,%d} ",stu1[x].i,stu1[x].j,stu1[x].value);

}

printf("\n");

}

void display2()

{

int d,b;

for(d=0;d

{

for(b=0;b

{

相关主题
相关文档
最新文档