huffman编码&解码器 (转)

huffman编码&解码器 (转)[@more@]

/*huffman coder & decoder*/

include
#ifndef N
#define N 27
#endif
#define M (2*N-1)
#define Max (100*N)
#define Min  5
/*define each node's imformation*/
typedef struct nodetype
{
int weight ;
int lch;
int rch;
int parent;
char data;
}node;
/*structure code*/
typedef struct codetype
{
int bits[N];/*0,1*/
int start;/*1..n*/
}code;
/*assistant variable*/
typedef struct sign
{
int wt;
int num;
}tag;



/*typedef node huftree;
typedef code hufcode;*/

/*create huftree*/
void Create(struct nodetype  ht0[],struct codetype  hcd0[])
{
int h,i,j,l,k;
int wt;
int c1,sgn=0,r=1;
char chr;
tag flag[M];
tag md;
code cd;

for(i=1;i<=M;i++)
{
ht0[i-1].parent=0;
ht0[i-1].lch=ht0[i-1].rch=0;
}

for(i=1;i<=N;i++)
{
getchar();
printf(" 请输入第%d个数",i);
printf(" 请输入数据信息:字符--");
chr=getchar();
if(((chr>='a')&&(chr<='z'))||((chr>='A')&&(chr<='Z')))
  ht0[i-1].data=chr;
else
{
printf(" 太不小心了吧!Again!AgainAgain!!");
i++;
continue;
}
printf(" 请输入数据信息:权重--");
scanf("%d",&wt);
ht0[i-1].weight=wt;
}
  printf(" 辛苦了:).上帝是仁慈的,瞧!有结果了.仰天长笑吧!哈-哈哈!");

for(i=N+1;i<=M;i++)
{

for(k=1,j=0;k<=i-1;k++)
if(ht0[k-1].parent==0)
{
j++;
flag[j-1].wt=ht0[k-1].weight;
flag[j-1].num=k;
    }
for(l=1;l{
for(h=l+1;h<=j;h++)

if(flag[l-1].wt>flag[h-1].wt)
{
md=flag[l-1];
flag[l-1]=flag[h-1];
flag[h-1]=md;
}
}
ht0[flag[1-1].num-1].parent=i;
ht0[flag[2-1].num-1].parent=i;
ht0[i-1].lch=flag[1-1].num;
ht0[i-1].rch=flag[2-1].num;
ht0[i-1].weight=ht0[flag[1-1].num-1].weight+ht0[flag[2-1].num-1].weight;
}
/**/

for(i=0;i<=N;i++)
cd.bits[i-1]=0;
for(r=1;r<=N;r++)
{
cd.start=N;
c1=r;
sgn=ht0[c1-1].parent;

while(sgn)
{
if(ht0[sgn-1].lch==c1)
cd.bits[cd.start-1]=0;

else if(ht0[sgn-1].rch==c1)
cd.bits[cd.start-1]=1;
cd.start--;
c1=sgn;
sgn=ht0[sgn-1].parent;
}
hcd0[r-1]=cd;
}
}


/**/
void Table(struct nodetype ht[],struct codetype hcd[])
{
int i,j;
Create(ht,hcd);
for(i=1;i<=N;i++)
{
printf(" %c ",ht[i-1].data);
for(j=hcd[i-1].start+1;j<=N;j++)
{
printf("%d",hcd[i-1].bits[j-1]);
}
}

}

/**/
void Coding(node ht2[],code hcd2[])
{
char str1[Max];
int h,i,j,k=0;
Create(ht2,hcd2);
printf(" 请输入正文: ");
for(h=0;(str1[h]=getchar())!='#';h++);/*great!!!!!!!!!!!!*/
while(str1[k]!=0)
{
for(i=1;i<=N;i++)
if(ht2[i-1].data==str1[k])
for(j=hcd2[i-1].start+1;j<=N;j++)
printf("%d",hcd2[i-1].bits[j-1]);
k++;
}
printf(" 哇塞!太幸福了. ");
}
/**/
void Decoding(node ht3[],code hcd3[])
{
char *str2=" ";
node q;
Create(ht3,hcd3);
printf(" 请输入编码: ");
scanf("%s",str2);
while(*str2)
{
q=ht3[M-1];
while(q.lch!=0)
{
if(*str2=='0')
{
q=ht3[q.lch-1];
str2++;
}
else if (*str2=='1')
{
q=ht3[q.rch-1];
str2++;
}
}
printf("%c",q.data);
}
printf(" 呼呼!看不懂得01真的很浪漫哟:)");
}

/**/


void main()
{
  char ctrl,ctrl1,ctrl2;
  int i=0;
  node ht1[M];
  code hcd1[N];

 do
 {
printf(" 欢迎你来到哈夫曼王国!! 该系统具有以下功能: (1):建立哈夫曼编码树 (2):输出编码表 (3):编码 (4):译码 (0):退出。再见!!!! 选择 :--|0|1|2|3|4| 可实现你的要求:--");
i++;
  scanf("%c",&ctrl);
  scanf("%c",&ctrl2);
  if(ctrl=='1')
Create(ht1,hcd1);
else if(ctrl=='2')
  Table(ht1,hcd1);
  else if(ctrl=='3')
  Coding(ht1,hcd1);
  else if(ctrl=='4')
  Decoding(ht1,hcd1);
  else if(ctrl=='0')
    goto loop;
else if(Min-i>0)
{
  printf(" 哈哈!你错了哟!看一看提示吧:) 要珍惜机会哦!! p(^_^)q ");

}

  if ((Min-i)==0)
  printf(" 欢迎再来! Bye,HAVE A GOOD DAY!   ");
  else
  {
printf(" 只有%d次机会了",Min-i);
printf(" 还要继续吗?加油喔:) y|n");
printf(" ");
scanf("%c",&ctrl1);
    }
  getchar();/*waiting*/
  }while((ctrl1=='y')&&(i<=Min-1));
  loop:
  ;
 }


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-956457/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-956457/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值