1、基于哈夫曼编码实现文本文件的压缩和解压缩#include #include #ifndef _UNISTD_H#define _UNISTD_H#include #include #endif /* _UNISTD_H */#include #include#include #include #include #include #include using namespace std;typedef struct int weight; int parent,lchild,rchild;HTNode,*HuffmanTree;typedef struct char* data; int* nu
2、m; int length;TNode;typedef struct char *data; char* HM;Code;typedef char* HuffmanCode;bool IfEncode() int choose; system(color 70); coutt 1.加密压缩 2.无密压缩endl; coutchoose; if(choose=1) return true; else return false;void Encode(vector& v) char ch30; v.push_back(); system(color 70); cout请输入压缩密码ch; for(
3、int i=0;chi!=0;i+) v.push_back(chi); v.push_back(0); v.push_back(); cout开始压缩!endl; void ReadTxt(vector& v) char ch; ifstream infile(test.txt,ios:in); if(!infile) cerropen errorendl; exit(1); if(IfEncode() Encode(v); while(infile.peek()!=EOF) infile.get(ch); v.push_back(ch); infile.close(); void Init
4、List(TNode& T) T.data=new char256; T.num=new int256; if(!T.data|!T.num) exit(1); T.length=0;bool Find(TNode T,char ch) int i; for(i=0;iT.length;i+) if(ch=T.datai) return true; return false;void TCount(vector v1,TNode &T) int i,j=0; char ch; int m=v1.size(); for(i=0;im;i+) ch=v1i; if(!Find(T,ch) T.da
5、taj=ch; T.numj=count(v1.begin(),v1.end(),ch); j+; T.length+; void Select(HuffmanTree &HT,int m,int& s1,int& s2) int k,j,n,min=32767; for(k=1;k=m;k+) if(HTk.parent=0) if(HTk.weight=min) j=k; min=HTk.weight; s1=j; HTj.parent=1; min=32767; for(k=1;k=m;k+) if(HTk.parent=0) if(HTk.weight=min) n=k; min=HT
6、k.weight; s2=n;void CreateHuffmanTree (HuffmanTree &HT,TNode T,int length) int m,i,s1,s2; if(length=1) return; m=2*length-1; HT=new HTNodem+1; for(i=1;i=m;+i) HTi.parent=0; HTi.lchild=0; HTi.rchild=0; for(i=1;i=length;+i) HTi.weight=T.numi-1; for(i=length+1;i=m;i+) Select(HT,i-1,s1,s2); HTs1.parent=
7、i; HTs2.parent=i; HTi.lchild=s1; HTi.rchild=s2; HTi.weight=HTs1.weight+HTs2.weight; void CreatHuffmanCode (HuffmanTree HT,HuffmanCode &HC,int n) int i,f,c,start; HC=new char*n+1; char* cd=new charn; cdn-1=0; for(i=1;i=n;i+) start=n-1; c=i; f=HTi.parent; while(f!=0) -start; if(HTf.lchild=c) cdstart=0
8、; else cdstart=1; c=f; f=HTf.parent; HCi=new charn-start; strcpy(HCi,&cdstart); delete cd;int getFileSizeSystemCall(char * strFileName) struct stat temp; stat(strFileName, &temp); return temp.st_size; void Zip(HuffmanCode HC,vector v,TNode T) int i=0,j=0,k=0; ofstream outfile(zip.txt,ios:out); if(!o
9、utfile) cerropen errorendl; exit(1); cout输出哈夫曼编码endl; for(i=0;iv.size();i+) for(j=0;jT.length;j+) if(T.dataj=vi) break; for(k=0;HCj+1k!=0;k+) coutHCj+1k ; for(k=0;HCj+1k!=0;k+) outfileHCj+1k; coutendl; outfile.close(); cout正在压缩 。;Sleep(500);cout 。;Sleep(500);cout 。endl; cout压缩成功!可到zip.txt中查看压缩后文件end
10、l; bool REncode(char ch) int i=0; char code30; cout请输入解压密码:code; if(strcmp(code,ch)=0) return true; else return false;int in_HM(Code &c) c.data=new char256; c.HM=new char* 256; int length=0,i; ifstream infile(code.dat,ios:in); if(!infile) cerropen errorlength; for(i=0;ic.datai; c.HMi=new charlength+
11、1; infilec.HMi; infile.close(); return length; void out_HM(HuffmanCode HC,TNode T) int i; ofstream outfile(code.dat,ios:out); if(!outfile) cerropen errorendl; exit(1); outfileT.lengthendl; for(i=0;iT.length;i+) outfileT.dataiendl; outfileHCi+1endl; outfile.close();void RZip(HuffmanCode HC,TNode T) c
12、har ch; char ch230; int i,j,flag,flag2=0,m=0; ofstream outfile(rzip.txt,ios:out); ifstream infile(zip.txt,ios:in); if(!outfile) cerropen errorendl; exit(1); if(!infile) cerropen errorch; cdi=ch; cdi+1=0; for(j=1;j=T.length;j+) if(strcmp(HCj,cd)=0) if(T.dataj-1=) if(flag2=0) flag=1; flag2=1; delete c
13、d; break; if(flag2=1) while(1) if(REncode(ch2) cout密码正确,正在解压! endl; flag2=0; break; else cout密码错误,请重新输入endl; flag=1; delete cd; break; if(flag2=1) ch2m=T.dataj-1; flag=1; m+; delete cd; break; if(flag2=0) outfileT.dataj-1; flag=1; delete cd; break; if(flag=1) break; cout正在解压 。;Sleep(500);cout 。;Slee
14、p(500);cout 。endl; cout解压成功!请到rzip.txt中查看解压后文件 endl; void about() system(cls); system(color 70); couttendl; coutt 欢迎使用解压缩程序 endl; coutt 关于程序: endl; coutt 压缩: endl; coutt 默认压缩程序所在目录下的test.txt,生成压缩后的zip.txt endl; coutt endl; coutt 解压: endl; coutt 默认解压序所在目录下的zip.txt,生成解压后的rzip.txt endl; coutt endl; cou
15、tt endl; coutt 程序支持压缩中英文文件。 endl; coutt endl; couttendl;void menu() system(color 70); couttendl; coutt endl; coutt 欢迎使用解压缩程序 endl; coutt endl; coutt 1. 压缩文件 endl; coutt 2. 解压文件 endl; coutt 3. 具体信息 endl; coutt 4. 退出程序 endl; coutt endl; couttendl; int main() vector v; int n,choose,size1,size2,size3; T
16、Node T; InitList(T); HuffmanTree HT; HuffmanCode HC; while(1) system(cls); menu(); system(color 70); coutchoose; switch(choose) case 1: ReadTxt(v); TCount(v,T); n=T.length; CreateHuffmanTree (HT,T,n); CreatHuffmanCode (HT,HC,n); Zip(HC,v,T); size1=getFileSizeSystemCall(test.txt); cout原文件中有size1个字节en
17、dl; coutendl; size2=getFileSizeSystemCall(zip.txt); cout压缩后有size2个字节endl; coutendl; out_HM(HC,T); system(pause); break; case 2: RZip(HC,T); size3=getFileSizeSystemCall(rzip.txt); cout解压缩后有size3个字节endl; coutendl; system(pause); break; case 3: about(); system(pause); break; case 4: return 0; default: system(color 70); cout输入错误!请重新输入; system(pause); break; return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1