| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1013 人关注过本帖
标题:想做一个哈夫曼编码器,可是总是不对,求大神 帮助
取消只看楼主 加入收藏
Super_br
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-7-11
收藏
 问题点数:0 回复次数:0 
想做一个哈夫曼编码器,可是总是不对,求大神 帮助
#include<stdio.h>
#include<conio.h>
#include"iostream.h"
#include"stdlib.h"
typedef char datatype;
#define MAXVALUE 10000
#define MAXLEAF 300
#define MAXNODE MAXLEAF*2-1
#include<windows.h>
#define MAXBIT 50

static int count=0;
void log()
{
    system("color 05");
    printf("                              ************************************************************************\n");
    printf("\t\t\t\t\t\t\t");
    printf("欢");
    Sleep(100);
        printf("迎");
    Sleep(100);
        printf("进");
    Sleep(100);
        printf("入");
    Sleep(100);
        printf("哈");
    Sleep(100);
        printf("夫");
    Sleep(100);
        printf("曼");
    Sleep(100);
        printf("编");
    Sleep(100);
        printf("码");
    Sleep(100);
        printf("系");
    Sleep(100);
        printf("统");
    Sleep(100);
    //    printf("~");
    //Sleep(100);
    printf("\n");
    printf("                              ************************************************************************\n");
   
}

void UserInterface()
{
    printf("\n");
    printf("\n");
    printf("\n");
    printf("                              ************************************************************************\n");
    printf("                              ##   请输入以下数字进行功能操作:                                     ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                              1,记忆输入字符                        ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                              2,编码                               ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                              3,译码                            ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ##                                                                    ##\n");
    printf("                              ************************************************************************\n");
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
 
//哈夫曼树的数据结构
typedef struct node
{   
    char letter;
    int weight;
    int parent;
    int lchild;
    int rchild;
    datatype data;
}HNodeType;

typedef struct
{
    char letter;
    int bit[MAXBIT];
    int start;
}HCodeType;

typedef struct
{
    char s;
    int num;
    int count;
}Bowen;
Bowen data[MAXVALUE];

Bowen* HuffmanTree(HNodeType HuffNode[MAXVALUE],int n,Bowen a[])
{
    //if(n<1)return;
    //printf("%d",n);
    int i,j,m1,m2,x1,x2,temp1;
    char temp2;
    for(i=0;i<2*n-1;i++)
    {
        HuffNode[i].letter=NULL;
        HuffNode[i].weight=0;
        HuffNode[i].parent=-1;
        HuffNode[i].lchild=-1;
        HuffNode[i].rchild=-1;
    }
    for(i=0;i<n-1;i++)
        for(j=i+1;j<n-1;j++)
            if(a[j].num>a[i].num)
            {
                temp1=a[i].num ;
                a[i].num=a[j].num;
                a[j].num=temp1;
                temp2=a[i].s;
                a[i].s=a[j].s;
                a[j].s=temp2;
            }
            for(i=0;i<n;i++)
            {
                HuffNode[i].weight=a[i].num;
                HuffNode[i].letter=a[i].s;
            }
            for(i=0;i<n-1;i++)    //构造哈夫曼树
            {
                m1=m2=MAXVALUE;
                x1=x2=0;
                for(j=0;j<n+i;j++) //找出两颗权值最小的子树
                {
                    if(HuffNode[j].parent==-1&&HuffNode[j].weight<m1)
                    {
                        m2=m1;
                        x2=x1;
                        m1=HuffNode[j].weight;
                        x1=j;
                    }
                    else if(HuffNode[j].parent==-1&&HuffNode[j].weight<m2)
                    {
                        m2=HuffNode[j].weight;
                        x2=j;
                    }
                }

                /*将找出的两颗子树合并为一颗*/
                HuffNode[x1].parent=n+1;HuffNode[x2].parent=n+1;
                HuffNode[n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;
                HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;

                printf("建立哈夫曼树成功\n");
                return data;
}
}

/*生成哈夫曼编码*/
void  HuffmanCode(HNodeType HuffNode[MAXVALUE],int n,Bowen a[])
{
      
    HCodeType HuffCode[MAXLEAF],cd;
    int i,j,c,p;
    HuffmanTree(HuffNode,n,a);
    for(i=0;i<n;i++)        //求每个叶子的哈夫曼编码
    {
        cd.start=n-1;
        c=i;
        p=HuffNode[c].parent;
        while(p!=-1)
        {
            if(HuffNode[p].lchild==c)
                cd.bit[cd.start]=0;
            else
                cd.bit[cd.start]=1;
            cd.start--;
            c=p;
            p=HuffNode[c].parent;
        }
     for(j=cd.start+1;j<n;j++)//保存求出的每个叶节点的哈夫曼和编码的起始位
         HuffCode[i].bit[j];
     HuffCode[i].start=cd.start;
    }
    printf("输出每个叶子的哈夫曼编码:\n");
    for(i=0;i<n;i++)
    {
        HuffCode[i].letter=HuffNode[i].letter;
        printf("%c:",HuffCode[i].letter);
        for(j=HuffCode[i].start+1;j<n;j++)
        printf("%d",HuffCode[i].bit[j]);
        printf("\n");
    }
  }
/*生成哈夫曼译码*/
void HuffmanTranscode(HNodeType HuffNode[MAXVALUE],int n)
{
    //HNodeType HuffCode[MAXLEAF];
    char code[300],*m;
    int i,c;
    //char code[30],*m;
    printf("请输入电文位数:\n");
    scanf("%d",&n);
    printf(" 请输入电文(1/0):\n");
    for(i=0;i<30;i++)
        code[i]=NULL;
    scanf(" %s",&code);
    m=code;
    c=2*n-2;
    printf("输出的哈夫曼驿马:\n");
    while(*m!=NULL)
    {
        if(*m=='0')
        {
            c=i=HuffNode[c].lchild;
            if(HuffNode[c].lchild==-1&&HuffNode[c].rchild==-1)
            {
                printf("%c",HuffNode[i].letter);
                c=2*n-2;
            }
        }
        if(*m=='1')
        {
            c=i=HuffNode[i].rchild;
                    if(HuffNode[c].lchild==-1&&HuffNode[c].rchild==-1)
            {
                printf("%c",HuffNode[i].letter);
                c=2*n-2;
            }
            m++;
        }
        printf("\n");
    }
}


Bowen* HuffmanMemory()
{
    char s[100],*p;
    int i;
    printf("\n 请输入一些字符:");
    scanf("%s",&s);
    for(i=0;i<30;i++)
    {
        data[i].s =NULL;
        data[i].num=0;
    }
    p=s;
    while(*p)
    {
        for(i=0;i<=count+1;i++)
        {
            if(data[i].s ==NULL)
            {
                data[i].s =*p;
                data[i].num++;
                count++;
                break;
            }
            else if(data[i].s ==*p)
            {
                data[i].num++;
                break;
            }
        }
        p++;
   
    }
    printf("\n 不同的字符数字:%d\n",count);
            for(i=0;i<count;i++)
            {
                printf(" %c",data[i].s );
                printf(" 权值:%d",data[i].num);
                printf("\n");
     
            }            
            
            printf("记忆成功!");
            return data;
}

void main()
{
    HNodeType HuffNode[MAXVALUE];
    log();
    UserInterface();
    int z,i;      
    printf("请选择所要进行的功能:");
    while(1)
    {
    scanf("%d",&z);
    switch (z)
    {
    case 1:
        {
      
            printf("你现在进行的功能是编码:\n");
            HuffmanMemory();
    /*for(i=0;i<30;i++)
        printf("%c ",data[i].s);  */  
    //printf("%d",count);
    //count=*f;
    break;
        }
    case 2:
        {
            fflush(stdin);
            HuffmanCode(HuffNode,count,data);
            
        break;
        }
    case 3:
        {
         HuffmanTranscode(HuffNode,count);
         //getch();
        break;
        }
    }

}
}



        
   

            

                    
搜索更多相关主题的帖子: 编码器 system include color count 
2016-07-11 13:48
快速回复:想做一个哈夫曼编码器,可是总是不对,求大神 帮助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.120731 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved