ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:17.19KB ,
资源ID:7586486      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7586486.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(基于哈夫曼编码实现文本文件的压缩和解压缩.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

基于哈夫曼编码实现文本文件的压缩和解压缩.docx

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