社区
数据结构与算法
帖子详情
500分求解哈夫曼编码的压缩与解压程序
renhao1982
2001-11-25 02:36:41
将一个文本文件进行哈夫曼编码,以文本文件中各字符的个数作为权值,然后生成一个编码文件,包括哈夫曼树和编码序列。解压程序是读出哈夫曼树,输入一文件名,然后解码至文件中。请各位高手帮忙啊!
解决的话,一直加到500分!!!1
...全文
599
42
打赏
收藏
500分求解哈夫曼编码的压缩与解压程序
将一个文本文件进行哈夫曼编码,以文本文件中各字符的个数作为权值,然后生成一个编码文件,包括哈夫曼树和编码序列。解压程序是读出哈夫曼树,输入一文件名,然后解码至文件中。请各位高手帮忙啊! 解决的话,一直加到500分!!!1
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
42 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
huanghaohua
2002-05-19
打赏
举报
回复
不用分隔标志呀,搜索到叶子就是结束了嘛
h209727
2002-05-18
打赏
举报
回复
莫名其妙,自己不做,去找别人要!
给的人也是莫名其妙,给别人作业就是毁掉别人的学习动力!你给贴出来,只是表现你会做而已,还有什么?
自己努力一次会学到很多东西的。
给E_mial 的好象都想要哦。
我是大二的,学计算机,正好也在学《数据结构》,和同学计算机的大二学生还可以交流下,我的E_mail是:h209727@163.com
wyly
2002-05-18
打赏
举报
回复
这种东西又不难,自己试试看。不要动不动就要源码!!!!!!!!!!
hongyucn
2002-05-18
打赏
举报
回复
其实做Jpeg压缩算法中就有Huffman算法!自己去写以下Jpeg算法就知道了!
opengl3d
2002-05-16
打赏
举报
回复
拿来主义是也
xiongxiao
2002-05-16
打赏
举报
回复
学习
fyxhw
2002-05-16
打赏
举报
回复
有没有delphi的源码如有,请给我一份我现在正在做一个MIS系统想将最后的备份数据压缩。我的E-mail:fy_xhw@163.net
systree
2002-05-16
打赏
举报
回复
jpeg的标准组织提供了源码,那里面的编码方法
和程序处理都很标准,效率也还可以,
是个学习的好样本
tpu
2002-05-16
打赏
举报
回复
你们这帮人就知道要,要,要。自己看懂了,做出来了,才是自己的。想不劳而获是不行的。
你们可以研究一下jpeg标准里面是怎样实现的。
LIDEHUA1975
2002-05-15
打赏
举报
回复
我也想要,非常感谢!我的邮箱是lidehua1975@163.com
panxiongfei
2002-05-15
打赏
举报
回复
你好!我也要!pan-xiongfei@163.com
qiangqiang1112
2002-05-15
打赏
举报
回复
我也想要一份!qiangqiang_1112@163.com
xiexie !!!!!!!!
lele130
2002-04-29
打赏
举报
回复
我也想要,非常感谢!我的邮箱是lanmok@263.net
growbit
2002-03-24
打赏
举报
回复
我也想要,非常感谢!我的邮箱是GROWBIT@YAHOO.COM.CN
foxmike
2002-01-19
打赏
举报
回复
我有huffman编码的压缩和解压缩源程序,可能帮得上你的忙,你留下
email地址吧。我发给你。
cycsain
2002-01-18
打赏
举报
回复
能不能给我一份源代码??
cycsain@sina.com
谢谢!
andy_lau
2002-01-09
打赏
举报
回复
你真的很幸运,我前两天刚做好,共享一下(题目要求基本上和你的要求一直)\/////////////////////////////////////////////////////////////////////////////////
题目要求:1)建立一正文文件(本程序中建立的文件名文test.txt),向其中输入字符(本程序中可输入任意ASCII值)
2)通过哈夫曼算法求出文件中字符的相应编码
3)建立一与正文文件(test.txt)对应的编码文件(本程序中为bainma.txt)
//////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
/*声明两种链表结构----start*/
struct node_a{ /*链表1-----作用:用来统计文件中字符的个数和种类(通过count)*/
char data;
int count;
struct node_a *next;
};
typedef struct node_a node,*list;
list head=NULL;
struct nodeb{ /*链表2-----作用:用来建立用相应的编码代替字符的新文件*/
char data;
struct nodeb *next;
};
typedef struct nodeb node_b,*list_b; /*jiang bianma xieru wenjian*/
list_b head_b=NULL;
/*声明两种链表结构----end*/
/*哈夫曼算法种相关的3种结构的声明-----start*/
struct forest{
float weight;
int root;
};
struct alphabet{
char symbol;
float probability;
int leaf;
char *bianma;
};
struct tree{
int lchild;
int rchild;
int parent;
};
typedef struct tree *tree_ptr,tree_node;
typedef struct forest *forest_ptr,forest_node;
typedef struct alphabet *alphabet_ptr,alphabet_node;
tree_ptr tree1;
forest_ptr forest1;
alphabet_ptr alphabet1;
int lasttree,lastnode;
int least,second;
/*哈夫曼算法种相关的3种结构的声明-----end*/
/**************stack difination start****************/
struct stacknode{
char *bian_ma;
struct stacknode *next;
};
typedef struct stacknode stack_node;
typedef stack_node *link;
link top=NULL;
void push(char *item){
link p;
if(top!=NULL){
p=(link)malloc(sizeof(stack_node));
if(p==NULL){
printf("Memory allocation error!");
return;
}
p->bian_ma=item;
p->next=top;
top=p;
}
else{
top=(link)malloc(sizeof(stack_node));
if(!top){
printf("Memory allocation error!");
return;
}
top->bian_ma=item;
top->next=NULL;
}
}
void pop(void){
link p;
p=top;
top=top->next;
free(p);
}
void makenull(void){
while(top!=NULL)
pop();
}
int empty(){
if(top==NULL)
return 1;
else
return 0;
}
char* tops(void){
return (top->bian_ma);
}
void display_stack(link s){ /*显示栈重的内容*/
link ptr;
ptr=s;
while(ptr!=NULL){
printf("%s",ptr->bian_ma);
ptr=ptr->next;
}
}
/***************************stack__difination is end************************/
void display(list h){ /*显示链表1*/
list ptr;
int i=1;
ptr=h->next;
while(ptr!=NULL){
printf("%d,%c,%d\n",i,ptr->data,ptr->count);
i++;
ptr=ptr->next;
}
}
void display_b(list_b h){ /*显示链表2*/
list_b ptr;
int i=1;
ptr=h->next;
while(ptr!=NULL){
printf("%d,%c\n",i,ptr->data);
i++;
ptr=ptr->next;
}
}
void insert(char item){ /*用于插入元素以建立链表1*/
list temp,ptr;
int flag;
ptr=head->next;
if(ptr==NULL){
head->next=(list)malloc(sizeof(node));
head->next->data=item;
head->next->count=1;
head->next->next=NULL;
}
else{
while(ptr!=NULL){
if(ptr->data==item){
ptr->count=ptr->count+1;
flag=1;
}
ptr=ptr->next;
}
ptr=head;
if(flag==1)
return;
else{
temp=ptr->next;
ptr->next=(list)malloc(sizeof(node));
ptr->next->data=item;
ptr->next->count=1;
ptr->next->next=temp;
}
}
}
void insert_b(char item){ /*插入元素以建立链表2*/
list_b ptr_b,temp_b;
ptr_b=head_b;
if(ptr_b->next==NULL){
head_b->next=(list_b)malloc(sizeof(node_b));
head_b->next->data=item;
head_b->next->next=NULL;
}
else{
while(ptr_b->next!=NULL){
ptr_b=ptr_b->next;
}
ptr_b->next=(list_b)malloc(sizeof(node_b));
ptr_b->next->data=item;
ptr_b->next->next=NULL;
}
}
void search(void){ /*搜索文件并将文件中的数据分别存入作用不同的链表中*/
FILE *fp;
list ptr;
char ch;
if((fp=fopen("test.txt","r"))==NULL)
printf("Reading error!\n");
while(!feof(fp)){
ch=getc(fp);
if(ferror(fp)){
printf("error!\n");
break;
}
if(ch==EOF)
break;
insert(ch); /*插入元素进链表1*/
insert_b(ch); /*插入元素进链表2*/
}
printf("\n");
fclose(fp);
}
void display_struct(int n){ /*显示哈夫曼算法中的3个结构树组 */
int i=0;
printf("\n\n=======================================\n\n");
printf("FOREST_STRUCT_ARRY :\n\n\n");
for(i=0;i<=n;i++){
printf("%f,%d\n",forest1[i].weight,forest1[i].root);
}
getch();
printf("\n\nALPHABET_STRUCT_ARRY :\n\n\n");
for(i=0;i<=n;i++){
printf("%f,%d,%c\n",alphabet1[i].probability,alphabet1[i].leaf,alphabet1[i].symbol);
}
getch();
printf("\n\nTREE_STRUCT_ARRY :\n\n\n");
for(i=0;i<=2*n-1;i++)
printf("%d,%d,%d\n",tree1[i].lchild,tree1[i].rchild,tree1[i].parent);
printf("\n\n======================================\n\n");
getch();
}
int init_struct(void){ /*初始化哈夫曼算法中3种结构数组*/
list ptr;
float count=.0;
int i=1,n=0;
ptr=head->next;
while(ptr!=NULL){
count=ptr->count+count;
n++;
ptr=ptr->next;
}
ptr=head->next;
forest1=(forest_ptr)malloc(sizeof(forest_node)*n+sizeof(forest_node));
alphabet1=(alphabet_ptr)malloc(sizeof(alphabet_node)*n+sizeof(alphabet_node));
tree1=(tree_ptr)malloc(sizeof(tree_node)*n*2-sizeof(tree_node));
forest1[0].weight=alphabet1[0].probability=0.0;
forest1[0].root=alphabet1[0].leaf=0;
alphabet1[0].symbol='0';
while(ptr!=NULL){
forest1[i].weight=alphabet1[i].probability=ptr->count/count;
forest1[i].root=alphabet1[i].leaf=i;
alphabet1[i].symbol=ptr->data;
i++;
ptr=ptr->next;
}
for(i=0;i<=2*n-1;i++){
tree1[i].lchild=0;
tree1[i].rchild=0;
tree1[i].parent=0;
}
return n;
}
void creat(void){ /*创建正文文件test.txt*/
FILE *fp,*out;
char ch;
if((fp=fopen("test.txt","w+t"))==NULL)
printf("Creat error!\n");
printf("Input the data:\n");
ch=getch();
putch(ch);
while(ch!='#'){
putc(ch,fp);
ch=getch();
putch(ch);
}
fclose(fp);
}
void creat_bianma(int number){ /*根据哈夫曼算法建立文件中各种字符对应的编码*/
int i,j,n;
int p;
char *bm=malloc(sizeof(char)*number);
for(n=1;n<=number;n++){
j=i=n;
makenull();
p=tree1[i].parent;
while(tree1[p].parent!=0){
if(tree1[p].lchild==i)
push("0");
else
push("1");
i=p;
p=tree1[p].parent;
}
if(tree1[p].lchild==i)
push("0");
else
push("1");
strcpy(bm," "); /*目的:使创建编码文件中的各编码中间存在间隔*/
while(!empty()){
strcat(bm,tops());
pop();
}
alphabet1[j].bianma=malloc(sizeof(char)*number);
strcpy(alphabet1[j].bianma,bm);
printf("\n%c:%s",alphabet1[j].symbol,alphabet1[j].bianma); /*打印出相应的编码*/
getch();
}
}
void write_new_file(int number){ /*根据相应的编码重新建立编码文件*/
FILE *fp;
list_b ptr;
int i;
char *ch=malloc(sizeof(char)*number);
ptr=head_b->next;
if((fp=fopen("bianma.txt","w"))==NULL)
printf("Write in a new file error!");
else{
while(ptr!=NULL){
for(i=1;i<=number;i++){
if(ptr->data==alphabet1[i].symbol){
strcpy(ch,alphabet1[i].bianma);
fputs(ch,fp);
}
}
ptr=ptr->next;
}
}
fclose(fp);
}
void main(void){
int i,num;
char ch;
void huffman(void);
void lightones();
head=(list)malloc(sizeof(node));
head->next=NULL;
head->data='';
head->count=0;
head_b=(list_b)malloc(sizeof(node_b));
head_b->data='';
head_b->next=NULL;
do{
system("cls");
creat();
search();
printf("\nlianbiao1:\n");
display(head);/*显示链表1*/
getch();
printf("\nlianbiao2:\n");
display_b(head_b);
getch();
num=init_struct();
printf("\n");
printf("The 3 init_struct of huffman?\n");
display_struct(num);/*显示初始化的哈夫曼书的相关3个结构数组*/
lastnode=num;
lasttree=num;
huffman();
printf("Now the 3 struct through huffman shuanfa\n");
display_struct(num);/*显示哈夫曼树中相关的3种结构(经过哈夫曼算法处理)*/
printf("\nThe bian_ma is:\n");
creat_bianma(num);
write_new_file(num);
printf("\nDo you want to re_try(y/n)?");
ch=getchar();
}while(ch=='y');
}
/*哈夫曼算法-----defination_start*/
void lightones(void){
int i;
if(forest1[1].weight<=forest1[2].weight){
least=1;
second=2;
}
else{
least=2;
second=1;
}
for(i=3;i<=lasttree;i++)
if(forest1[i].weight<forest1[least].weight){
second=least;
least=i;
}
else
if(forest1[i].weight<forest1[second].weight)
second=i;
}
int creat_tree(int lefttree,int righttree){
lastnode=lastnode+1;
tree1[lastnode].lchild=forest1[lefttree].root;
tree1[lastnode].rchild=forest1[righttree].root;
tree1[lastnode].parent=0;
tree1[forest1[lefttree].root].parent=lastnode;
tree1[forest1[righttree].root].parent=lastnode;
return(lastnode);
}
void huffman(void){
int i,j;
int newroot;
while(lasttree>1){
lightones();
i=least;
j=second;
newroot=creat_tree(i,j);
forest1[i].weight=forest1[i].weight+forest1[j].weight;
forest1[i].root=newroot;
forest1[j]=forest1[lasttree];
lasttree=lasttree-1;
}
}
/*哈夫曼算法-----defination end*/
编译环境:windows_2000P TURBOC_2.0
NewViewer
2002-01-09
打赏
举报
回复
我知道该如何做了。其实生成哈夫曼编码很简单,关键的是编码树的存储方式,即如何在压缩文件中存储编码树及其他相关信息,这是解码的关键。
我的一种设想的格式是:
[分隔标志编码]原文件信息,如文件名等。。[分隔标志编码]
。。。编码树数据。。。[分隔标志编码]
编码后数据[分隔标志编码]
难在编码树数据存储,如何根据这些数据新组织起一棵树?
leonadoromario
2002-01-08
打赏
举报
回复
用有限自动状态机识别
原理可以看 编译原理
很容易的。
要是不明白,可以问我。
leonadoromario@263.net
justtest
2002-01-08
打赏
举报
回复
请有程序的朋友尽快发一分到我邮箱可否,justali@21cn.com
加载更多回复(22)
哈夫曼树和
哈夫曼编码
(数据结构
程序
设计).docx
哈夫曼树和
哈夫曼编码
(数据结构
程序
设计).docx哈夫曼树和
哈夫曼编码
(数据结构
程序
设计).docx
Shannon_Python香农编码_python_shannon_香农编码_
基于python的香农编码实现,进攻大家交流学习。
数据结构课程设计题目表
数据结构的练习,数据结构初学者的重要参考资料,胡学钢老师教的课
数据结构习题
关于数据结构的课设题,十
分
有效可以练习。
数据结构题目
题目
数据结构与算法
33,028
社区成员
35,337
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章