#include<stdio.h>
#include<stdlib.h>
#include "Haffman.h"
#define Maxvalue 10000
#define Maxbit 10
#define MaxN 100
void main()
{
int u[80];
int j,n=4,m,x=1;
printf("请输入一段字符:");
int i,b[129]={0};
char a[100];
gets(a);
for(i=0;a[i];i++)
b[a[i]]++;
for(i=0;i<129;i++)
if(b[i]!=0)
{
printf("%c出现的次数为:%d \n",i,b[i]);
x++;
}
n=x-1;
printf("输入的字符个数为:%d\n",n);
Haffnode *myhafftree=(Haffnode *)malloc(sizeof(Haffnode)*(2*n+1));
Code *myhaffcode=(Code *)malloc(sizeof(Code)*n);
if(n>MaxN)
{
printf("给出的n越界,修改MaxN!\n");
exit(1);
}
printf("出现的权值数组为:");
int k=0;
for(i=0;a<129;i++)
if(b[i]!=0)
{
printf("%d ",b[i]);
u[k]=b[i];
k++;
}
printf("\n");
Haffman(u,n,myhafftree);
Haffmancode(myhafftree,n,myhaffcode);
//PrintBiTree(myhafftree,n);
//printf("哈夫曼树为:\n");
//printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");printf("\n");
printf("哈夫曼编码输出为:\n");
//输出每个叶节点的哈夫曼编码
for(m=0;m<n;m++)
{
printf(" Weight=%d Code;=",myhaffcode[m].weight);
for(j=myhaffcode[m].start+1;j<n;j++)
printf("%d",myhaffcode[m].bit[j]);
printf("\n");
}
/*Haffnode *mhafftree=(Haffnode *)malloc(sizeof(Haffnode)*(2*n+1));
Code *mhaffcode=(Code *)malloc(sizeof(Code)*n);
int w[]={1,3,5,7};
Haffman(w,n,mhafftree);
Haffmancode(mhafftree,n,mhaffcode);
for(m=0;m<n;m++)
{
printf("Weight=%d Code=",mhaffcode[m].weight);
for(j=mhaffcode[m].start+1;j<n;j++)
printf("%d",mhaffcode[m].bit[j]);
printf("\n");
}*/
}
评论0