哈弗曼树-VC++6.0版

哈弗曼树-VC++6.0版

ID:37755348

大小:18.89 KB

页数:19页

时间:2019-05-30

哈弗曼树-VC++6.0版_第1页
哈弗曼树-VC++6.0版_第2页
哈弗曼树-VC++6.0版_第3页
哈弗曼树-VC++6.0版_第4页
哈弗曼树-VC++6.0版_第5页
资源描述:

《哈弗曼树-VC++6.0版》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、哈弗曼树源代码:#include#include#include#defineN255#defineM2*N-1/*运行成功标记*/intok=0;/*保存原文件名*/charfile[20]={'0'};/*哈夫曼树节点类型*/typedefstruct{intdata;/*字符值*/intweight;/*权重*/intparent;/*双亲结点*/intlchild;/*左孩子结点*/intrchild;/*右孩子结点*/}Tree;/*哈夫曼编码类型*

2、/typedefstruct{/*存放哈夫曼码*/charcd[N];intstart;}Code;/*生成节点及编码数组*/Treeht[M];Codehcd[N];/*函数声明*/intcmp(constvoid*,constvoid*);intNumberOfChar();voidReset();voidInputFile();voidEncode(int);voidDecode(int);voidOutputFile(int);voidPrintHuffmanCode(int);voidCreateH

3、T(int);voidCreateHCode(int);/*主函数*/intmain(){intx=0;intn=0;charch;while(1){system("cls");printf("1.读入原文件");printf("2.在屏幕上打印哈夫曼代码表");printf("3.编码原文件");printf("4.解码原文件");printf("5.退出");printf("Input1-5:");scanf("%d",&x);switch(x){case1:Reset();Input

4、File();if(ok){/*排序使得有效字符在前面*/qsort(ht,N,sizeof(Tree),cmp);/*记下有效字符的个数*/n=NumberOfChar();CreateHT(n);CreateHCode(n);OutputFile(n);system("pause");}break;case2:system("cls");if(ok){PrintHuffmanCode(n);}else{printf("原文件尚未读入!");}system("pause");break;case3:sy

5、stem("cls");if(ok){Encode(n);}else{printf("原文件尚未读入!");}system("pause");break;case4:system("cls");if(ok){Decode(n);}else{printf("原文件尚未读入!");}system("pause");break;case5:return0;default:/*防止输入错误序号,刷新缓冲区*/fflush(stdin);}}return0;}/*快速排序比较函数*/intcmp(constvo

6、id*a,constvoid*b){return(*(Tree*)a).weight<(*(Tree*)b).weight?1:-1;}/*统计有效的字符数量*/intNumberOfChar(){inti,num=0;for(i=0;i0)num++;}returnnum;}/*初始化哈夫曼树*/voidReset(){inti;for(i=0;i

7、[i].rchild=-1;}}/*读入文件内容*/voidInputFile(){FILE*fp;charch;system("cls");printf("请输入原文件名:");fflush(stdin);scanf("%s",file);/*打开原文件*/if((fp=fopen(file,"rt"))==NULL){printf("找不到原文件%s!",file);ok=0;system("pause");return;}/*读入字符并处理权重*/while(fscanf(fp,"%c",&ch)!

8、=EOF){ht[ch].data=ch;ht[ch].weight++;}/*关闭文件指针*/fclose(fp);printf("原文件%s读入成功!",file);ok=1;}/*编码*/voidEncode(intn){inti,k;charch;FILE*fp1,*fp2;/*利用哈夫曼代码表进行编码*/if(access("Huffman_Code.txt",0)!=0){print

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。