为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

哈夫曼压缩解压

2017-11-21 14页 doc 31KB 69阅读

用户头像

is_353097

暂无简介

举报
哈夫曼压缩解压哈夫曼压缩解压 #include #include #include #include struct head { unsigned char b; long count; long parent,lch,rch; char bits[256]; } header[512],tmp; void compress() { char filename[255],outputfile[255],buf[512]; unsigned char c; long i,j,m,n,f; long min1,p...
哈夫曼压缩解压
哈夫曼压缩解压 #include #include #include #include struct head { unsigned char b; long count; long parent,lch,rch; char bits[256]; } header[512],tmp; void compress() { char filename[255],outputfile[255],buf[512]; unsigned char c; long i,j,m,n,f; long min1,pt1,flength,length1,length2; double div; FILE *ifp,*ofp; printf("\t请您输入需要压缩的文件:"); gets(filename); ifp=fopen(filename,"rb"); if(ifp==NULL) { printf("\n\t文件打开失败!\n\n"); return; } printf("\t请您输入压缩后的文件名:"); gets(outputfile); ofp=fopen(strcat(outputfile,".hub"),"wb"); if(ofp==NULL) { printf("\n\t压缩文件失败!\n\n"); return; } flength=0; while(!feof(ifp)) { fread(&c,1,1,ifp); header[c].count++; flength++; } flength--; length1=flength; header[c].count--; for(i=0;i<512;i++) { if(header[i].count!=0) header[i].b=(unsigned char)i; else header[i].b=0; header[i].parent=-1;header[i].lch=header[i].rch=-1; } for(i=0;i<256;i++) { for(j=i+1;j<256;j++) { if(header[i].countheader[j].count) { pt1=j; min1=header[j].count; continue; } } header[i].count=header[pt1].count; header[pt1].parent=i; header[i].lch=pt1; min1=999999999; for(j=0;jheader[j].count) { pt1=j; min1=header[j].count; continue; } } header[i].count+=header[pt1].count; header[i].rch=pt1; header[pt1].parent=i; } for(i=0;i=8) { for(i=0;i<8;i++) { if(buf[i]=='1') c=(c<<1)|1; else c=c<<1; } fwrite(&c,1,1,ofp); pt1++; strcpy(buf,buf+8); j=strlen(buf); } if(f==flength) break; } if(j>0) { strcat(buf,"00000000"); for(i=0;i<8;i++) { if(buf[i]=='1') c=(c<<1)|1; else c=c<<1; } fwrite(&c,1,1,ofp); pt1++; } fseek(ofp,4,SEEK_SET); fwrite(&pt1,sizeof(long),1,ofp); fseek(ofp,pt1,SEEK_SET); fwrite(&n,sizeof(long),1,ofp); for(i=0;i0) m=p/8+1; else m=p/8; for(j=0;jf;l--) { strcat(header[i].bits,"0"); } strcat(header[i].bits,buf); } header[i].bits[p]=0; } for(i=0;istrlen(header[j].bits)) { tmp=header[i]; header[i]=header[j]; header[j]=tmp; } } } p=strlen(header[n-1].bits); fseek(ifp,8,SEEK_SET); m=0; bx[0]=0; while(1) { while(strlen(bx)<(unsigned int)p) { fread(&c,1,1,ifp); f=c; itoa(f,buf,2); f=strlen(buf); for(l=8;l>f;l--) { strcat(bx,"0"); } strcat(bx,buf); } for(i=0;i
/
本文档为【哈夫曼压缩解压】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索