简单实现哈夫曼编码译码过程,简单易懂`
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void weight(HuffmanTree &HT,char filename[])
{
FILE *fp;
char ch;
int i;
fp = fopen(filename,"r");
if((fp =fopen(filename,"r"))==NULL){
printf("can't open file\n");
getchar();
exit(0);
}
while(!feof(fp)){
ch = fgetc(fp);
i = ch-'a'+1;
if(i>=1&&i<=26){
HT[i].weight++;
}
else if(i==-64){
HT[27].weight++;
}
}
fclose(fp);
}
void select(HuffmanTree HT,int num,int &s1,int &s2)
{
int i;
s1 = s2 = 0;
HT[0].weight = 100000;
for(i=1;i<=num;i++){
if(HT[i].weight<HT[s1].weight&&HT[i].parent==0){
s1 = i;
}
}
for(i=1;i<=num;i++){
if(HT[i].weight<HT[s2].weight&&HT[i].parent==0&&i!=s1){
s2 = i;
}
}
}
void createHuffman(HuffmanTree &HT,char filename[])
{
int m,i,s1,s2;
int n = 27;
m = 2*n - 1;
HT = new HTNode[m+1];
for(i=1;i<=n;i++){
HT[i].ch = 'a'+i-1;
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].weight = 0;
}
for(i=n+1;i<=m;i++){
HT[i].ch = '#';
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].weight = 0;
}
HT[27].ch = ' ';
weight(HT,filename);
for(i=n+1;i<=m;i++){
select(HT,i-1,s1,s2);
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;
}
}
void createHuffmanCode(HuffmanTree HT,HuffmanCode &HC)
{
int i,c,f,start,n;
n = 27;
char *cd;
HC = new char*[n+1];
cd = new char[n];
cd[n-1] = '\0';
for(i=1;i<=n;i++){
start = n-1;
c = i;
f = HT[i].parent;
while(f!=0){
--start;
if(HT[f].lchild==c)
cd[start] = '0';
else
cd[start] = '1';
c = f;
f = HT[f].parent;
}
HC[i] = new char[n-start];
strcpy(HC[i],&cd[start]);
}
delete cd;
}
void code(HuffmanCode HC,char filename[])
{
FILE *fp1,*fp2;
int i;
char ch;
fp1 = fopen(filename,"r");
if((fp1 =fopen(filename,"r"))==NULL){
printf("can't open file\n");
getchar();
exit(0);
}
fp2 = fopen("code.txt","w");
if((fp2 =fopen("code.txt","w"))==NULL){
printf("can't open file\n");
getchar();
exit(0);
}
while(!feof(fp1)){
ch = fgetc(fp1);
i = ch-'a'+1;
if(i>=1&&i<=26){
fputs(HC[i],fp2);
}
else if(i==-64){
fputs(HC[27],fp2);
}
}
fclose(fp1);
fclose(fp2);
}
void print(HuffmanTree HT,HuffmanCode HC)
{
printf("字符 出现次数 编码\n");
for(int i=1;i<=27;i++)
{
printf("%c %d %s\n",HT[i].ch,HT[i].weight,HC[i]);
}
}
void decode(HuffmanTree HT,char filename[])
{
FILE *fp;
fp = fopen(filename,"r");
char ch;
int n = 27;
int m = 2*n-1;
int f;
while(!feof(fp)){
f = m;
while(HT[f].ch=='#'&&!feof(fp)){
ch = fgetc(fp);
if(ch=='0'){
f = HT[f].lchild;
}
else{
f = HT[f].rchild;
}
}
if(ch!=EOF){
printf("%c",HT[f].ch);
}
}
fclose(fp);
}
void decodefile(HuffmanTree HT,char filename[])
{
FILE *fp1,*fp2;
fp1 = fopen(filename,"r");
fp2 = fopen("decode.txt","w");
char ch;
int n = 27;
int m = 2*n-1;
int f;
while(!feof(fp1)){
f = m;
while(HT[f].ch=='#'&&!feof(fp1)){
ch = fgetc(fp1);
if(ch=='0'){
f = HT[f].lchild;
}
else{
f = HT[f].rchild;
}
}
if(ch!=EOF){
fputc(HT[f].ch,fp2);
}
}
fclose(fp1);
fclose(fp2);
}
void huffmancodefile(HuffmanTree HT,HuffmanCode HC)
{
FILE *fp;
int i;
int n = 27;
fp = fopen("huffmancodetree.txt","w");
if((fp =fopen("huffmancodetree.txt","w"))==NULL){
printf("can't open file\n");
getchar();
exit(0);
}
fprintf(fp,"字符 编码\n");
for(i=1;i<=n;i++){
fprintf(fp,"%c %s\n",HT[i].ch,HC[i]);
fflush(fp);
}
fclose(fp);
printf("哈夫曼编码文件已写入\n");
}
void huffmanfile(HuffmanTree HT)
{
FILE *fp;
int i;
int n = 27;
int m = 2*n-1;
fp = fopen("huffmantree.txt","w");
if((fp =fopen("huffmantree.txt","w"))==NULL){
printf("can't open file\n");
getchar();
exit(0);
}
fprintf(fp,"字符 权值 左孩子 右孩子 双亲\n");
for(i=1;i<=m;i++){
fprintf(fp,"%c %d %d %d %d\n",HT[i].ch,HT[i].weight,HT[i].lchild,HT[i].rchild,HT[i].parent);
fflush(fp);
}
fclose(fp);
printf("哈夫曼树文件已写入\n");
}
int main()
{
int choose;
char filename[30];
char filename1[30];
HuffmanTree HT;
HuffmanCode HC;
while(1){
printf("系统菜单\n");
printf("1、生成哈夫曼编码\n");
printf("2、查看编码\n");
printf("3、生成哈夫曼树文件\n");
printf("4、生成哈夫曼编码文件\n");
printf("5、生成编码文件\n");
printf("6、生成译码文件\n");
printf("7、查看译码内容\n");
printf("8、退出\n");
printf("\n");
printf("请选择功能:");
scanf("%d",&choose);
getchar();
switch(choose){
case 1:
printf("请输入需编码文件的位置:");
gets(filename);
createHuffman(HT,filename);
createHuffmanCode(HT,HC);
printf("完成编码\n");
break;
case 2:
print(HT,HC);
break;
case 3:
huffmanfile(HT);
break;
case 4:
huffmancodefile(HT,HC);
break;
case 5:
code(HC,filename);
printf("编码文件已生成\n");
break;
case 6:
printf("请输入编码文件\n");
gets(filename1);
decodefile(HT,filename1);
printf("译码文件已生成\n");
break;
case 7:
printf("请输入编码文件\n");
gets(filename1);
decode(HT,filename1);
printf("\n");
break;
case 8:
exit(0);
default:
printf("请输入正确的序号\n");
}
system("pause");
system("cls");
}
return 0;
}
|