




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include<stdio.h>#include<stdlib.h>#inelude<math.h>#inelude<string.h>#defineN1000charkind[N]={'\0'};intnum[N]={0};intcount=0;ints1,s2;typedefstruct{intweight;/*字符权重*/intparent,lchild,rchild;}HTNode,*HuffmanTree;/*定义哈夫曼树结点*/typedefchar**HuffmanCode;structcharcode{chardata;charcode[20];}Code[N],d[N],e[N];voidCreat_message()/*输入字符并存入文件*/{FILE*fp;charc;if((fp=fopen(”文本文件.txt","wb"))==NULL){printf("cannotopenthisfile!\n");return;}printf("请输入原代码,以#表示结束\n”);do{scanf("%c",&c);fputc(c,fp);}while(c!='#');fclose(fp);}voidCalculate() /*计算文件中字符的权值 */{charc;inti=1,k;FILE*fp;if((fp=fopen(”文本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}c=fgetc(fp);while(c!='#'){for(k=O;k<i;k++)if(c==kind[k]){num[k]++;break;}if(k>=i&&kind[k-1]!=c){kind[i]=c;num[i]++;i++;}c=fgetc(fp);}count=i-1;}voidselect(HuffmanTreeHu,intj) /*寻找输入的字符数据中权值最小的两个结点 */{intk=1,t;while(Hu[k].parent!=O)k++;for(t=k;t<=j;t++)if(Hu[t].parent==O&&Hu[t].weight<Hu[k].weight)k=t;s仁k;k=1;while(Hu[k].parent!=O||k==s1)k++;for(t=k;t<=j;t++)if(Hu[t].weight<Hu[k].weight&&Hu[t].parent==O&&t!=s1)k=t;s2=k;}voidHuffmanCoding() /*构建哈夫曼树并编码*/{voidselect(HuffmanTreeHu,intj);HuffmanTreeHT;HuffmanCodeHC;intm,i,start,f,c;char*cd;FILE*fp;if(count<=1)return;m=2*count-1;HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));for(i=1;i<=count;i++)HT[i].weight=num[i];for(i=1;i<=m;i++){HT[i].parent=O;HT[i].lchild=0;HT[i].rchild=O;}for(i=count+1;i<=m;i++){select(HT,i-1);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;}HC=(HuffmanCode)malloc((count+1)*sizeof(char*));cd=(char*)malloc(count*sizeof(char));cd[count-1]='\0:for(i=1;i<=count;i++){start=count-1;for(c=i,f=HT[i].parent;f!=O;c=f,f=HT[f].parent)if(HT[f].lchild==c)cd[--start]='0';elsecd[--start]='1';HC[i]=(char*)malloc((count-start)*sizeof(char));strcpy(HC[i],&cd[start]);}if((fp=fopen(”字符代码.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=1;i<=count;i++){Code[i].data=kind[i];strcpy(Code[i].code,HC[i]);fwrite(&Code[i],sizeof(structcharcode),1,fp);}fclose(fp);}voidTransform() /*将字符文件翻译成代码文件 */{FILE*fp1,*fp2,*fp3;inti;charc;if((fp仁fopen(”字符代码.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp2=fopen(”文本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp3=fopen(”码本文件.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=0;i<count;i++)fread(&d[i],sizeof(structcharcode),1,fp1);while(!feof(fp2)){c=fgetc(fp2);for(i=0;i<count;i++)if(d[i].data==c){fputs(d[i].code,fp3);break;}}fputc(#,fp3);fclose(fp3);}voidTranslate() /*将代码文件翻译成字符文件 */{FILE*fp1,*fp2,*fp3;inti,j,k;charc[20]={'\0'};if((fp仁fopen(”码本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp2=fopen(”字符代码.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp3=fopen(”破译文件.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=1;i<=count;i++)fread(&d[i],sizeof(structcharcode),1,fp2);i=0;c[i]=fgetc(fp1);while(c[i]!='#'){for(k=1;k<=count;k++)if(!strcmp(c,d[k].code)){fputc(d[k].data,fp3);for(j=0;j<=i;j++)c[j]='\0';i=-1;break;}c[i]=fgetc(fp1);}fputc('#',fp3);fclose(fpl);fclose(fp2);fclose(fp3);}voidprint1(char*name) /*打印文件函数*/{FILE*fp;charc;if((fp=fopen(name,"rb"))==NULL){printf("cannotopenthisfile\n");return;}c=fgetc(fp);while(c!='#'){printf("%c",c);c=fgetc(fp);}}voidprint2(char*name) /*打印文件函数*/{FILE*fp;inti=1;if((fp=fopen(name,"rb"))==NULL){printf("cannotopenthisfile\n");return;}while(!feof(fp)){fread(&e[i],sizeof(structcharcode),1,fp);printf("%c--%s\n",e[i].data,e[i].code);i++;}}voidmain(){Creat_message();printf("您输入的文本文件是:\n");print1("文本文件.txt");printf("\n");Calculate();HuffmanCoding();
printf(”每个字符相应的哈夫曼编码是:
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 事业单位改制正式合作协议
- 资本投资合作合同
- 无人机制造项目研发合同
- 保安服务合同终止协议书
- 木工单包工劳务合同书
- 环保节能设备制造合同
- 房屋出售居间合同
- 装修工程人工劳务合同
- 工业互联网平台运营合作协议
- 房屋中介服务合同
- 【人教版】《劳动教育》七上 劳动项目一 疏通厨房下水管道 课件
- 2024特斯拉的自动驾驶系统FSD发展历程、技术原理及未来展望分析报告
- 2024-2030年中国银行人工智能行业市场深度调研及发展趋势与投资前景研究报告
- 2024七年级英语下册 Module 1 Lost and found教案(新版)外研版
- 2024年公共卫生基本知识考试题库(附含答案)
- 《垃圾发电厂炉渣处理技术规范》
- 环境空气气态污染物(SO2、NO2、O3、CO)连续自动监测系统安装验收技术规范(HJ 193-2013部分代替 HJ-T 193-2005)
- 《生活垃圾转运站技术规范+CJJT+47-2016》详细解读
- 总体国家安全观-创新引领10周年全文课件
- 鸟类知识科普课件
- 中国通用电气有限公司员工手册
评论
0/150
提交评论