#include <stdio.h>
#include <stdlib.h>
#define MAXSYMBS 68 //字符种数or叶子数+1
#define MAXNODE 133 //结点数:算出来m=67,父结点数为2*67-1=133仅对此文档有效,2*maxsymbs-1
void rate(); //求字符在文章中出现的频率
void sort(); //排序
void huffmantree();//构造哈夫曼树
void table(); //建立码表
void huffmancode(); //哈夫曼编码
void code_txt(); //对文档编码
void decode_txt(); //解码
int n[127] = {0}; //字符出现的次数
double symbrate[127] = {0}; //字符出现的频率
int num = 0; //字符总数
int m = 0; //字符种数or叶子数
int code_num = 0; //编码总数
struct huffnode
{
int weight; //权重
int flag; //标志
int parent; //父结点
int lchild; //左孩子
int rchild; //右孩子
} huff_node[MAXNODE];
struct huffcode
{
int bits[MAXSYMBS];
int start;
} huff_code[MAXSYMBS], cd;
struct tab //码表结构
{
char ch;
int ch_num;
} tab[MAXSYMBS];
int main()
{
rate();
sort();
huffmantree();
huffmancode();
table();
code_txt();
decode_txt();
getchar();
return 0;
}
void rate()
{
FILE *fp, *out;
char c;
int i;
if((fp=fopen("Project_huffman.txt","r"))==NULL)
{
printf("Project_huffman.txt文件打开失败..\n");
return ;
}
while(!feof(fp))
{
c = fgetc(fp);
for(i=0; i<=127; i++)
{
if(c == i)
{
num++;
n[i]++;
c语言 哈夫曼编码
最新推荐文章于 2023-08-27 22:12:56 发布