《数据构造》实验报告
班级:
姓名:
学号:
E-mail:
日期:
◎实验题目:
P149哈夫曼编/译码器
完成Huffman编码的译码过程。即输入一个+1]);
break;
}
}
}
printf("\n\n");
return;
}
voidyima(charstr[],chars[],huffmancodehc)//译码
{
intj,t=0;
char*p,*r,*m;
p=str;
m=str;
for(;*p<='1'&&*p>='0';)
{
for(t=0,j=1;j<=27;j++)
{
r=hc[j];
while(*p==*r&&*p!='\0')
{
p++,r++,t++;
}
if(*r=='\0')
{
printf("%c\n",s[j-1]);
break;
}
p=m;
t=0;
}
p=m+t;
m=p;
}
printf("\n\n");
return;
}
intmain()//主函数
{
inti=0,n=27,m;
int*w;
chars[27],p[100];
huffmantreeht;
huffmancodehc;
hc=(huffmancode)malloc(n*sizeof(huffmancode));
w=(int*)malloc(n*sizeof(int));
FILE*fp=fopen("","r");
while(!feof(fp))
{
fscanf(fp,"%c%d\n",&s[i],&w[i]);
i++;
}
huffmancoding(ht,hc,w,n);
printf("输出编码为:\n");
for(i=0;i<n;i++)
{
printf("字母为:%c编码为:%s\n",s[i],hc[i+1]);
}
while(1)
{
printf("编码:1\n译码:2\n结束:0\n");
scanf("%d",&m);
getchar();
gets(p);
switch(m)
{
case1:
bianma(p,s,hc);
break;
case2:
yima(p,s,hc);
break;
case0:
return0;
default:
printf("error\n\n");
break;
}
}
fflush(stdin);
getchar();
return0;
}(四)程序使用说明及测试结果
1.程序使用说明
1)本程序的运转环境为。
2)进入演示程序后即显示提示信息:
哈夫曼数据结构实验报告 来自淘豆网m.daumloan.com转载请标明出处.