程序设计报告.docx
- 文档编号:26996646
- 上传时间:2023-06-25
- 格式:DOCX
- 页数:30
- 大小:1,014.62KB
程序设计报告.docx
《程序设计报告.docx》由会员分享,可在线阅读,更多相关《程序设计报告.docx(30页珍藏版)》请在冰豆网上搜索。
程序设计报告
序号:
学号:
12453111
课程设计
设计课程名称:
C语言课程设计
题目:
DataCompression
学生姓名:
张晶晶
学院:
数理学院专业班级:
应数121
指导教师:
王军专业技术职务:
讲师
设计时间:
2013年6月17日2013年6月28日
目录
1、引言及设计要求3
1.1引言3
1.2设计需求3
1.2.1题目要求3
1.2.2输出要求3
2、概要设计3
2.1数据结构的描述3
2.1.1数据类型的定义3
2.1.2主要算法流程描述4
2.2流程图4
3、详细设计及实现5
4、调试分析5
5、总结及心得9
5.1专题总结9
5.2心得体会9
6、设计日志及参考文献10
6.1设计日志10
6.2参考文献10
附录:
程序源代码11
1、引言及设计要求
1.1引言
在多媒体计算机系统中,信息从单一媒体转到了多种媒体,要表示、传输和处理大量的声音、图像甚至影像视频信息,其数据量之大是非常惊人的。
数字化多媒体信息的数据量是如此巨大,加之信息种类多、实时性要求高,给数据的存储、传输以及加工处理均带来了巨大的压力,不仅要求计算机有更高的数据处理和数据传输能力以及巨大的存储空间,而且也要求通信信道有更高的带宽。
为了解决存储、处理和传输多媒体数据的问题,除了提高计算机本身的性能以及通信信道的带宽外,更重要的则是对多媒体数据进行有效的压缩。
因此数据压缩编解码自然就成为了多媒体技术中最为关键的核心技术。
1.2设计需求
1.2.1题目要求
A:
在每一次迭代过程中,我们需要跟踪的符号(或复合)的最低频率和第二频率最低的发生。
这可以很容易地使用优先队列(一个链表,其中的元素总是插入正确的位置)。
文件encode.c包含的模板代码(pq_insert())的实现优先级队列。
你需要填写缺少的部分。
确保你照顾下列条件:
(i)队列为空(ii)新的元素在开始前存储进去(iii)新元素存储在队列最后或中间。
B:
符号的使用优先队列的pq_pop功能删除。
在一个优先队列中,元素总是从一开始就删除。
文件encode.c包含模板代码来实现这个。
请填写所缺的部分。
确保你的更新要被删除的元素的指针。
C:
一旦代码树是建立在内存中,我们需要生成的每个符号的编码字符串。
填入所缺的代码生成generate_code()。
D:
最后填写缺失的代码来释放所有资源和内存,然后退出代码。
1.2.2输出要求
程序输出两个文件encoded.txt含有编码的输出和code.txt显示霍夫曼编码。
2、概要设计
2.1数据结构的描述
2.1.1数据类型的定义
structtnode
{
structtnode*left;/*usedwhenintree*/
structtnode*right;/*usedwhenintree*/
structtnode*parent;/*usedwhenintree*/
structtnode*next;/*usedwheninlist*/
floatfreq;
intisleaf;
charsymbol;
};
structSymbol
{
charsymbol;
floatfreq;
};
/*globalvariables整体变量*/
charcode[MAX_SYMBOLS][MAX_LEN];
structtnode*root=NULL;/*treeofsymbols*/
structtnode*qhead=NULL;/*listofcurrentsymbols*/
structcnode*chead=NULL;/*listofcode*/
2.1.2主要算法流程描述
Main函数调用pq_insert函数频率初始化,调用pq_pop、talloc、pq_insert函数构建树,调用generate_code函数构建代码,调用dump_code函数输出代码,调用encode函数实现一个样品的字符串编码。
2.2流程图
3、详细设计及实现
分配新的代码:
动态申请一块区域,用强制类型(structtnode*)把它转换成结构体类型structtnode的指针并赋给同类型的指针变量p,再将信息赋给申请的区域里。
在代码功能中显示tnodes的列表:
调用structtnode,输出字符和出现的频率。
插入一个元素到优先列表中:
利用全局变量qhead,分不同的情况将元素插入到优先列表中。
删除第一个元素:
利用全局变量qhead,分两种情况删除元素。
生成的字符串的代码树:
运用递归调用的方法建立一个叶子节点。
输出代码文件:
将code信息输出到文件(就是code.txt)。
输出压缩流:
输出信息。
Main函数:
调用不同的函数完成这个程序。
4、调试分析
在主函数中调用其它函数,获得以下结果:
5、总结及心得
5.1专题总结
在本专题的课程设计中运用了C语言的知识设计了数据压缩,数据压缩包含的内容很多,我做的这个程序调用的函数也比较多。
程序包含7个调用函数和一个主函数,主函数分别调用这几个不同功能的函数。
分别为:
分配新的代码,在代码功能中显示tnodes的列表,插入一个元素到优先列表中,删除第一个元素,生成的字符串的代码树,输出代码文件,输出压缩流。
总结:
获取a,b,c...的频率;根据频率做霍夫曼二叉树;根据二叉树获得a,b,c...的编码;编码到文件中。
5.2心得体会
在这次编程中,我把之前不懂的地方(如:
指针、链表、递归等)都认真的了解了一遍。
在不懂的地方,我不仅看书,还请教了别人,试着能明白这个程序让我们做什么的,在请教别人的情况下,我也把程序里的代码做了一定的修改。
开始时,看了数据压缩的英文版解释,大致了解了这个程序分为三个部分,第一部分:
霍夫曼解码器的实现:
第二部分:
实现霍夫曼编码器;第三部分:
压缩大文件。
第一部分相对其它两个部分简单点,第二部分应该是其中最难的了。
而且,对于第二部分所涉及的C语言知识,我也是没有想到的,因为之前对于链表、递归我还是不了解的,我也通过这次C语言课程设计把落下的知识补上来了。
也许我做的还不是很好,但是,通过这次课程设计我学会了在C语言编程中如何更好地运用所学的知识,和别人合作完成一个大的编程。
6、设计日志及参考文献
6.1设计日志
六月十五号--六月十六号我们拿到题目,进行了分组。
六月十七号--六月十八号将老师所给题目资料、部分程序代码进行整理和汇总,题目英文部分进行翻译,打印成册,组员每人一份。
初步了解设计的要求、目的、所需知识内容,将设计分为三个部分,分配人员负责。
六月十九号--六月二十号进一步了解设计,发现问题,重新部署。
六月二十一号--六月二十三号查阅资料,结合老师已给程序,弄清每部分程序的职能,完成大概的程序填充。
六月二十四号--六月二十五号请教高水平同学,完善粗糙的程序。
六月二十六号进行程序的调试运行,纠错。
六月二十七号撰写程序设计报告,编写设计心得等。
六月二十八号完善设计报告和准备答辩
6.2参考文献
《c语言程序设计(第二版)》
《数据压缩基本原理》
附录:
程序源代码
第一部分:
霍夫曼解码器的实现
#include
#include
#include
#defineMAX_SYMBOLS255
#defineMAX_LEN10
structtnode
{
structtnode*left;/*usedwhenintree*/
structtnode*right;/*usedwhenintree*/
intisleaf;
charsymbol;
};
structcode
{
intsymbol;
charstrcode[MAX_LEN];
};
/*globalvariables*/
structtnode*root=NULL;/*treeofsymbols*/
/*
@functiontalloc
@descallocatesnewnode
*/
structtnode*talloc()
{
structtnode*p=(structtnode*)malloc(sizeof(structtnode));
if(p!
=NULL)
{
p->left=p->right=NULL;
p->symbol=0;
p->isleaf=0;
}
returnp;
}
/*
@functionbuild_tree
@descbuildsthesymboltreegiventhelistofsymbolsandcode.h
NOTE:
alterstheglobalvariablerootthathasalreadybeenallocatedinmain
*/
voidbuild_tree(FILE*fp)
{
charsymbol;
charstrcode[MAX_LEN];
intitems_read;
inti,len;
structtnode*curr=NULL;
while(!
feof(fp))
{
items_read=fscanf(fp,"%c%s\n",&symbol,strcode);
if(items_read!
=2)break;
curr=root;
len=strlen(strcode);
for(i=0;i { /*TODO: createthetreeasyougo*/ if(strcode[i]=='0') { if(curr->left) { curr=curr->left; } else { structtnode*p=(structtnode*)malloc(sizeof(structtnode)); curr->left=p; p->left=0; p->right=0; p->symbol=0; p->isleaf=0; curr=p; } } else { if(curr->right) { curr=curr->right; } else { structtnode*p=(structtnode*)malloc(sizeof(structtnode)); curr->right=p; p->left=0; p->right=0; p->symbol=0; p->isleaf=0; curr=p; } } } /*assigncode*/ curr->isleaf=1; curr->symbol=symbol; printf("insertedsymbol: %c,%s\n",symbol,strcode); } } /* functiondecode */ voiddecode(FILE*fin,FILE*fout) { charc; structtnode*curr=root; while((c=getc(fin))! =EOF) { /*TODO: traversethetree printthesymbolsonlyifyouencounteraleafnode */ if(curr->isleaf==0) { if(c=='1') { curr=curr->right; if(curr->isleaf) { putc(curr->symbol,fout); curr=root; } } else { curr=curr->left; if(curr->isleaf) { putc(curr->symbol,fout); curr=root; } } } } curr=NULL; free(curr); } /* @functionfreetree @desccleansupresourcesfortree */ voidfreetree(structtnode*root) { if(root==NULL) return; freetree(root->right); freetree(root->left); free(root); } intmain() { constchar*IN_FILE="encoded.txt"; constchar*CODE_FILE="code.txt"; constchar*OUT_FILE="decoded.txt"; FILE*fout; FILE*fin; /*allocateroot*/ root=talloc(); fin=fopen(CODE_FILE,"r"); /*buildtree*/ build_tree(fin); fclose(fin); /*decode*/ fin=fopen(IN_FILE,"r"); fout=fopen(OUT_FILE,"w"); decode(fin,fout); fclose(fin); fclose(fout); /*cleanup*/ freetree(root); getchar(); return0; } 第二部分: 实现霍夫曼编码器 #include #include #include #include #include #defineMAX_SYMBOLS128 #defineMAX_LEN20 structtnode { structtnode*left;/*usedwhenintree*/ structtnode*right;/*usedwhenintree*/ structtnode*parent;/*usedwhenintree*/ structtnode*next;/*usedwheninlist*/ floatfreq; intisleaf; charsymbol; }; structSymbol { charsymbol; floatfreq; }; /*globalvariables*/ charcode[MAX_SYMBOLS][MAX_LEN]; structtnode*root=NULL;/*treeofsymbols*/ structtnode*qhead=NULL;/*listofcurrentsymbols*/ structcnode*chead=NULL;/*listofcode*/ /* @functiontalloc @descallocatesnewnode */ structtnode*talloc(intsymbol,floatfreq) { structtnode*p=(structtnode*)malloc(sizeof(structtnode)); if(p! =NULL) { p->left=p->right=p->parent=p->next=NULL; p->symbol=symbol; p->freq=freq; p->isleaf=1; } returnp; } /* @functiondisplay_tnode_list @descdisplaysthelistoftnodesduringcodeconstruction */ voidpq_display(structtnode*head) { structtnode*p=NULL; printf("list: "); for(p=head;p! =NULL;p=p->next) { printf("(%c,%f)",p->symbol,p->freq); } printf("\n"); } /* @functionpq_insert @descinsertsanelementintothepriorityqueue NOTE: makesuseofglobalvariableqhead */ voidpq_insert(structtnode*p) { structtnode*curr=NULL; structtnode*prev=NULL; printf("inserting: %c,%f\n",p->symbol,p->freq); if(qhead==NULL)/*qheadisnull*/ { /*TODO: writecodetoinsertwhenqueueisempty*/ qhead=p; } /*TODO: writecodetofindcorrectpositiontoinsert*/ else { curr=prev=qhead; if(qhead->next==NULL||(p->freq<=qhead->freq)) { curr=qhead; } else { do { prev=curr; curr=prev->next; if((curr==NULL)||((prev->freq<=p->freq)&&(curr->freq>=p->freq))) break; }while (1); } if(curr==qhead) { /*TODO: writecodetoinsertbeforethecurrentstart*/ if(p->freq<=curr->freq) { qhead=p; qhead->next=curr; } else { qhead->next=p; p->next=NULL; } } else/*insertbetweenprevandnext*/ { /*TODO: writecodetoinsertinbetween*/ prev->next=p; p->next=curr; } } } /* @functionpq_pop @descremovesthefirstelement NOTE: makesuseofglobalvariableqhead */ structtnode*pq_pop() { structtnode*p=NULL; /*TODO: writecodetoremovefrontofthequeue*/ if(qhead->next! =NULL) { p=qhead; qhead=qhead->next; printf("popped: %c,%f\n",p->symbol,p->freq); returnp; } else { printf("popped: %c,%f\n",qhead->symbol,qhead->freq); returnqhead; } } /* @functionbuild_code @descgeneratesthestringcodesgiventhetree NOTE: makesuseoftheglobalvariableroot */ chara[MAX_LEN]=""; voidgenerate_code(structtnode*root,char*a,intdepth) { staticintsymbol; staticintlen;/*lengthofcode编码长度*/ if(root==NULL)return; if(root->isleaf) { symbol=root->symbol; len=depth; /*startbackwards开始向后*/ a[len]=0; code[symbol][len]=0; /* TODO: followparentpointertothetop togeneratethecodestring跟随父顶指针生成代码字符串 */ printf("builtcode: %c,%s\n",symbol,a); strcpy(code[symbol],a); } else { a[depth]='0'; generate_code(root->left,a,depth+1); a[depth]='1'; generate_code(root->right,a,depth+1); } } /* @funcdump_code @descoutputcodefile */ voiddump_code(FILE*fp) { inti=0; for(i=0;i { if(code[i][0])/*nonempty*/ { if(i! =32
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 报告