📄 +
字号:
#include <stdio.h> /*宏定义*/
#include <stdlib.h>
# define n 27
# define m 2*n-1
# define max 2048
/*构造hufm树*/
typedef struct
{
double weight;
int lchild,rchild,parent;
} hufmtree;
hufmtree tree[m];
void kerry_hufmtree()
{
int i,j,p1,p2;
double small1,small2;
for(i=0;i<m;i++)
{
tree[i].parent=-1;
tree[i].lchild=0;
tree[i].rchild=0;
tree[i].weight=0;
}
tree[0].weight=0.063;
tree[1].weight=0.0105;
tree[2].weight=0.023;
tree[3].weight=0.035;
tree[4].weight=0.105;
tree[5].weight=0.0225;
tree[6].weight=0.011;
tree[7].weight=0.047;
tree[8].weight=0.055;
tree[9].weight=0.001;
tree[10].weight=0.003;
tree[11].weight=0.029;
tree[12].weight=0.021;
tree[13].weight=0.059;
tree[14].weight=0.0654;
tree[15].weight=0.0175;
tree[16].weight=0.001;
tree[17].weight=0.054;
tree[18].weight=0.052;
tree[19].weight=0.072;
tree[20].weight=0.0225;
tree[21].weight=0.008;
tree[21].weight=0.012;
tree[23].weight=0.002;
tree[24].weight=0.012;
tree[25].weight=0.001;
tree[26].weight=0.2;
for(i=27;i<m;i++)
{
p1=0;
p2=0;
small1=100;
small2=100;
for(j=0;j<i;j++)
if(tree[j].parent==-1)
if(tree[j].weight<small1)
{
small2=small1;
small1=tree[j].weight;
p2=p1;
p1=j;
}
else
if(tree[j].weight<small2)
{
small2=tree[j].weight;
p2=j;
}
tree[p1].parent=i;
tree[p2].parent=i;
tree[i].lchild=p1;
tree[i].rchild=p2;
tree[i].weight=tree[p1].weight+tree[p2].weight;
}
}
/* 构造完毕*/
/*编码*/
typedef struct character
{
int bits[n];
int start;
char ch;
}codetype;
codetype code[n];
void kerry_hufmancode()
{
int i,c,p;
codetype cd;
for(i=0;i<n;i++)
{
cd.start=n;
c=i;
p=tree[i].parent;
while(p!=-1)
{
cd.start--;
if(tree[p].lchild==c)
cd.bits[cd.start]=0;
else
cd.bits[cd.start]=1;
c=p;
p=tree[p].parent;
}
code[i]=cd;
}
}
/*编码完毕,但未把char 赋值*/
/*把字符赋进数组里*/
void kerry_charcode()
{
int i,j='a';
for(i=0;i<26;i++)
{
code[i].ch=j;
j++;
}
code[26].ch=' ';
}
/*赋字符完毕*/
/*到hufmancode 即 编码 $ 的函数*/
codetype code2[max];
void kerry_tohufm()
{
char a;
int j,i,o;
a=getchar();
i=0;
while(a!='$')
{
if(97<=a&&a<=122)
{
j=a-97;
code2[i]=code[j];
}
if(a==' ')
{
j=26;
code2[i]=code[26];
}
i++;
a=getchar();
}
for(j=0;j<i;j++)
{
o=code2[j].start;
while(o!=n)
{
printf("%d",code2[j].bits[o]);
o++;
}
printf(" ");
}
}
/*到hufmancode 即 编码 $ 的函数完毕*/
/*到字符( 译码 )的函数*/
void kerry_tochar()
{
int i;
char b,a;
i=m-1;
b=getchar();
while(b!='2')
{
if(b!='0'&&b!='1')
{
printf(" input error ! ");
break;
}
if(b=='0') i=tree[i].lchild;
if(b=='1') i=tree[i].rchild;
if(tree[i].rchild==0)
{
putchar(code[i].ch);
i=m-1;
}
b=getchar();
}
if(tree[i].lchild!=0&i!=m-1)
printf(" ---is the result; but some code may be input error!\n");
}
/*到字符( 译码 )的函数完毕*/
/*选择函数*/
int change()
{
char a,b,c,d,e;
while(1)
{
printf(" 欢迎进入Kerry HAFUMAN code系统!!! \n ");
printf(" ** [进入请输入I 退出输入O (并以回车结束!)]** \n ");
a=getchar();
b=getchar();
if(a=='O')
return(0);
if(a=='I')
printf("欢迎进入,编码请输入A译码输入B,返回输入C,返回主界面输入D \n ");
if(a!='I')
{
printf(" %%%%输入错误,无法让您键入系统! 可以重新输入!%%%%%%\n\n\n\n ");
continue;
}
while(1)
{
printf(" 编码记得以$结束,译码记得以2结束 \n");
c=getchar();
d=getchar();
if(c=='A')
{
kerry_tohufm();
e=getchar();
printf(" \n !!!!!OK!!!!! \n");
}
if(c=='B')
{
kerry_tochar();
e=getchar();
printf(" \n !!!!!OK!!!!! \n");
}
if(c=='C')
continue;
if(c=='D')
break;
if(c!='A'&c!='B'&c!='C'&c!='D')
{
printf("输入错误了!还可以继续啊!\n");
continue;
}
printf("\n\t(恭喜您)——————操作完成 输入A,B,C或D\n");
}
}
}
/*主函数*/
int main()
{
kerry_hufmtree();
kerry_hufmancode();
kerry_charcode();
change();
printf(" \n 结束了!再见! \n ");
}
/*程序结束*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -