怎样把汉字打出来?
#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);
}
只有哈夫曼编码没有汉字