2023年霍夫曼编码实验报告C++.docx





《2023年霍夫曼编码实验报告C++.docx》由会员分享,可在线阅读,更多相关《2023年霍夫曼编码实验报告C++.docx(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、一、实验名称:霍夫曼编码二、实验环境软件环境:Windows 202 3 , M i cros o ft Visual C+6. 0硬件环境:P 4, 2.4GHz,256内存,IBM-PC及兼容机三、实验目的掌握霍夫曼编码、译码原理,并可以通过程序模拟其编码、译码功能。四、实验原理1、消息按其出现的概率由大到小地排成一个概率序列;2、把概率最小两个消息提成一组,其中一个消息编为0,另一个编为1,然后求 其概率和,并把这个新概率与其他尚未解决过的概率重新按概率由大到小排成一 个新的概率序列;3、反复环节,直到所有概率都已联合解决完为止。五、实验过程与实验结果源程序:# inclu d e i
2、n cl u deusing n amespace s t d ;#inc 1 ude typedef s t ruct霍夫曼树的结构体c h ar c h;d o ub 1 e weight; /权值,此处为概率i n t par e nt,Ichi 1 d ,rch i 1 d ;)htn o d e,*hfmt r e e ;* * * * * * *type d ef char *hfmc o de;* * * 全局变量* * * * * * * * * * * *doubl e H= 0 .0;for(int i= 1 ;i=n; i+)。H+ = - 1 *HT i .w e i
3、ght*log(HT i .weig h t) / log( 2 );retu r n H;)/求编码效率do u blc To_Get_Code_Effie i ency ()dou b 1c H 2 ,H,Av e _L;oH=T o _Get_H();Ave_L=Ave_Cod e _L e ng t h();H2=H/ Ave_L; r et u rn II2;)/分析结果输出void Outpu t ()(dou b le H2,H, A v e _ L ;H=To_Get_H();Av e _L= A ve_Code_ L eng t h();H 2=To_Ge t _Code_E
4、 f fic i enc y ();co ui霍夫曼编码结果如下:(消息字符一一码长一一码字)”endl ;for(int i= 1 ; i=n;i+)co u t II Ti.c h c ode_ 1 ength i , 一HCiendl;。c outvv”平均编码长度 A V e_L=Ll* p 1+L2 * p2 + . + Ln* p n=A v e_L(码元/消息)” en d 1;cout信源懒 H=-( p l*lo g (pl) + p 2 * 1 o g(p2)+.+pn * log( p n)/l o g (2)=H (bit/消息)e n dl;c out 码元焙 H2=
5、H / A ve_L=H 2 ( b it/码元)e n d 1 ;-co u 编码效率 E= (H 2 / H2m a x)*100%=H2*100 %e n dl;/* * * * * *编码模块 * * * * * * * * * */字符串合理性检测int M e ssa g e_S t r_Che c k(st r in g temp)(。而fl a g=l;/先假设输入的消息串不含非法字符 in t j;for(i n t i=0;tempi! =、() ;i + +) f o r(j=l;j= n ; j+) 。i f(temp i=HTj.ch)oobreak;0)表达出现非法
6、字符。f lag=0;。b r e a k ;Oft o r eturn flag;/获取待编码消息串s t r ing G et_M e s s ag e _Str()(str i ng temp;int fl a g;A:out”输入待编码的消息串:n ;*cintem p ;f lag=Me s sagc_St r _Chcck( t cmp);if(flag=O)输入的消息串含非法字符(8cout”输入的消息串含非法字符,请重新输入! endl;8 g o to A;return temp;)/对输入的消息串进行编码str i ng Ge t _All_Cod e _S tr ( s
7、t r in g Message_S t r)(% t r in g Al 1 _Co d e_Str=,n;Antj;f o r (in t i=O;Message_Stri!-O; i+)efbr(j=l ; j=n; j +)bif (Messa g e _S t r i=H T j .ch)A 1 1_C o de_St r +=HCj;b re a k;r etur n Al 1 _C o de_ S tr;输出得到的二进制序列void Output_All_Cod e _Str(string A11_C o de_Str)cout”该消息串相应的编码序列如下:“ end 1 ;co
8、utAl 1 _Cod e _S t re n dl;/编码void E n cod i n g ()str i ng Message_S tr,A 1 l_Code_ S tr;Mess age _Str=Get_Mes sag e _S t r ();A 1 1 _Code_Str=Get_All_ C ode_St r (Me s sag e _Str);oOu t p u t_A 1 l_Code_St r (All_Code_S t r);/* * * * *译码模块* * * * * * */ /检测输入的二进制序列是否具有非法字符int Bin a ry_Str_C he c k
9、(stri ng t emp)-int f lag=l,假设不含非法字符fo r (i n t i = 0 ;tempi!=,0,; i+)i f ( ! (temp i = =rO|temp i =-1*)o ofla g =0;。,b reak;)return f 1 ag;)/获取待译的二进制序列string Get_Bi n a r y_Str()(s t r ing t e m p ;in t f 1 ag;B :y o utV t emp;f 1 ag= B inar y _S t r_Che c k( t emp);oif(flag=0) /输入的二进制序列含非法字符(。c ou
10、t 输入的二进制序列含非法字符,请重新输入!”V e ndl;oogot o B; r e turn temp;)/获取源码s tring Get_Ori g inal_Mess a ge_Str ( s tr i ng B inary_ S tr,in t * flag)t r ing tern p =,n,;。* f lag= 1 ;st r ing O r iginaI_Message_Str=;。i n t j ;f or( i n t i= 0 ;B i n a r y_S t r i! =0; i+)。 temp +=B i na r y_St r i;8f o r(j= 1 ;j
11、=n;j+ ) (。i f (HCj=temp)00。Origin a 1_M e s s ag e _St r +=HT j .c h ;lemp= * *;重置 tempbr e a k;00 |0)-if(temp! = )cout”您输入的二进制序列不可译,请重新输入! e n dl;*flag= 0 ;Ir e tu r n Origin a l_Message_Str;1输出得到的源码void Output_Original.Mes s age_Str( s t r ing Orig i n a 1_Me s sag e _Str) ocoutV该二进制序列相应的源码如下:“Ven
12、dl;cou t 0r i gina 1 _Messa g e_Slr e ndl;)译码v oid D e c odin g ()(int f 1 a g;st r i n g B i nary_ S tr,Orig i n al_Messa g e_Str;D : B inary_Str= G ct_B i nary_Str();O riginal_M e s s a ge_ S tr=G e t _O r iginalMes s age_Str(B i nary_Str, & f la g );oi f (! f 1 ag) goto D;Outpu t _ 0 r i ginal_Me
13、ss age _Str(O r ig i n a 1_M e ssag e _St r );)/* * * * * * * * * * * 主函数* * * * * * * * * /int main() char choice=/*用于记录初始化情况*/in t flag=0;0coutnowhi 1 e ( c h oice! =,Q&c h oic e !-q) / / 当 c h o i ce 的值不为 q 且不为 Q 时循环C:you t ,* * * * * * * * * * *霍夫曼编码,译码器* * * * * * * * * * *nc ou t Vv I .输入建立“v”
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2023 年霍夫曼 编码 实验 报告 C+

限制150内