标题:怎样把汉字打出来?
取消只看楼主
leon57
Rank: 1
来 自:xznu
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-7-19
 问题点数:0 回复次数:0 
怎样把汉字打出来?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define Maxsize 50

typedef struct
{
    char data;
    int weight,parent,lchild,rchild;
}HTNode,*HuffmanTree;

typedef char **HuffmanCode;



void HuffmanCoding(HuffmanTree &ht,HuffmanCode &hcode,int n)
{

    char d[]={" 的地得于个和在再是有"};

    int w[]={0,26,6,3,14,8,7,9,4,18,5};
    int m=2*n-1;
    int i,j,s1,s2,start,c,p;
    ht=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
    for(i=1;i<=n;i++)
    {    
        ht[i].data=d[i];
        ht[i].weight=w[i];
        ht[i].parent=0;
        ht[i].lchild=0;
        ht[i].rchild=0;
    }
    for(i=n+1;i<=m;i++)
    {
        ht[i].weight=0;
        ht[i].parent=0;
        ht[i].lchild=0;
        ht[i].rchild=0;  
    }
    for(i=n+1;i<=m;i++)
    {
        for(j=1;j<=i-1;j++)
        {
            if(ht[j].parent==0)
            {
                s1=j;
                break;
            }
        }
        for(;j<=i-1;j++)
        {
            if(ht[j].parent==0)
                if(ht[j].weight<ht[s1].weight)
                    s1=j;
        }
        
        for(j=1;j<=i-1;j++)
        {
            if(ht[j].parent==0&&ht[j].weight>ht[s1].weight)
            {
                s2=j;
                break;
            }
        }
        for(;j<=i-1;j++)
        {
            if(ht[j].parent==0)
                if(ht[j].weight>ht[s1].weight&&ht[j].weight<ht[s2].weight)
                    s2=j;
        }
        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;
    }


    char *cd;
    hcode=(HuffmanCode)malloc((n+1)*sizeof(char *));
    cd=(char *)malloc(n*sizeof(char));
    cd[n-1]='\0';

    for(i=1;i<=n;i++)
    {
        start=n-1;
        for(c=i,p=ht[i].parent;p!=0;c=p,p=ht[p].parent)
        {
            if(ht[p].lchild==c)
                cd[--start]='0';
            else
                cd[--start]='1';
        }

        
            hcode[i]=(char *)malloc((n-start)*sizeof(char));
            strcpy(hcode[i],&cd[start]);
        

    }    
        free(cd);



}

void disphuffman(HuffmanTree &ht,HuffmanCode &hcode,int n)
{
    int i;
    for(i=1;i<=n;i++)
    {
        printf("%c",ht[i].data);
        printf("\t");
        printf("哈夫曼编码:");
        puts(hcode[i]);
        printf("\n");
    }

}


void main()
{
    HuffmanTree ht;
    HuffmanCode hcode;
    int n=10;
    HuffmanCoding(ht,hcode,n);
    disphuffman(ht,hcode,n);
}
只有哈夫曼编码没有汉字
搜索更多相关主题的帖子: 汉字 
2008-11-20 20:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-245450-1-1.html




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

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