资源描述
香农编码源程序#include#include#include#includeclassTpublic:T()T();voidCreate();voidCoutpxj();voidCoutk();voidCoutz();voidPrint();protected:intn;double*p;double*pxj;int*k;double*mz;voidT:Create()coutn;p=newdoublen;cout请分别输入这n个概率:n;for(inti=0;ipi;pxj=newdoublen;k=newintn;mz=newdoublen;doublesum=0.0;for(i=0;in;i+)sum+=pi;if(sum!=1.0)throw1;elsefor(i=0;in;i+)intk=i;for(intj=i+1;jn;j+)if(pkpj)k=j;doublem=pi;pi=pk;pk=m;T:T()deletep;deletepxj;deletek;deletemz;voidT:Coutpxj()pxj0=0;for(inti=1;in;i+)pxji=0;for(intj=0;ji;j+)pxji+=pj;voidT:Coutk()for(inti=0;i0)ki=(int)d+1;elseki=(int)d;voidT:Print()coutXisetw(8)P(xi)setw(8)Pa(xj)setw(8)Kisetw(8)码字endl;for(inti=0;in;i+)coutXi+1setw(8)setprecision(2)pisetw(8)setprecision(2)pxjisetw(8)ki;mzi=pxji;for(intj=0;j=0)cout1;mzi=2*mzi-1;elsecout0;mzi=2*mzi;coutendl;doubleK=0.0,H=0.0,Y;for(i=0;in;i+)K+=(double)pi*ki;H+=(-1)*pi*(log10(pi)/log10(2.0);Y=H/K;cout平均码长:Kendl;cout信源熵:Hendl;cout编码效率:Yendl;voidmain()Tt;inte;tryt.Create();t.Coutpxj();t.Coutk();t.Print();输入错误,请重新运行;catch(inte)if(e=1)cout费诺编码源程序#include#include#defineN15intpaNN;voidfano(floatp,intaNN,intn,intm,intk)floatg=0.0,h=0.0,d,b,c;inti,j,flase;if(nm)for(i=n;i=m;i+)g=pi+g;g=g/2;for(i=n;ig)d=h-pi;b=h-g;c=g-d;if(cb)for(j=n;j=i;j+)ajk=0;fano(p,a,n,i,k+1);for(j=i+1;j=m;j+)ajk=1;fano(p,a,i+1,m,k+1);elsefor(j=n;j=i-1;j+)ajk=0;fano(p,a,n,i-1,k+1);for(j=i;j=m;j+)ajk=1;fano(p,a,i,m,k+1);break;voidmain()inti,j,kN,n,flase=0;floatpN,m,H=0.0,K=0.0,sum=0.0;cout输入信源符号个数n;cout输入各信源符号概率endl;for(i=1;ipi;for(i=1;i=n;i+)sum=sum+pi;for(i=1;i=n;i+)if(pi1.0|sum!=1.0)flase=1;break;coutinputgailverror!;if(flase=0)for(i=0;i=n;i+)for(j=0;j=n;j+)paij=10;fano(p,pa,1,n,1);cout信源费诺编码如下:n;for(i=1;i=n;i+)ki=0;coutxi=pit码字为t;for(j=1;j=n;j+)if(paij!=10)coutpaij;ki+;coutt码长为tkiendl;for(i=1;i=n;i+)H=-(pi*log(pi)/log(2)+H;coutvv信源熵H(X)=vvHvv(比特/符号)endl;for(i=1;i=n;i+)K=pi*ki+K;coutvv平均码长K=vKvv(比特/符号)endl;cout编码效率为(H/K)*100%endl;/if(flase=0)coutvvendl;/main()QnuTAUOO03亠0)上RuessQd80S84彳XH0二蒸*1S岀0女ST0EX20殳s0殳=?-屋理鵬把腺W妲Mmolp垛m妲竦綽X蘇护垛慝妁連
展开阅读全文