特殊矩阵的压缩与解压缩参考模板_第1页
特殊矩阵的压缩与解压缩参考模板_第2页
特殊矩阵的压缩与解压缩参考模板_第3页
特殊矩阵的压缩与解压缩参考模板_第4页
特殊矩阵的压缩与解压缩参考模板_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课程名称: 数据结构课程设计课程设计题目: 特殊矩阵压缩与解压缩姓 名:院系: 计算机科学与技术学院 专 业: 计算机科学与技术 年 级: 2011级 学 号:指导教师: 王爱平2013 年 9月 14 日 1 / 18目 录1 课程设计的目的32 需求分析3 3 课程设计报告内容3 3.1概要设计33.2详细设计33.3调试分析4 3.4用户手册4 3.5测试结果4 4 小结 4 5程序清单4 6 参考文献 157 程序截图 15 1.课程设计的目的(1) 熟练使用 C 语言编写程序,解决实际问题;(2) 了解并掌握数据结构与算法的设计方法,具备初步的独立分析和设计能力;(3) 初步掌握软件

2、开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;(4) 提高综合运用所学的理论知识和方法独立分析和解决问题的能力;2.需求分析(1)对对称、三角、带状等特殊矩阵进行压缩,以节省存储空间。(2)对压缩后的矩阵进行解压缩3 特殊矩阵的压缩与解压缩的设计3.1概要设计压缩矩阵数据结构:typedef struct ElemType *elem;int amount;int bound;int w;int kind;SpecialMatrix;3.2详细设计int RandomArray(int aMAX, int kind, int *bound, int *w)/函数功能:随机生成一

3、个特殊矩阵int InitMatrix(SpecialMatrix *Matrix, int kind, int bound, int w)/函数功能:压缩矩阵初始化void DestoryMatrix(SpecialMatrix *Matrix)/函数功能:销毁压缩矩阵void ArrayToMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:矩阵压缩void ArrayToSMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:对称矩阵压缩void ArrayToTMatrix(SpecialMatrix *Matr

4、ix, int aMAX)/函数功能:三角矩阵压缩void ArrayToBMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:带状矩阵压缩void MatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:特殊矩阵解压缩void SMatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:对称矩阵解压缩void TMatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:三角矩阵解压缩void BMatrixToArray(

5、int aMAX, SpecialMatrix Matrix)/函数功能:带状矩阵解压缩void ShowMatrix(SpecialMatrix Matrix)/函数功能:显示压缩矩阵void ShowArray(int aMAX, int bound)/函数功能:显示初始矩阵3.3调试分析(略)3.4用户手册(略)3.5测试结果(略)4总结(略)5、程序清单:#include <stdio.h>#include <stdlib.h>#include <time.h>#define MAX 18typedef int ElemType;typedef st

6、ruct /压缩矩阵数据结构ElemType *elem;int amount;int bound;int w;int kind;SpecialMatrix;int RandomArray(int aMAX, int, int *, int *);int InitMatrix(SpecialMatrix *, int, int, int);void DestoryMatrix(SpecialMatrix *);void ArrayToMatrix(SpecialMatrix *, int aMAX);void ArrayToSMatrix(SpecialMatrix *, int aMAX);

7、void ArrayToTMatrix(SpecialMatrix *, int aMAX);void ArrayToBMatrix(SpecialMatrix *, int aMAX);void ShowMatrix(SpecialMatrix);void ShowArray(int aMAX, int);void MatrixToArray(int aMAX, SpecialMatrix);void SMatrixToArray(int aMAX, SpecialMatrix);void TMatrixToArray(int aMAX, SpecialMatrix);void BMatri

8、xToArray(int aMAX, SpecialMatrix);int main()int MMAXMAX, bound, w, kind, choase;SpecialMatrix Matrix;while(1)printf("*特殊矩阵压缩与解压缩*n");printf("1.压缩n");printf("2.解压缩n");printf("3.退出n");printf("请选择:");scanf("%d", &choase);switch(choase)case

9、 1:printf("1.对称矩阵n");printf("2.三角矩阵n");printf("3.带状矩阵n");printf("4.退出n");printf("请选择:");scanf("%d", &kind);if(4 = kind)break;RandomArray(M, kind, &bound, &w);printf("随机生成的矩阵为:n");ShowArray(M, bound);InitMatrix(&Mat

10、rix, kind, bound, w);ArrayToMatrix(&Matrix, M);printf("矩阵压缩后:n");ShowMatrix(Matrix);printf("n");break;case 2:if(!Matrix.elem)printf("不存在Matrixn");break;printf("矩阵压缩内容为:n");ShowMatrix(Matrix);MatrixToArray(M, Matrix);printf("n解压后为:n");ShowArray(M,

11、 Matrix.bound);printf("n");break;case 3:exit(1);default: break;DestoryMatrix(&Matrix);return 0;int RandomArray(int aMAX, int kind, int *bound, int *w)/函数功能:随机生成一个特殊矩阵int i, j, r;time_t t;srand(unsigned)time(&t);if(kind < 1 | kind > 3)printf("输入有误!n");return 0;*w = 1

12、;*bound = 0;while(1)*bound = rand()%MAX;if(*bound> 2)break;switch(kind)case 1:for(i=0; i<*bound; i+)for(j=0; j<=i; j+)r = rand()%20;aij = aji = r;break;case 2:*w = rand()%2+1;/生成随机数以控制上三角或下三角switch(*w)case 1:for(i=0; i<*bound; i+)for(j=0; j<*bound; j+)if(i >= j)r = rand()%20+1;aij

13、= r;elseaij = 0;break;case 2:for(i=0; i<*bound; i+)for(j=0; j<*bound; j+)if(i <= j)r = rand()%20+1;aij = r;elseaij = 0;break;break;case 3:*w = rand()%(*bound)+1;while(0 = (*w)%2)*w = rand()%*bound+1;for(i=0; i<*bound; i+)for(j=0; j<*bound; j+)if(j>=(i-(*w/2) && j<=(i+(*w

14、/2)r = rand()%20+1;aij = r;elseaij = 0;break;return 1;int InitMatrix(SpecialMatrix *Matrix, int kind, int bound, int w)/函数功能:压缩矩阵初始化if(kind<1 | kind>3 | bound<1 | bound >100 | w<1 | w>bound)printf("输入有误, 重新输入n");return 0;elseMatrix->kind = kind;Matrix->bound = bound

15、;Matrix->w = w;switch(kind)case 1:Matrix->amount = (bound*(bound+1)/2;break;case 2:Matrix->amount = (bound*(bound+1)/2;break;case 3:Matrix->amount = bound*bound-(bound-1-w/2)*(bound-w/2);printf("%d", Matrix->amount);break;if(!(Matrix->elem = (ElemType *)malloc(Matrix->

16、amount*sizeof(ElemType)return 0;return 1;void DestoryMatrix(SpecialMatrix *Matrix)/函数功能:销毁压缩矩阵free(Matrix->elem);Matrix->bound = 0;Matrix->amount = 0;Matrix->kind = 0;Matrix->w = 0;void ArrayToMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:矩阵压缩switch(Matrix->kind)case 1:ArrayToSMatri

17、x(Matrix, a);break;case 2:ArrayToTMatrix(Matrix, a);break;case 3:ArrayToBMatrix(Matrix, a);break;void ArrayToSMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:对称矩阵压缩int i, j, k;for(i=1; i<=Matrix->bound; i+)for(j=1; j<=i; j+)k = (i-1)*i/2+j-1;Matrix->elemk = ai-1j-1;void ArrayToTMatrix(Speci

18、alMatrix *Matrix, int aMAX)/函数功能:三角矩阵压缩int i, j, k;switch(Matrix->w)case 1:for(i=1; i<=Matrix->bound; i+)for(j=1; j<=i; j+)k = (i-1)*i/2+j-1;Matrix->elemk = ai-1j-1;break;case 2:for(j=1; j<=Matrix->bound; j+)for(i=1; i<=j; i+)k = (j-1)*j/2+i-1;Matrix->elemk = ai-1j-1;break

19、;void ArrayToBMatrix(SpecialMatrix *Matrix, int aMAX)/函数功能:带状矩阵压缩int i, j, k = 0;for(i=1; i<=Matrix->bound; i+)for(j=1; j<=Matrix->bound; j+)if(j>=(i-Matrix->w/2) && j<=(i+Matrix->w/2)Matrix->elemk+ = ai-1j-1;void MatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:

20、特殊矩阵解压缩switch(Matrix.kind)case 1:SMatrixToArray(a, Matrix);break;case 2:TMatrixToArray(a, Matrix);break;case 3:BMatrixToArray(a, Matrix);break;void SMatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:对称矩阵解压缩int i, j, k, flag;for(k=0; k<Matrix.amount; k+)for(i=1; i<=Matrix.bound; i+)flag = 0;fo

21、r(j=1; j<=i; j+)if(k = (i-1)*i/2+j-1)ai-1j-1 = aj-1i-1 = Matrix.elemk;flag = 1;break;if(1 = flag)break;void TMatrixToArray(int aMAX, SpecialMatrix Matrix)/函数功能:三角矩阵解压缩int i, j, k = 0;switch(Matrix.w)case 1:for(i=1; i<=Matrix.bound; i+)for(j=1; j<=Matrix.bound; j+)if(i>=j)ai-1j-1 = Matrix.elemk+;elseai-1j-1 = 0;break;case 2:for(j=1; j<=Matrix.bound; j+)for(i=1; i<=Matrix.bound; i+)if(i<=j)ai-1j-1 = Matrix.elemk+;elseaj-1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论