⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 关于huffman编码的程序2.txt.txt

📁 哈夫曼编码程序
💻 TXT
字号:
这是我刚编好的huffman编码程序,其中有一中问题,求改!!!

#include <malloc.h>
#include <stdio.h>
#define max 65535

typedef struct
{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HuffmanTreeNode,*HuffmanTree;

typedef struct
{
    char *ch;
    int *weight;
    int count;
}WeightAndInfo;

typedef char * *HuffmanCode;
void CreateHuffmanTree_Code(HuffmanTree *HT,HuffmanCode *HC,int *w,int n);
void Select(HuffmanTree HT,int i,int *s1,int *s2);

void CreateHuffmanTree_Code(HuffmanTree *HT,HuffmanCode *HC,int *w,int n)
{
    int m,s1,s2,i,c,start,f;
    char *cd;
    HuffmanTree p;
    if(n<1) return;
    (*HT)=(HuffmanTree)malloc((m+1)*sizeof(HuffmanTreeNode));
    for(p=*HT,i=1;i<=n;i++)
    {    p[i].weight=w[i-1];
        p[i].parent=0;
        p[i].lchild=0;
        p[i].rchild=0;
    }
    for(;i<=m;i++)
    {
        p[i].weight=0;
        p[i].parent=0;
        p[i].lchild=0;
        p[i].rchild=0;
    }
    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)->weight=(*HT)[s1].weight+(*HT)[s2].weight;
    }
    cd=(char *)malloc(n * sizeof(char));
    cd[n-1]='0';
    for(i=1;i<=n;i++)
    {
        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]);
    }
    free(cd);
}

void Select(HuffmanTree HT,int i,int *s1,int *s2)
{
    int j;
    int min;
    min=max;
    for(j=1;j<=i;j++)
    {
        if(HT[j].parent==0 && HT[j].weight<=min)
        {
            *s1=j;
            min=HT[j].weight;
        }
    }
    HT[*s1].parent=*s1;
    min=max;
    for(j=1;j<=i;j++)
    {
        if(HT[j].parent==0 && HT[j].weight<=min)
        {
            *s2=j;
             min=HT[j].weight;
        }
    }
    HT[*s2].parent=*s2;
}

main()
{
    HuffmanCode chCode;
    int i;
    HuffmanTree HufTree=NULL;
    WeightAndInfo w;
    printf("input num of count:\n");
    scanf("%d\n",&w.count);
    chCode=(HuffmanCode )malloc((w.count+1)* sizeof(char));
    w.weight=(int *)malloc(w.count * sizeof(int));
    w.ch=(char *)malloc(w.count * sizeof(char));
    for(i=0;i<w.count;i++)
        scanf("&d,%c",&(w.weight[i]),&(w.ch[i]));
    CreateHuffmanTree_Code(&HufTree,&chCode,w.weight,w.count);
    for(i=1;i<=w.count;i++)
        printf("待编码的字符ch为%c,对应的权重weight为%d,相应的哈夫曼编码code为%s\n",w.ch[i-1],w.weight[i-1],chCode[i]);
    getch();
    for(i=1;i<=w.count;i++)
        if(chCode[i])
            free(chCode[i]);
    free(chCode);
}





我调试过,一个错误都没有,不过就是运行时,只执行到我输入count这一步,下面就不行啦,求高手帮我修改一下啦!!!
在此先谢谢啦!!!  

 
 
作者:boxertony      发表时间:2005-10-26 17:17:00  

 第1楼  

#include <malloc.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#define max 65535

typedef struct
{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HuffmanTreeNode,*HuffmanTree;

typedef struct
{
    char *ch;
    int *weight;
    int count;
}WeightAndInfo;

typedef char * *HuffmanCode;
void CreateHuffmanTree_Code(HuffmanTree *HT,HuffmanCode *HC,int *w,int n);
void Select(HuffmanTree HT,int i,int *s1,int *s2);

void CreateHuffmanTree_Code(HuffmanTree *HT,HuffmanCode *HC,int *w,int n)
{
    int m,s1,s2,i,c,start,f;
    char *cd;
    HuffmanTree p;

    m = n*2;    // m不赋值就使用吗?

    if(n<1) return;
    (*HT)=(HuffmanTree)malloc((m+1)*sizeof(HuffmanTreeNode));
    for(p=*HT,i=1;i<=n;i++)
    {    p[i].weight=w[i-1];
        p[i].parent=0;
        p[i].lchild=0;
        p[i].rchild=0;
    }
    for(;i<=m;i++)
    {
        p[i].weight=0;
        p[i].parent=0;
        p[i].lchild=0;
        p[i].rchild=0;
    }
    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)->weight=(*HT)[s1].weight+(*HT)[s2].weight;
    }
    cd=(char *)malloc(n * sizeof(char));
    cd[n-1]='0';
    // 下面的循环给cd[]元素赋值时超出了下标范围,你自己检查一下吧,懒得看了,呵呵。
    for(i=1;i<=n;i++) // 
    {
        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]);
    }
    free(cd);
}

void Select(HuffmanTree HT,int i,int *s1,int *s2)
{
    int j;
    int min;
    min=max;
    for(j=1;j<=i;j++)
    {
        if(HT[j].parent==0 && HT[j].weight<=min)
        {
            *s1=j;
            min=HT[j].weight;
        }
    }
    HT[*s1].parent=*s1;
    min=max;
    for(j=1;j<=i;j++)
    {
        if(HT[j].parent==0 && HT[j].weight<=min)
        {
            *s2=j;
             min=HT[j].weight;
        }
    }
    HT[*s2].parent=*s2;
}

int main()
{
    HuffmanCode chCode;
    int i;


    HuffmanTree HufTree=NULL;
    WeightAndInfo w;
    printf("input num of count:\n");
    scanf("%d",&w.count);    // 为什么要在%d后面加个\n呢?

    chCode=(HuffmanCode )malloc((w.count+1)* sizeof(char));
    w.weight=(int *)malloc(w.count * sizeof(int));
    w.ch=(char *)malloc(w.count * sizeof(char));
    for(i=0;i<w.count;i++)
        scanf("%d,%c",&(w.weight[i]),&(w.ch[i]));

    CreateHuffmanTree_Code(&HufTree,&chCode,w.weight,w.count);
    for(i=1;i<=w.count;i++)
        printf("待编码的字符ch为%c,对应的权重weight为%d,相应的哈夫曼编码code为%s\n",w.ch[i-1],w.weight[i-1],chCode[i]);
    getch();
    for(i=1;i<=w.count;i++)
        if(chCode[i])
            free(chCode[i]);
    free(chCode);

    return 0;
}  
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -