SourceCodeSky QQ客服 SourceCodeSky 400电话 客服电话 暂无

1.cpp ( 文件浏览 )

  • keepall 发布于 2016-08-26 12:16
  • 浏览次数:309
  • 下载次数: 0
  • 下载需 10 积分
  • 侵权举报
//HuffmanCompressor.cpp 
#include <iostream.h> 
#include <fstream.h> 
#include <queue.h> 
#include <bitset.h> 

#include <memory.h> 
#include <windows.h> 
#include <direct.h> 
using namespace std; 
#include "huffman.h" 

int g_nCurDirLayer = 0;  //当前正在处理的文件夹所处的层次 
const int ATTR_FILE = 0; 
const int ATTR_DIR =  1; 
ofstream fout; 

const unsigned long MaxNameLength=256; 

struct tagFileInfo { 
long nAttr; //为 ATTR_FILE 代表文件,为 ATTR_FILE 代表文件夹 
unsigned char nLayer; //文件或文件夹所在的层次,根文件夹所处层次为0 ,如果一个文件夹处于n层,那么该文件夹下的文件就都是 n+1层的 
unsigned long nLength; //改为文件压缩前长度 
//unsigned long nZipLength; //压缩后长度 
char szFileName[MaxNameLength]; //文件名或文件夹名字 
}; 

inline ostream& operator << (ostream &out, const tagFileInfo &tag) 
{ 
out.write(reinterpret_cast<const char *>(&tag.nAttr),sizeof(tag.nAttr)); 
out.write(reinterpret_cast<const char *>(&tag.nLayer),sizeof(tag.nLayer)); 
out.write(reinterpret_cast<const char *>(&tag.nLength),sizeof(tag.nLength)); 
unsigned long t=0; 
do{ 
out.write(reinterpret_cast<const char *>(&tag.szFileName[t]),1); 
}while (tag.szFileName[t++]); 
return out; 
} 

inline istream& operator >> (istream &in, tagFileInfo &tag) 
{ 
in.read(reinterpret_cast<char *>(&tag.nAttr),sizeof(tag.nAttr)); 
in.read(reinterpret_cast<char *>(&tag.nLayer),sizeof(tag.nLayer)); 
in.read(reinterpret_cast<char *>(&tag.nLength),sizeof(tag.nLength)); 
unsigned long t=0; 
do{ 
in.read(reinterpret_cast<char *>(&tag.szFileName[t]),1); 
}while (tag.szFileName[t++]); 
return in; 
} 

//unsigned long FileNumber=0; 

void CompressFile( char * szFile) 
{ 
tagFileInfo FileInfo; 
ifstream fin(szFile,ios::in|ios::binary); 
//FileInfo初始化 
strcpy( FileInfo.szFileName,szFile); 
FileInfo.nLayer = g_nCurDirLayer; 
FileInfo.nAttr = ATTR_FILE; 
FileInfo.nLength = 0; 
//以下程序首先预读取源文件,对字符加权统计,构成哈夫曼树 
memset(&tempcode,0,sizeof(tempcode)); 
memset(asc,0,sizeof(asc)); 
unsigned char ch=0; 
while(!fin.eof()) 
{ 
fin.read(reinterpret_cast<char *>(&ch),1); 
FileInfo.nLength++; 
asc[ch].power++; 
} 
--FileInfo.nLength; 
cout<<"Layer:"<<(unsigned long)FileInfo.nLayer<<" "<<FileInfo.szFileName<<" Original Length:"<<FileInfo.nLength<<"bytes."<<endl; 
fout<<FileInfo;//fout.write(reinterpret_cast<const char *>(&FileInfo),sizeof(FileInfo)); 

int m,n,i; 
priority_queue<int,vector<int>,low > proc; 
for (i=0;i<256;i++) 
if (asc.power) proc.push(i); 
int len=256; 
while (proc.size()>1) 
{ 
m=proc.top(); 
proc.pop(); 
n=proc.top(); 
proc.pop(); 
asc[len].power=asc[m].power+asc[n].power; 
asc[len].left=m; 
asc[len].right=n; 
asc[len].len=0; 
asc[m].code=0;asc[m].len=1; 
asc[n].code=1;asc[n].len=1; 
proc.push(len++); 
} 
proc.pop(); 
visit(len-1,-1,fout); 
//构成哈夫曼树完毕 
//从头开始读取文件,按哈夫曼编码写入 
fin.clear(); 
fin.seekg(0); 
for (i=0;i<FileInfo.nLength;i++)//whil[!==More==!]			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
免费下载源码
Sponsored links

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
/算法分析/ 0 Bytes 2005-11-25 09:18:36
/算法分析/1.cpp 8.40 KB 2005-11-25 09:19:02
/算法分析/1.dsp 3.26 KB 2005-11-25 09:19:30
/算法分析/1.dsw 527 Bytes 2005-11-25 09:21:06
/算法分析/1.ncb 33.00 KB 2005-11-25 09:21:06
/算法分析/1.opt 47.50 KB 2005-11-25 09:21:04
/算法分析/1.plg 1.39 KB 2005-11-25 09:19:30
/算法分析/Debug/ 0 Bytes 2005-11-25 09:19:30
Sponsored links
正在加载中……

Switch to the English version?

Yes
SourceCodeSky 英文版
No
SourceCodeSky 中文版

完善个人资料,获价值¥30元积分奖励!