《2022年2022年哈夫曼编码译码C语言编写 .pdf》由会员分享,可在线阅读,更多相关《2022年2022年哈夫曼编码译码C语言编写 .pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、哈夫曼编码译码C语言编写10 标签: 哈夫曼 , 编码, 译码 如上题 C 语言编个程序懂的帮搞个能运行的程序来#11 别到网上找那些我找过了没用的蝶风待夕魂回答:2 人气:19 解决时间 :2009-05-14 18:44 满意答案好评率: 100%#include #include #include typedef char ElemType; typedef struct ElemType elem; unsigned int weight; unsigned int parent,lchild,rchild; HTNode,*HuffmanTree; typedef char* Huf
2、fmanCode; typedef int Status; typedef struct char elem; unsigned int weight; Weight; / save the information of the symbolizes; void HuffmanCoding(HuffmanTree *,HuffmanCode *,Weight *,int); void Select(HuffmanTree,int,int *,int *); void OutputHuffmanCode(HuffmanTree,HuffmanCode,int); Status main(void
3、) HuffmanTree HT; HuffmanCode HC; Weight *w; char c; int i,n; int wei; printf(input the tatol number of the Huffman Tree: ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - scanf(%d,&n); w=(Weight *)malloc(n*sizeof(Weight); for(i=0;in;i+) p
4、rintf(input the element & its weight:); scanf(%1s%d,&c,&wei); wi.elem=c; wi.weight=wei; HuffmanCoding(&HT,&HC,w,n); OutputHuffmanCode(HT,HC,n); return 1; 回答人的补充 2009-05-08 00:07 接上面的。字数限制一次发不完。void HuffmanCoding(HuffmanTree *HT,HuffmanCode *HC,Weight *w,int n) int i,m,s1,s2,start,f; unsigned int c;
5、char *cd; / HuffmanTree p; if(n=1) return; m=2*n-1; (*HT)=(HuffmanTree)malloc(m+1)*sizeof(HTNode); for(i=1;i=n;+i) (*HT)i.elem=wi-1.elem; (*HT)i.weight=wi-1.weight; (*HT)i.parent=(*HT)i.lchild=(*HT)i.rchild=0; for(;i=m;+i) (*HT)i.elem=0; (*HT)i.weight=(*HT)i.parent=(*HT)i.lchild=(*HT)i.rchild=0; for
6、(i=n+1;i=m;+i) Select(*HT,i-1,&s1,&s2); (*HT)s1.parent=i;(*HT)s2.parent=i; (*HT)i.lchild=s1;(*HT)i.rchild=s2; (*HT)i.weight=(*HT)s1.weight+(*HT)s2.weight; 回答人的补充 2009-05-08 00:12 (*HC)=(HuffmanCode)malloc(n*sizeof(char*); cd=(char *)malloc(n*sizeof(char); cdn-1=0; for(i=1;i=n;+i) start=n-1; 名师资料总结 -
7、 - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - for(c=i,f=(*HT)i.parent;f!=0;c=f,f=(*HT)f.parent) if(*HT)f.lchild=c) cd-start=0; else cd-start=1; (*HC)i=(char *)malloc(n-start)*sizeof(char); strcpy(*HC)i,&cdstart); 回答人的补充 2009-05-08 00:18 void Sele
8、ct(HuffmanTree HT,int n,int *s1,int *s2) int i; (*s1)=(*s2)=0; for(i=1;i=n;i+) if(HTi.weightHT(*s2).weight&HTi.parent=0&(*s2)!=0) if(HTi.weightHT(*s1).weight) (*s2)=(*s1); (*s1)=i; else (*s2)=i; if(*s1)=0|(*s2)=0)&HTi.parent=0) if(*s1)=0) (*s1)=i; else if(*s2)=0) if(HTi.weight(*s2) i=(*s1); (*s1)=(*
9、s2); (*s2)=i; return; void OutputHuffmanCode(HuffmanTree HT,HuffmanCode HC,int n) int i; printf(nnumber-element-weight-huffman coden); for(i=1;i0) p=s1; s20=s10; s21=0; r=s2; e=0; while(q=strstr(p,r)!=NULL) *q=0; strcat(temp,p); strcat(temp,s3); p=q+strlen(s2); e+; m-=e; strcat(temp,p); strcpy(s1,te
10、mp); s5n=s20; n+; strcpy(temp,); s5n=0; strcpy(s1,s5); printf( 压缩后的电文(即叶结点): %sn,s1); return s1; HNODETYPE huffmantree(char *s2,char s3) HNODETYPE huffnodeMAXNODE; HCODETYPE huffcodeMAXLEAF,cd; int sum,i,j,n1,n2,x1,x2,p,k,c; char s126=a,b,c,d,e,f,g,h,i,j,k,l,m, n,o,p,q,r,s,t,u,v,w,x,y,z; char s5MAXL
11、EAF; int ww26=0,n=0; strcpy( s5,s2); sum=strlen(s2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - for(i=0;i26;i+) /*统计所有字符出现的频度 */ for(j=0;jsum;j+) if(s2j=s1i) wwi+; n=strlen(s3); for (i=0;i2*n-1;i+) huffnodei.weight=0; huffnodei.parent=
12、-1; huffnodei.lchild=-1; huffnodei.rchild=-1; for(i=0;in;i+) /*分配给各叶结点权值 */ for(j=0;j26;j+) if (s3i=s1j) huffnodei.weight=wwj; for (i=0;in-1;i+) /*构造哈夫曼树 */ n1=n2=MAXVALUE; x1=x2=0; for(j=0;jn+i;j+) if (huffnodej.parent=-1 & huffnodej.weightn1) n2=n1; x2=x1; n1=huffnodej.weight; x1=j; else if (huffn
13、odej.parent=-1 & huffnodej.weightn2) n2=huffnodej.weight; x2=j; huffnodex1.parent=n+i; huffnodex2.parent=n+i; huffnoden+i.weight=huffnodex1.weight+huffnodex2.weight; huffnoden+i.lchild=x1; huffnoden+i.rchild=x2; for(i=0;in;i+) /*求每个叶结点的哈夫曼编码*/ cd.start=n-1; c=i; p=huffnodec.parent; while (p!=-1) if
14、(huffnodep.lchild=c) cd.bitcd.start=0; else cd.bitcd.start=1; cd.start-; c=p; p=huffnodec.parent; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - for (j=cd.start+1;jn;j+) huffcodei.bitj=cd.bitj; huffcodei.start=cd.start; printf( 各叶结点对应哈夫曼编码
15、 : );/*输出每个叶结点的哈夫曼编码*/ for(i=0;in;i+) for(j=huffcodei.start+1;jn;j+) printf(%d,huffcodei.bitj); printf( ); printf(n 电文的全部哈夫曼编码 : );/*输出电文的全部哈夫曼编码*/ for(k=0;ksum;k+) for(i=0;in;i+) if(s2k=s3i) for(j=huffcodei.start+1;jn;j+) printf(%d,huffcodei.bitj); printf( ); printf(n); main() char s1MAXLEAF,s2MAXLEAF; printf(n 请输入电文 : ); gets(s1); strcpy(s2,getcode1(s1, ,); huffmantree(s1,getcode(s2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -