哈夫曼编译码器遇到的问题

 
#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
 
编写程序过程中遇到的问题
在编写程序过程中觉得还不够熟练,还是遇到些问题
在编写的程序中还有个问题不能解决,就是编码,译码,退出程序只能运行一次,第二次就不能运行了。想了很久还是不能解决。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值