哈夫曼编码译码课程设计报告文档格式.docx
- 文档编号:16276596
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:25
- 大小:544.60KB
哈夫曼编码译码课程设计报告文档格式.docx
《哈夫曼编码译码课程设计报告文档格式.docx》由会员分享,可在线阅读,更多相关《哈夫曼编码译码课程设计报告文档格式.docx(25页珍藏版)》请在冰豆网上搜索。
search(k,j,p)搜索二叉树
6:
Print_tree()打印二叉树
7:
menu()主菜单
9:
main()主函数
4、实验结果与分析
(1)大致个人测试案例:
主界面:
初始化:
Huofuman.txt初始化存入文件结果如下:
编码结果如下:
codefile.txt编码存入文件如下:
译码结果如下:
textfile.txt译码存入文件如下:
打印结果如下:
打印树结果如下:
treeprint.txt存入文件结果如下:
(2)本例测试案例_1
已知某系统在通信联络中只可能出现八种字符,其频率分别为0.05,0.29,0.07,0.08,0.14,0.23,0.03,0.11,试设计哈夫曼编码。
解:
假设八种字符为ABCDEFGH,将各个概率乘以100可得权值为529781423311
启动程序,测试得:
编码:
译码打印结束:
(3)本例测试案例_2
用下表给出的字符集和频度的实际统计数据建立哈夫曼树,并实现以下报文的编码和译码:
“THISPROGRAMEISMYFAVORITE”。
字符
A
B
C
D
E
F
G
H
I
J
K
L
M
频度
188
64
13
22
32
103
21
15
47
57
1
5
20
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
63
48
51
80
23
8
18
16
先假设空格为#,所以输入字符时,将空格变为#。
输入如下:
先将从空格到Z输入权值
然后对字符进行编码:
空格到Z译码
然后输入字符串并编码:
保存到Codefile.txt中
然后将开始译码并结束
将译码结果保存到Textfile.txt中
5、实验总结
从这个课程设计中,我发现了很多问题,包括文件存储,字符串输入等等,最主要的是经历了一次找BUG的过程,当把自己写的代码里的错误一个一个找出来时,是非常非常兴奋的。
还是希望能有多这个经历。
不喜欢copy,如果copy了,就缺少了一次挑战自己的经历。
六、主要代码
//Huffman_2.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"
stdafx.h"
#include<
stdio.h>
stdlib.h>
string.h>
math.h>
#definemaxsize1000
#definelen20
#definerowsize20
//--------初始化-------------//
struct
{
intparent;
intlchild,rchild;
intweight;
}HFM_tree[maxsize];
charhfstr;
}HFM_num[maxsize];
intstart;
intbit[len];
}HFM_hf;
struct
charhfch;
intlength;
}HFM_code[maxsize];
intleaves;
voidInitialization()
inti,j;
FILE*HFM_f;
HFM_f=fopen("
HuofumanTree.txt"
"
w"
);
if(HFM_f==NULL)
{
printf("
createfileerror!
\n"
}
printf("
请输入字符集大小:
"
scanf("
%d"
&
leaves);
fprintf(HFM_f,"
----输入的值-----\n"
字符大小%4d\n"
leaves);
字符权值\n"
for(i=0;
i<
leaves;
i++)
请输入第%d个字符和其权值:
"
i+1);
scanf("
%c"
HFM_num[i].hfstr);
HFM_num[i].weight);
fprintf(HFM_f,"
%4c"
HFM_num[i].hfstr);
%4d\n"
HFM_num[i].weight);
//存储字符和权值
//-----建立哈夫曼树-----//
maxsize;
i++)//哈夫曼树初始化
HFM_tree[i].parent=-1;
HFM_tree[i].lchild=-1;
HFM_tree[i].rchild=-1;
HFM_tree[i].weight=0;
HFM_tree[i].weight=HFM_num[i].weight;
leaves-1;
intm1,m2;
intm1_pos,m2_pos;
m1=m2=65536;
m1_pos=m2_pos=0;
for(j=0;
j<
leaves+i;
j++)
{
if(HFM_tree[j].weight<
m1&
&
HFM_tree[j].parent==-1)
{
m2=m1;
m1=HFM_tree[j].weight;
m2_pos=m1_pos;
m1_pos=j;
}
else
if(HFM_tree[j].weight<
m2&
{
m2=HFM_tree[j].weight;
m2_pos=j;
}
}
HFM_tree[leaves+i].parent=-1;
HFM_tree[leaves+i].lchild=m1_pos;
HFM_tree[leaves+i].rchild=m2_pos;
HFM_tree[m1_pos].parent=leaves+i;
HFM_tree[m2_pos].parent=leaves+i;
HFM_tree[leaves+i].weight=m2+m1;
//----输出哈夫曼树----//
----------------哈夫曼编码--------------\n"
parentlchildrchildweight\n"
-------------哈夫曼编码------------\n"
leaves*2-1;
%8d%8d%8d%8d\n"
HFM_tree[i].parent,HFM_tree[i].lchild,HFM_tree[i].rchild,HFM_tree[i].weight);
%8d%8d%8d%8d\n"
fclose(HFM_f);
}
//--------编码--------------//
voidEncoding()
inti,j,p,c,k;
FILE*HFM_f=fopen("
CodeFile.txt"
openfileerror!
c=i;
p=HFM_tree[i].parent;
HFM_hf.start=len-1;
while(p!
=-1)
if(HFM_tree[p].lchild==c)
HFM_hf.bit[HFM_hf.start]=0;
HFM_hf.bit[HFM_hf.start]=1;
--HFM_hf.start;
c=p;
p=HFM_tree[c].parent;
for(j=HFM_hf.start+1,k=0;
len;
j++,k++)
HFM_code[i].bit[k]=HFM_hf.bit[j];
HFM_code[i].length=len-HFM_hf.start-1;
HFM_code[i].start=HFM_hf.start+1;
HFM_code[i].hfch=HFM_num[i].hfstr;
character:
%cstart:
%dlength:
%dCode:
HFM_code[i].hfch,HFM_code[i].start,HFM_code[i].length);
HFM_code[i].length;
printf("
HFM_code[i].bit[j]);
fprintf(HFM_f,"
//--------译码--------------//
voidDecoding()
chara[maxsize];
charHFM_dc[maxsize];
inti,j,k,p;
Textfile.txt"
FILE*hf=fopen("
r"
if(HFM_f==NULL)
fscanf(hf,"
%s"
a);
j=0;
p=0;
k=2*leaves-2;
while(HFM_tree[k].lchild!
=-1&
HFM_tree[k].rchild!
if(a[j]=='
0'
)
k=HFM_tree[k].lchild;
1'
k=HFM_tree[k].rchild;
j++;
HFM_dc[p]=HFM_code[k].hfch;
p++;
p;
if(HFM_dc[i]=='
#'
)
HFM_dc[i]='
'
;
%c"
HFM_dc[i]);
fclose(hf);
//--------打印代码文件------//
voidPrint_file()
FILE*hc=fopen("
CodePrint.txt"
charstr[maxsize];
ints,i;
str);
s=strlen(str);
s;
str[i]);
fprintf(hc,"
if((i+1)%50==0)
fprintf(hc,"
fclose(hc);
//-------打印哈夫曼树------//
intfp[rowsize][rowsize];
intf=5;
voidsearch(intk,intj,intp)
intc;
intd;
intb;
if(HFM_tree[k].lchild!
c=HFM_tree[k].lchild;
d=j+1;
b=p-2;
if(fp[d][b])
b=b+1;
fp[d][b]=HFM_tree[c].weight;
search(c,d,b);
if(HFM_tree[k].rchild!
c=HFM_tree[k].rchild;
b=p+2;
b=b-1;
voidPrint_tree()
inti,j,k,s,p,c;
TreePrint.txt"
fileopenerror!
memset(fp,0,sizeof(fp));
fp[0][10]=HFM_tree[leaves*2-2].weight;
search(leaves*2-2,0,10);
p=(int)log(leaves*2-1)/log
(2)+1;
p+2;
rowsize;
if(fp[i][j]==0)
printf("
fprintf(HFM_f,"
%3d"
fp[i][j]);
\n\n"
//-----输入字符串------//
voidinputcode()
pleaseinputyourcharacterstring\n"
charhfman_str[maxsize];
inti,j,k,s;
FILE*fp=fopen("
if(fp==NULL)
getchar();
gets(hfman_str);
k=strlen(hfman_str);
k;
if(hfman_str[i]=='
for(j=0;
HFM_code[0].length;
HFM_code[0].bit[j]);
fprintf(fp,"
else
s=hfman_str[i]-'
A'
+1;
HFM_code[s].length;
HFM_code[s].bit[j]);
fclose(fp);
//--------将字符译码---------//
voidreoutstr()
Decoding();
//--------主界面-------------//
voidmenu()
*********班级:
10电信实验班学号:
Q10600132姓名:
王彬彬*********\n\n"
*****哈夫曼编码和译码****\n\n"
*************************\n"
*1:
初始化-------------I*\n"
*2:
编码---------------E*\n"
*3:
译码---------------D*\n"
*4:
打印代码文件-------P*\n"
*5:
打印哈夫曼树-------T*\n"
*6:
输入字符串并编码---C*\n"
*8:
输入译码-----------Y*\n"
*************************\n\n"
//--------主函数------------//
intmain()
charchoice;
menu();
while(choice!
='
Q'
pleaseinputyourchoicehere:
%c"
choice);
switch(choice)
case'
I'
:
i'
Initialization();
//初始化
break;
E'
e'
Encoding();
//编码
C'
c'
inputcode();
//输入字符串
Y'
y'
reoutstr();
D'
d'
Decoding();
//译码
P'
p'
Print_file();
//打印代码文件
T'
t'
Print_tree();
//打印哈夫曼树
q'
\n**************"
\n*程序终止!
*"
//停止
\n**************\n\n"
default:
inputerror\n"
system("
pause"
return0;
成绩评定表
平时成绩
答辩成绩
最终成绩
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 哈夫曼 编码 译码 课程设计 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)