哈夫曼编译码器遇到的问题
来源:互联网 发布:淘宝写文案范文 编辑:程序博客网 时间:2024/05/17 06:43
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#define MAX 25
typedef struct
{
char data;
int weight;
int parent;
int lchild;
int rchild;
} HTNode;
typedef struct
{
char cd[MAX];
int start;
} HuffmanCode;
int main()
{
HTNode ht[2*MAX];
HuffmanCode hcd[MAX], d;
int i, k, f, l, r, n, c, s1, s2;
cout<<"* * * * * * * * * * * * * * * * * * * * * /n"
<<"/t哈夫曼编码与译码系统/n"
<<"* * * * * * * * * * * * * * * * * * * * * /n";
cout<<"/n请输入哈夫曼码元素个数:";
cin>>n;
cout<<"请输入各个元素的结点值与权值:/n";
for(i=1;i<=n;i++)
{
cout<<" 第"<<i<<"个元素-->/n/t结点值:";
cin>>&ht[i].data;
cout<<"/t权 值:";
cin>>ht[i].weight;
}
for(i=1;i<=2*n-1;i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=0;
for(i=n+1;i<=2*n-1;i++)
{
s1=s2=32767;
l=r=0;
for(k=1;k<=i-1;k++)
if(ht[k].parent==0)
if(ht[k].weight<s1)
{
s2=s1;
r=l;
s1=ht[k].weight;
l=k;
}
else if(ht[k].weight<s2)
{
s2=ht[k].weight;
r=k;
}
ht[l].parent=i;
ht[r].parent=i;
ht[i].weight=ht[l].weight+ht[r].weight;
ht[i].lchild=l;
ht[i].rchild=r;
}
for(i=1;i<=n;i++)
{
d.start=n+1;
c=i;
f=ht[i].parent;
while(f!=0)
{
if(ht[f].lchild==c)
d.cd[--d.start]='0';
else
d.cd[--d.start]='1';
c=f;
f=ht[f].parent;
}
hcd[i]=d;
}
cout<<"输出哈夫曼编码:/n";
for(i=1;i<=n;i++)
{
cout<<ht[i].data<<": ";
for(k=hcd[i].start;k<=n;k++)
cout<<hcd[i].cd[k];
cout<<"/n";
}
l: cout<<"/n请选择编码/译码/退出系统: (B/Y/E): ";
char hfm;
cin>>hfm;
if(hfm=='e')
return 0;
else
{
switch(hfm)
{
case'b':
{
int q ;
char bs;
cout<<"/n* * * 哈夫曼编码 * * */n";
cout<<"请输入字符代码: "<<endl;
for(q=0;bs!=10;q++)
{
bs=getchar();
for(i=1;i<=n;i++)
{
if (bs==ht[i].data)
for(k=hcd[i].start;k<=n;k++)
cout<<hcd[i].cd[k];
}
}
cout<<endl;
} break;
case'y':
{
char e;
int t,u;
t=2*n-1;
cout<<"/n* * * 哈夫曼译码 * * */n";
cout<<"/n请输入哈夫曼码: "<<endl;
for(u=0;e!=10;u++)
{
if(ht[t].lchild!=0)
{
e=getchar();
if(e=='0')
t=ht[t].lchild;
else
t=ht[t].rchild;
}
else
{
cout<<ht[t].data;
t=2*n-1;
}
}
cout<<endl;
} break;
}
goto l;
}
return 0;
} //Huffman
编写程序过程中遇到的问题
在编写程序过程中觉得还不够熟练,还是遇到些问题
在编写的程序中还有个问题不能解决,就是编码,译码,退出程序只能运行一次,第二次就不能运行了。想了很久还是不能解决。
- 哈夫曼编译码器遇到的问题
- 哈夫曼编/译码器
- 哈夫曼编译码器
- 哈夫曼编/译码器
- 哈夫曼编/译码器
- 哈夫曼编/译码器
- 哈夫曼编/译码器
- 哈夫曼编译码器的实现 使用文件
- 指令译码器的作用
- 译码器的应用
- 译码器
- 译码器
- 基于switch语句的译码器
- 3-8译码器的设计
- 译码器的定义及功能
- 哈夫曼编码/译码器的实现
- 3.5 74HC138三八译码器的应用
- 51单片机 数码管的显示与译码器
- MappedByteBuffer的umap() JAVA Bug!
- 关于存储过程的参数和ASP优化问题
- WebCast 笔记 -- C#3.0锐利体验(1)
- Java中ThreadLocal的设计与使用
- 软件设计师考纲
- 哈夫曼编译码器遇到的问题
- 所有IS中的自定义窗体
- 无奈也开源
- ARM DSP FPGA的区别
- oracle 使用存储过程分割字符串为数组
- 宇宙将走向哪里
- 一个研究生毕业以后的人生规划
- 学JAVA 从哪入手哦!!!!!!!!!!!!!!!!
- 黑客的故事(搞笑)