1、void editHCode(HTNode ht,HCode hcd,CHar &ch,int n,char bianma); /编码函数void printyima(HTNode ht,HCode hcd,int n,char bianma); /译码函数void creatHT(HTNode ht,int n);void CreateHCode (HTNode ht,HCode hcd,int n);void DispHCode(HTNode ht,HCode hcd,int n);void input_key(CHar &ch);void input_file(CHar &void in
2、put_cw(HTNode ht);void bianma1(HTNode ht,HCode hcd,CHar &void bianma2(HTNode ht,HCode hcd,CHar &void yima1(HTNode ht,HCode hcd,int n,char bianma);void yima2(HTNode ht,HCode hcd,int n,char bianma);void creat_cw();void bianmacaidan();void yimacaidan();void bianmayima();int caidan(); /结构体typedef struct
3、 char data; int parent; int weight; int lchild; int rchild;HTNode; char cdN; int start;HCode; char sN; int num;CHar;CHar ch;HTNode htM;HCode hcdN;/主函数int main() int xh; while(1) system(color 1f); /操作菜单背景颜色 xh=caidan(); /调用菜单函数 switch(xh) /switch语句 case 1:system(clscreat_cw();break; case 2:creatHT(ht
4、,n); case 3:CreateHCode(ht,hcd,n);DispHCode(ht,hcd,n); case 4:bianmayima(); case 0:printf(nnnnnnnnntttt感谢使用本系统!nnnnnnn tttexit(0); default:putchar(a printf(ntt输入有误,请重新输入:n return 0;/菜单函数int caidan() /菜单函数模块/ printf(nnntt 欢迎使用哈夫曼编码译码系统 ntt ntt*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*ntt*= =*ntt*=
5、 *=*=*=*=*=*=*=*=*=*=*= =*ntt*= 1.建立字符权值 =*ntt*= 2.建立并输出哈夫曼树 =*ntt*= 3.生成并查看哈夫曼编码 =*ntt*= 4.编码与译码 =*ntt*= 0.退出系统 =*nntt请输入序号进行选择: scanf(%d, &xh); return xh; /返回从键盘接收的选项void bianmayima() nnnnntt 编码与译码 ntt*= *=*=*=*=*=*=*=*=*=*=*=*=*=*=* =*ntt*= 1.编码 =*ntt*= 2.译码 =*ntt*= 0.返回上级菜单 =*n,&bianmacaidan();y
6、imacaidan();return;void yimacaidan()tt 译码 ntt*= 1.键盘输入编码进行译码 =*ntt*= 2.文件读入编码进行译码 =*nyima1(ht,hcd,n,bianma);yima2(ht,hcd,n,bianma);void bianmacaidan()tt 编码 ntt*= 1.键盘输入字符集编码 =*ntt*= 2.文件读入文章编码 =*nbianma1(ht,hcd,ch,n,bianma);bianma2(ht,hcd,ch,n,bianma);void creat_cw() int xh2;tt 建立字符权值 ntt*= 1.从键盘输入字
7、符集进行统计 =*ntt*= 2.从文件读入字符集统计 =*ntt*= 3.自定义字符权值 =*nxh2); switch(xh2) /switch语句input_key(ch);input_file(ch);input_cw(ht);/建立字符权值模块ch) int i,j=0; char stN;请输入字符集(以#结束): for(i=0;iN;i+) scanf(%csti); if(sti=#) sti=0; strcpy(ch.s,st); ch.num=strlen(st); count(ch,ht);按任意键返回! getch(); system( return; int i;
8、FILE*fp; char filename20;请输入要打开的文件名(*.txt):%sfilename); if(fp=fopen(filename,r)=NULL) printf(ntt文件打开失败! return;feof(fp); fread(&ch.si,sizeof(char),1,fp); ch.num=strlen(ch.s);读入成功!文件中的字符集为:%sn,ch.s); fclose(fp);void input_cw(HTNode ht) int i,w,s,j; char a;要输入的字符总个数是?:s); n=s;请输入字符及其权值:s;请输入第%d个字母:,i+
9、1);a); hti.data=a;请输入其权值:w); hti.weight=w; FILE *fp; if(fp=fopen(data.txt,w)=0)n定义权值成功!nn各字符及其权值为: fprintf(fp, 字符t权值 for(j=0;ja&cjzch.si+32=cj) break; if(jAch.siZ) cj=ch.si+32; else cj=ch.si; m+; hti.data=ci; hti.weight=sumi; n=m; FILE *fp;n统计权值成功!/构造哈夫曼树void creatHT(HTNode ht,int n)哈夫曼树.txt int i,j
10、,k,lnode,rnode; int min1,min2; for (i=0;2*n-1;i+) hti.parent=hti.lchild=hti.rchild=-1; for (i=n; min1=min2=32767; lnode=rnode=-1; for(k=0;k=i-1;k+) if(htk.parent=-1) if (htk.weightmin1) min2=min1;rnode=lnode; min1=htk.weight;lnode=k; else if(htk.weightmin2) min2=htk.weight;rnode=k; htlnode.parent=i;
11、htrnode.parent=i; hti.weight=htlnode.weight+htrnode.weight; hti.lchild=lnode;hti.rchild=rnode;建立huffman树成功!输出huffman树:t字符t权值t父节点t 左子节点t右子节点 for(j=1;t %-8c%-8d%-10d%-14d%-10d,htj.data,htj.weight,htj.parent,hti.lchild,htj.rchild);哈夫曼树已输出至文件“哈夫曼树.txt”!/生成哈夫曼编码void CreateHCode (HTNode ht,HCode hcd,int n) int i,f,c,j=0; HCode hc;n; hc.start=n;c=i; hc.cdhc.start-=0 f=hti.parent; while(f!=-1) if (htf.lchild=c) else1 c=f;f=htf.parent; hc.start+;hc.start;j
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1