云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 哈夫曼编码译码器实验报告(免费)

哈夫曼编码译码器实验报告(免费)

  • 62 次阅读
  • 3 次下载
  • 2025/6/17 11:42:28

FILE * f1=fopen(\FILE * f2=fopen(\FILE * f3=fopen(\

int main(){

init(SN); //初始化字符数据库// input(f1); //读入初始文件的字符// for(int i=0;forest[i]!=NULL;i++)

cout<c<<\ //输出字符及出现次数// cout<<\出现字符种类 \ //输出字符种类//

HFM huffman(count); //创建哈夫曼树实例// huffman.creat();

//创建哈夫曼树// count=0;

huffman.hufcode(); //哈夫曼编码,此时为逆向//

exchange(); //调整首尾对调哈夫曼编码//

huffman.savewithhufcode(f1,f2); //用哈夫曼编码存储原文件// cout<

cout<<\查看哈夫曼编码\ cout<<\哈夫曼解码\ cout<<\查看压缩率\

int choice; cin>>choice;

while(choice>=1&&choice<=3){ switch(choice){ case 1:{

for(i=0;hufNode[i].sig!=NULL;i++){

cout<<\字符\的哈夫曼编码:\ //输出哈夫曼编码// for(int j=0;j

}

cout<<\最大列数:\ } case 2:{

fclose(f2);

f2=fopen(\

huffman.hufdecode(f2,f3); //哈夫曼解码//

cout<

break;

compress(); //查看压缩情况// cout<

cout<<\查看哈夫曼编码\ cout<<\哈夫曼解码\

cout<<\查看压缩率\cin>>choice; }

cout<<\谢谢使用*\ //退出操作// return 0; } .h

#include using namespace std;

struct signode{ //signode节点,哈夫曼树节点//

char c; //字符// int weight; //权重//

bool b; //文章中是否出现// signode * parent; signode * left; signode * right;

signode(){ //初始化//

c=NULL; b=false; weight=0;

parent=left=right=NULL;

};

signode SN[256];

signode * forest[256]; //森林数组保存出现的字符// int count=0; //出现字符计数//

float memo1=0,memo2=0; //全局变量记录读入字符数和编码的0 1数//

void init(signode * sig){ //SN[]数组初始化,输入常见字符//

sig[0].c='a';sig[1].c='b';sig[2].c='c'; sig[3].c='d';sig[4].c='e'; sig[5].c='f';sig[6].c='g';sig[7].c='h';sig[8].c='i';sig[9].c='j'; sig[10].c='k';sig[11].c='l';sig[12].c='m';sig[13].c='n';sig[14].c='o'; sig[15].c='p';sig[16].c='q';sig[17].c='r';sig[18].c='s';sig[19].c='t'; sig[20].c='u';sig[21].c='v';sig[22].c='w';sig[23].c='x';sig[24].c='y'; sig[25].c='z'; }

sig[26].c='A';sig[27].c='B';sig[28].c='C';sig[29].c='D';sig[30].c='E'; sig[31].c='F';sig[32].c='G';sig[33].c='H';sig[34].c='I';sig[35].c='J'; sig[36].c='K';sig[37].c='L';sig[38].c='M';sig[39].c='N';sig[40].c='O'; sig[41].c='P';sig[42].c='Q';sig[43].c='R';sig[44].c='S';sig[45].c='T'; sig[46].c='U';sig[47].c='V';sig[48].c='W';sig[49].c='X';sig[50].c='Y'; sig[51].c='Z';

sig[52].c='0';sig[53].c='1';sig[54].c='2';sig[55].c='3';sig[56].c='4'; sig[57].c='5';sig[58].c='6';sig[59].c='7';sig[60].c='8';sig[61].c='9';

sig[62].c='+';sig[63].c='-';sig[64].c='*';sig[65].c='/';sig[66].c=',';

sig[67].c='.';sig[68].c='\\''; sig[69].c='\ }

void compress(){ //压缩情况对比// cout<<\压缩前:\ 压缩后:\cout<<\压缩率:\}

struct hufnode{ //哈夫曼编码对照表节点//

signode * sig;

int code[100]; //保存哈夫曼编码// int size;

bool b; hufnode(){sig=NULL;size=0;b=true;}

};

hufnode hufNode[256];

void exchange(){ //调换首尾交换哈夫曼编码// int temp;

for(int i=0;hufNode[i].sig!=NULL;i++){

for(int s=0,b=hufNode[i].size-1;s<=b;s++,b--){ temp=hufNode[i].code[s];

hufNode[i].code[s]=hufNode[i].code[b]; hufNode[i].code[b]=temp }

} }

sig[72].c='<';sig[73].c='>';sig[74].c='=';sig[75].c='?';sig[76].c=' '; sig[77].c='(';sig[78].c=')';sig[79].c='[';sig[80].c=']';sig[81].c='{'; sig[82].c='}';sig[83].c='!';sig[84].c='@';sig[85].c='#';sig[86].c='$'; sig[87].c='%';sig[88].c='^';sig[89].c='&';sig[90].c='\\\\';sig[91].c=10;

class HFM{ //哈夫曼类// private:

signode * root; //哈夫曼树根// signode * pt; //编码时做哨兵指针// int alleaf;

public:

HFM(int all){root=pt=NULL;alleaf=all;}//all是森林中树的个数// ~HFM(){}

signode * getroot(){return root;}

signode * creat(); //创建哈夫曼树// void hufcode(); //编码//

void savewithhufcode(FILE * inf,FILE * outf); //用哈弗曼编码存储文件// void hufdecode(FILE* ipf,FILE* opf); //解码// void inorder(signode * sig);

int maxc(); //求取哈夫码曼最大长度//

};

signode * HFM::creat(){

signode * pp=NULL;

for(int i=0;ib=false; //为hufcode函数作准备,与此函数无关// while(count>1){

int min=10000; int min1,min2;

for(int i=0;forest[i]!=NULL;i++){ //以下三个

for

循环选出当前森林中的最小两个节点//

if(forest[i]->weightweight;min1=i;} // } // min=10000; // for(i=0;forest[i]!=NULL&&i!=min1;i++){ //

if(forest[i]->weightweight;min2=i;} // }

for(i=min1+1;forest[i]!=NULL;i++){ //

if(forest[i]->weightweight;min2=i;} //

} //至此找到min1 min2

pp=new signode(); //新生成节点,权值为两最小节点权值之和// pp->left=forest[min1]; pp->right=forest[min2];

forest[min2]->b=true; //为hufcode函数作准备,与此函数无关// pp->weight=forest[min1]->weight+forest[min2]->weight; forest[min1]->parent=pp; forest[min2]->parent=pp;

forest[min1]=pp; //新生成节点加入森林for(i=min2;forest[i]!=NULL;i++)forest[i]=forest[i+1]; //min2后的节点依次前移// count--;

搜索更多关于: 哈夫曼编码译码器实验报告(免费) 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

FILE * f1=fopen(\FILE * f2=fopen(\FILE * f3=fopen(\ int main(){ init(SN); //初始化字符数据库// input(f1); //读入初始文件的字符// for(int i=0;forest[i]!=NULL;i++) cout

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com