#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <conio.h>
#define N 128
#define M 2*N-1
typedef struct
{/*声明结构体类型*/
char Symbol; /* 存放文本文件中出现的字符*/
int Freq; /* 权值即字符出现的频度*/
}SYMBOL;
typedef struct
{
int weight;
int parent;
int LChild;
int RChild;
int symbol;
}HTNode,HuffmanTree[M+1]; /*声明结构体类型和结构体数组类型*/
typedef char *HuffmanCode[N+1];/*声明字符数组类型*/
HuffmanTree ht;
int T;/*定义全局变量*/
SYMBOL* GetWeight(int *n)
{/*定义GetWeight()函数为取得叶子结点权值函数*/
SYMBOL *p;
FILE *fp; /*定义fp是文件型指针变量*/
int i=0,x=-1,j = 1,k=0;
char ch;
float pl[1000];
int str[128],count[1000],l[128],s[128] = {0};
char filename[50];
printf("Enter the file name to get the Huffman code:");
scanf("%s",filename);
if((fp=fopen(filename,"rb"))==NULL)
{/*fopen()函数为C库函数,用以打开文件,使用文件方式为“只读”*/
printf("First open SOURCE file error!\n");
getch();
exit(0);
}
while(!(feof(fp)))
{/*feof()函数为C库函数,用来检查文件是否结束*/
ch = fgetc(fp); /*fgetc()为C库函数用来从文件中读入一个字符*/
x++; /*x用以统计所有字符总数*/
s[ch]++; l[i++]=ch;/*l[]数组存放文本文件所有字符*/
}
for(i = 0; i < 128; i++)
{
if(s[i] != 0)
(*n)++; /* 统计出现的字符种数得到n */
}
p = (SYMBOL *)malloc(sizeof(SYMBOL) * ((*n)+1) );/* 为p分配内存空间 */
for(i = 0; i < 128; i++)
{
if(s[i]!=0)
{
p[j].Symbol = i;/*字符按ASCII代码值大小顺序存入数组*/
p[j++].Freq = s[i];
} /*存储叶子结点的字符和权值*/
}