哈夫曼编码译码程序出现问题 求大神解答 1C

这两个分别是我在DEV c++ 和VC6.0运行的结果,在VC运行时就出现了程序终止的画面,而且这个程序在编码和译码的时候都不能对空格进行编码和译码 译码在超过一定长度的时候就会不进行译码 求大神解答

#include<stdlib.h>
#include<string.h>
#include<limits.h>
#define N 28
#define MAX 100
typedef struct{
    double weight;
    unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;

void Select(const HuffmanTree &HT,int n,int &s1,int &s2);//找出权值最小的两个节点 

void HuffmanCreat(HuffmanTree &HT,HuffmanCode &HC,double *w,int n);//建立哈夫曼树 

void HuffmanCoding(HuffmanCode &HC,char *key,int n);//编码

void HuffmanDecoding(HuffmanCode &HC,char *key,int n);//译码 

int main()
{
    int i;
    char choice,flag=1;
    char key[N]={'0',' ','E','T','O','A','N','I','R','S','H','D','L','C','U','F','M','P','Y','W','G','B','V','K','X','J','Q','Z'};
    double w[N]={0,0.2,0.105,0.071,0.0644,0.063,0.059,0.054,0.053,0.052,0.047,0.035,0.029,0.023,0.0225,0.0221,0.021,0.0175,0.012,0.011,0.0105,0.008,0.003,0.002,0.001,0.001};
    //w权数组,key关键字数组 
    HuffmanTree HT;
    HuffmanCode HC;
    HuffmanCreat(HT,HC,w,N-1);
    while(flag)
    {   

        printf("\n");
        printf("        **************************************"); 
        printf("\n          **1---------------显示编码**"); 
        printf("\n          **2---------------进行编码**"); 
        printf("\n          **3---------------进行译码**"); 
        printf("\n          **4---------------退出    **\n");  
        printf("        ****************************************"); 
        printf("\n"); 
        printf(" 请输入选择的编号:");  
        scanf("%c",&choice);
        switch(choice)
        {
            case '1':{
                printf("\n");
                for(i=1;i<N;i++)
                    printf("%c:%s\n",key[i],HC[i]);
                printf("\n按任意键返回...");
                getchar();
            };break;
            case '2':{
                printf("请输入要编译的字符(以#号结束):\n");
                HuffmanCoding(HC,key,N-1);
                printf("\n按任意键返回...");
                getchar();
            };break;
            case '3':{
                printf("请输入编码(以#号结束):");
                HuffmanDecoding(HC,key,N-1);
                printf("\n按任意键返回...");
                getchar();
            };break;
            case '4':flag=0;break;
            default:system("cls");
        }
    }
    return 0;
}

void Select(const HuffmanTree &HT,int n,int &s1,int &s2){
    int i;
    s1=s2=0;
    double min1=INT_MAX;//最小值 
    double min2=INT_MAX;//次小值 
    for(i=1;i<=n;i++)
    {
        if(HT[i].parent==0)
        {
            if(HT[i].weight<min1)
            {
                min2=min1;
                s2=s1;
                min1=HT[i].weight;
                s1=i;
            }
            else if((HT[i].weight>=min1)&&(HT[i].weight<min2))
            {
                min2=HT[i].weight;
                s2=i;
            }
            else{
                ;
            }
        }
    }
}//选择两个无父的权值最小的节点 其序号为s1,s2 

void HuffmanCreat(HuffmanTree &HT,HuffmanCode &HC,double *w,int n)//建立哈夫曼树 
{
    int s1,s2;
    int m=2*n-1;//n个叶子节点的哈夫曼树有2n-1个节点
    if(n<=1)return;
    int i,c,f;
    char *cd;
    HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
    for(i=1;i<=n;i++){
        HT[i].weight=w[i];
        HT[i].lchild=0;
        HT[i].rchild=0;
        HT[i].parent=0;
    }//初始化前n个节点
    for(i=n+1;i<=m;i++){
        HT[i].weight=0;
        HT[i].lchild=0;
        HT[i].rchild=0;
        HT[i].parent=0;
    }//初始化后n-1个节点 
    for(i=n+1;i<=m;i++){
        Select(HT,i-1,s1,s2);
        HT[s1].parent=i;
        HT[s2].parent=i;
        HT[i].lchild=s1;
        HT[i].rchild=s2;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
    }
    HC=(HuffmanCode)malloc((n+1)*sizeof(char *));//分配n个字符编码的头指针变量 
    cd=(char * )malloc(n*sizeof(char));//分配求编码的存储空间 
    cd[n-1]='\0';//编码结束符 
    for(i=1;i<=n;i++){
        int start=n-1;//编码结束符位置 
        for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent){
            if(HT[f].lchild==c) cd[--start]='0';
            else cd[--start]='1';
        }
        HC[i]=(char *)malloc((n-start)*sizeof(char));
        strcpy(HC[i],&cd[start]);//从cd复制编码到HC 
    }
    free(cd);
}
void HuffmanCoding(HuffmanCode &HC,char *key,int n)//编码  问题 不接收空格 
{
    char string[MAX];
    int i,j;
    scanf("%s",string);
    for(i=0;string[i]!='#';i++)
    {
        for(j=1;j<=n;j++)
        {
            if(string[i]==key[j])
            {
                printf("%s",HC[j]);
                break;
            }

        }

    }
    return;

}
void HuffmanDecoding(HuffmanCode &HC,char *key,int n)//译码   问题  空格符  v以后不再输出 
{
    char copy[MAX],code[MAX];//copy用来提取code中的字符串 
    int i,j,k;
    scanf("%s",code);
    for(i=2;code[0]!='#';i++)
    {
        strncpy(copy,code,i);
        for(j=1;j<=n;j++)
        {
            if(strcmp(copy,HC[j])==0)
            {
                printf("%c",key[j]);
                for(k=0;code[k-1]!='#';k++)
                {
                    code[k]=code[k+i];//删除已经比对过的字符串 
                    copy[k]=copy[k+i];//初始化copy数组 
                }
                i=1;
                break;
            }
        }
    }
    return;
 } 

(https://img-ask.csdn.net/upload/201606/14/1465910048_999009.png)说明](https://img-ask.csdn.net/upload/201606/14/1465910033_309322.png)

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐