欢迎来到天天文库
浏览记录
ID:10823436
大小:84.00 KB
页数:10页
时间:2018-07-08
《运用哈夫曼编码压缩解压文件源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、运用哈夫曼编码压缩解压文件源代码2010-06-2211:25#include#include#include#include#includeusingnamespacestd;structhead{unsignedcharb; //记录字符longcount; //权重intparent,lch,rch; //定义双亲,左孩子,右孩子charbits[256]; //存放哈夫曼编码的数组}header[51
2、2],tmp; //头部一要定设置至少512个,因为结点最多可达256,所有结点数最多可达511unsignedcharctoa(chara[]) /*将数组的前八位转成二进制形式比特位*/{unsignedcharc=0;for(inti=0;i<8;i++) if(a[i]!=0) { c=c+(int)(a[i]-'0')*pow(2,8-1-i); }returnc;}char*code(unsignedchartemp,intleafnum) //寻找对应字符的编码串,并返回{for(inti
3、=0;i4、tpointnum; //定义总结点unsignedchartemp; //定义unsignedchar类型,暂存文件中字符的中间变量/*********************************文件中字符频度************************************/for(inti=0;i<256;i++){ header[i].count=0; //初始化权重 header[i].b=(unsignedchar)i; //初始化字符}ifstre5、aminfile(infilename,ios::in6、ios::binary);while(infile.peek()!=EOF){ infile.read((char*)&temp,sizeof(unsignedchar)); //读入一个字符 header[temp].count++; //统计对应结点字符权重 flength++; //统计文件长度}infile.close(); //关闭文件for(i=0;i<256-1;i++) //对结点进行冒泡排7、序,权重大的放在上面,编码时效率高 for(intj=0;j<256-1-i;j++) if(header[j].count8、中总结点数目/**********************************根据频度建树*************************************/longmin; //尽量用long,如果文件过大,这里建树可能不是最优树了ints1,s2;for(i=leafnum;i9、header[j].count
4、tpointnum; //定义总结点unsignedchartemp; //定义unsignedchar类型,暂存文件中字符的中间变量/*********************************文件中字符频度************************************/for(inti=0;i<256;i++){ header[i].count=0; //初始化权重 header[i].b=(unsignedchar)i; //初始化字符}ifstre
5、aminfile(infilename,ios::in
6、ios::binary);while(infile.peek()!=EOF){ infile.read((char*)&temp,sizeof(unsignedchar)); //读入一个字符 header[temp].count++; //统计对应结点字符权重 flength++; //统计文件长度}infile.close(); //关闭文件for(i=0;i<256-1;i++) //对结点进行冒泡排
7、序,权重大的放在上面,编码时效率高 for(intj=0;j<256-1-i;j++) if(header[j].count8、中总结点数目/**********************************根据频度建树*************************************/longmin; //尽量用long,如果文件过大,这里建树可能不是最优树了ints1,s2;for(i=leafnum;i9、header[j].count
8、中总结点数目/**********************************根据频度建树*************************************/longmin; //尽量用long,如果文件过大,这里建树可能不是最优树了ints1,s2;for(i=leafnum;i9、header[j].count
9、header[j].count
此文档下载收益归作者所有