用C语言编写哈夫曼的编码能进行英文的的翻译和反编译 求注释详细 好用来学习

u1、能够对27个字符进行编码、并存储.
2、利用编码能够实现任意英文句子的编码操作,并存储编码。
3、能够将二进制的密码翻译成句子,并将译文存储。
4、能随时查看字母的编码.

要求是相同的,不相同的地方就是涉及输入的地方,这个相信你可以自己解决的吧,



#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include<conio.h>

typedef struct

{

int weight;

int parent;

int left;

int right;

}hfmt;

typedef char*hfmcode;

void selectnode(hfmt *ht,int n,int *bt1,int *bt2)

{

int i;

hfmt *ht1,*ht2,*t;

ht1=ht2=NULL;

for(i=1;i<n+1;i++)

{

if(!ht[i].parent)

{

if(ht1==NULL)

{

ht1=ht+i;

continue;

}

if(ht2==NULL)

{

ht2=ht+i;

if(ht1->weight>ht2->weight)

{

t=ht2;

ht2=ht1;

ht1=t;

}

continue;

}

if(ht1&&ht2)

{

if(ht[i].weight<=ht1->weight)

{

ht2=ht1;

ht1=ht+i;

}

else if (ht[i].weight>ht1->weight)

{

ht2=ht+i;

}

}

}

}

if(ht1>ht2){

*bt2=ht1-ht;

*bt1=ht2-ht;

}

else{

*bt1=ht1-ht;

*bt2=ht2-ht;

}

}

void createtree(hfmt *ht,int n,int *w)

{

int i,m=2*n-1,bt1,bt2;

if(n<=1)return;

for(i=1;i<=n;++i)

{

ht[i].weight=w[i-1];

ht[i].parent=0;

ht[i].left=0;

ht[i].right=0;

}

for(;i<=m;i++)

{

ht[i].weight=0;

ht[i].parent=0;

ht[i].left=0;

ht[i].right=0;

}

for(i=n+1;i<=m;i++)

{

selectnode(ht,i-1,&bt1,&bt2);

ht[bt1].parent=i;

ht[bt2].parent=i;

ht[i].left=bt1;

ht[i].right=bt2;

ht[i].weight=ht[bt1].weight=ht[bt2].weight;

}

}


void hfmcoding(hfmt *ht,int n,hfmcode *hc)

{

char*cd;

int start ,i;

int current,parent;

cd=(char*)malloc(sizeof(char)*n);

cd[n-1]='\0';

for(i=1;i<=n;i++)

{

start=n-1;

current=i;

parent=ht[current].parent;

while(parent)

{

if(current==ht[parent].left)

cd[--start]='0';

else 

cd[--start]='1';

current=parent;

parent=ht[parent].parent;

}

hc[i-1]=(char*)malloc(sizeof(char)*(n-start));

strcpy(hc[i-1],&cd[start]);

}

free(cd);

}

void insert(hfmcode *hc,char*alphabet,char*str,char*code)

{

int len=0,i=0,j;

code[0]='\0';

while(str[i])

{

j=0;

while(alphabet[j]!=str[i])

j++;

strcpy(code+len,hc[j]);

len=len+strlen(hc[j]);

i++;

}

code[len]='\0';

}

void delete_(hfmt*ht,int m,char*code,char*alphabet,char*decode)

{

int position=0,i,j=0;

m=2*m-1;

while(code[position])

{

for(i=m;ht[i].left&&ht[i].right;position++)

{

if(code[position]=='0')

i=ht[i].left;

else

i=ht[i].right;

}

decode[j]=alphabet[i-1];

j++;

}

decode[j]='\0';

}

int main()

{

int i,n=4,m;

char test[]="DBDABCDABBCDBADCBDBCDBCDAB";

char code[100],code1[100];

char alphabet[]={'A','B','C','D'};

int w[]={4,9,5,8};

hfmt *ht;

hfmcode *hc;

m=2*n-1;

ht=(hfmt*)malloc((m+1)*sizeof(hfmt));

if(!ht)

{

printf("内存分配失败\n");

exit(0);

}

hc=(hfmcode*)malloc(n*sizeof(char*));

if(!hc)

{

printf("内存分配失败\n");

exit(0);

}

createtree(ht,n,w);

hfmcoding(ht,n,hc);

for(i=1;i<=n;i++)

printf("字母:%c,权重:%d,编码为 %s\n",alphabet[i-1],ht[i].weight,hc[i-1]);

insert(hc,alphabet,test,code);

printf("\n字符串:\n%s\n转换后为:\n%s\n",test,code);

delete_(ht,n,code,alphabet,code1);

printf("\n编码:\n%s\n转换后为:\n%s\n",code,code1);

getch();

return 0;

}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网