没有合适的资源?快使用搜索试试~ 我知道了~
这是压缩文件制作的源代码,有兴趣的可以下载
资源推荐
资源详情
资源评论
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
struct head
{
unsigned char b; //记录字符
long count; //权重
int parent,lch,rch; //定义双亲,左孩子,右孩子
char bits[256]; //存放哈夫曼编码的数组
}
header[512],tmp; //头部一要定设置至少512个,因为结点最多可达256,所有结点数最多可达511
unsigned char ctoa(char a[]) /*将数组的前八位转成二进制形式比特位*/
{
unsigned char c=0;
for(int i=0;i<8;i++)
if(a[i]!=0)
{
c=c+(int)(a[i]-'0')*pow(2,8-1-i);
}
return c;
}
char *code(unsigned char temp,int leafnum) //寻找对应字符的编码串,并返回
{
for(int i=0;i<leafnum;i++)
if(temp==header[i].b)
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
struct head
{
unsigned char b; //记录字符
long count; //权重
int parent,lch,rch; //定义双亲,左孩子,右孩子
char bits[256]; //存放哈夫曼编码的数组
}
header[512],tmp; //头部一要定设置至少512个,因为结点最多可达256,所有结点数最多可达511
unsigned char ctoa(char a[]) /*将数组的前八位转成二进制形式比特位*/
{
unsigned char c=0;
for(int i=0;i<8;i++)
if(a[i]!=0)
{
c=c+(int)(a[i]-'0')*pow(2,8-1-i);
}
return c;
}
char *code(unsigned char temp,int leafnum) //寻找对应字符的编码串,并返回
{
for(int i=0;i<leafnum;i++)
if(temp==header[i].b)
return header[i].bits;
return NULL;
}
void compress(char *infilename,char *outfilename)
{
long flength=0; //记录压缩前文件长度
long clength=8; //编码从偏移量8记录,统计压缩后编码长度加8
int leafnum; //定义叶子结点
int pointnum; //定义总结点
unsigned char temp; //定义unsigned char类型,暂存文件中字符的中间变量
/*********************************文件中字符频度************************************/
for(int i=0;i<256;i++)
{
header[i].count=0; //初始化权重
header[i].b=(unsigned char)i; //初始化字符
}
ifstream infile(infilename,ios::in|ios::binary);
while(infile.peek()!=EOF)
{
infile.read((char *)&temp,sizeof(unsigned char)); //读入一个字符
header[temp].count++; //统计对应结点字符权重
flength++; //统计文件长度
}
infile.close(); //关闭文件
for(i=0;i<256-1;i++) //对结点进行冒泡排序,权重大的放在上面,编码时效率高
for(int j=0;j<256-1-i;j++)
if(header[j].count<header[j+1].count)
{
tmp=header[j];
return NULL;
}
void compress(char *infilename,char *outfilename)
{
long flength=0; //记录压缩前文件长度
long clength=8; //编码从偏移量8记录,统计压缩后编码长度加8
int leafnum; //定义叶子结点
int pointnum; //定义总结点
unsigned char temp; //定义unsigned char类型,暂存文件中字符的中间变量
/*********************************文件中字符频度************************************/
for(int i=0;i<256;i++)
{
header[i].count=0; //初始化权重
header[i].b=(unsigned char)i; //初始化字符
}
ifstream infile(infilename,ios::in|ios::binary);
while(infile.peek()!=EOF)
{
infile.read((char *)&temp,sizeof(unsigned char)); //读入一个字符
header[temp].count++; //统计对应结点字符权重
flength++; //统计文件长度
}
infile.close(); //关闭文件
for(i=0;i<256-1;i++) //对结点进行冒泡排序,权重大的放在上面,编码时效率高
for(int j=0;j<256-1-i;j++)
if(header[j].count<header[j+1].count)
{
tmp=header[j];
剩余12页未读,继续阅读
资源评论
landeov123
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功