哈夫曼编码译码器代码及运行图示.doc
文本预览下载声明
#includestdio.h
#includestdlib.h
#includestring.h
#define n 27 /* 字符集的容量 */
#define MAXVALUE 2000 /*权值的最大值*/
#define MAXNODE 35 /*哈夫曼树最多节点数 */
#define MAXD 100 /*叶子节点数*/
typedef struct /* 字符集的元素结构 */
{ char data;
int weight;
}elemtype;
typedef struct /* 哈夫曼树的元素结构 */
{ char data;
int flag;
int weight;
int parent,lchild,rchild;
}htnode,*huffmantree;
typedef char * *HuffmanCode;
/*函数声明*/
void readdata(elemtype *w); /*读取字符信息*/
huffmantree createhuff(elemtype *w); /*建立哈弗曼树*/
char * *huffmancode(huffmantree ht); /*哈弗曼编码*/
void encoding(huffmantree ht,char * *hc); /*测试编码*/
void decoding(huffmantree ht); /*译码*/
void hf_print(elemtype *w); /*打印信息*/
void hf_readdata(elemtype *w); /*读取自带字符频度*/
void readweight(elemtype *w); /*读取用户输入字符频度*/
void Explain(); /*功能提示说明*/
void readdata(elemtype *w) /*读取字符信息*/
{ int i;
char zf;
w[0].data= ; w[1].data=a ;
w[2].data=b ; w[3].data=c ;
w[4].data=d ; w[5].data=e ;
w[6].data=f ; w[7].data=g ;
w[8].data=h ; w[9].data=i ;
w[10].data=j ; w[11].data=k ;
w[12].data=l ; w[13].data=m;
w[14].data=n ; w[15].data=o ;
w[16].data=p ; w[17].data=q ;
w[18].data=r ; w[19].data=s ;
w[20].data=t ; w[21].data=u;
w[22].data=v; w[23].data=w;
w[24].data=x ; w[25].data=y ;
w[26].data=z ;
printf(If you input new char weight yourself y/n:\t 是否自己输入字符频度y/n: \n);
zf=getchar();
while(zf!=nzf!=yzf!=q)
zf=getchar(); /* 提取选择字符 */
switch(zf)
{
case n: hf_readdata(w);break; /* 读取系统字符频度 */
case y: readweight(w);break; /* 用户自己输入字符频度 */
case q: return; /*返回功能选择*/
}
hf_print(w);
return;
}
void hf_readdata(elemtype *w) /*读取自带字符频度*/
{ w[0].weight=186 ; w[1].weight=64 ;
w[2].weight=13 ; w[3].weight=22 ;
w[4].weight=32 ; w[5].weight=103 ;
w[6].weight=21 ; w[7].weight=15 ;
w[8].weight=47 ; w[9].weight=57 ;
w[10].weight=1 ; w[11].weight=5 ;
w[12].weight=32 ; w[13].weight=20 ;
w[14].weight=5
显示全部