当前位置:文档之家› 完整word版数据结构哈夫曼编码源代码

完整word版数据结构哈夫曼编码源代码

#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#define N 1000char kind[N]={'\0'};int num[N]={0};int count=0;int s1,s2;typedef struct{int weight; /*字符权重*/int parent,lchild,rchild;}HTNode,*HuffmanTree; /*定义哈夫曼树结点*/ typedef char **HuffmanCode;struct charcode{char data;char code[20];}Code[N],d[N],e[N];void Creat_message() /*输入字符并存入文件*/{FILE *fp;char c;晩?灦昽灯湥尨文本文件.txt,wb))==NULL){printf(cannot open this file!\n);return ;}牰湩晴尨请输入原代码,以'#'表示结束\n);do{scanf(%c,&c);fputc(c,fp);}while (c!='#');fclose(fp);}void Calculate() /*计算文件中字符的权值*/ {char c;int i=1,k;FILE *fp;晩?灦昽灯湥尨文本文件.txt,b))==NULL){printf(cannot open this file\n);return ;}c=fgetc(fp);while(c!='#'){for (k=0;k<i;k++)if(c==kind[k]){num[k]++;break;}if(k>=i&&kind[k-1]!=c){kind[i]=c;num[i]++;i++;}c=fgetc(fp);}count=i-1;}void select(HuffmanTree Hu,int j) /*寻找输入的字符数据中权值最小的两个结点*/ {int k=1,t;while(Hu[k].parent!=0) k++;for(t=k;t<=j;t++)if(Hu[t].parent==0 && Hu[t].weight<Hu[k].weight)k=t;s1=k;k=1;while(Hu[k].parent!=0 ||k==s1) k++;for(t=k;t<=j;t++)if(Hu[t].weight<Hu[k].weight && Hu[t].parent==0 &&t!=s1)k=t;s2=k;}void HuffmanCoding () /*构建哈夫曼树并编码*/{void select(HuffmanTree Hu,int j);HuffmanTree HT;HuffmanCode HC;int m,i,start,f,c;char *cd;FILE *fp;if (count<=1) return;m=2*count-1;HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));for(i=1;i<=count;i++)HT[i].weight=num[i];for(i=1;i<=m;i++){HT[i].parent=0;HT[i].lchild=0;HT[i].rchild=0;}for(i=count+1;i<=m;i++){select(HT,i-1);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;}HC=(HuffmanCode)malloc((count+1)*sizeof(char*));cd=(char*)malloc(count*sizeof(char));cd[count-1]='\0';for(i=1;i<=count;i++){start=count-1;for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)if (HT[f].lchild==c) cd[--start]='0';else cd[--start]='1';HC[i]=(char*)malloc((count-start)*sizeof(char)); strcpy(HC[i],&cd[start]);}晩?灦昽灯湥尨字符代码.txt,wb))==NULL){printf(cannot open this file\n);return ;}for(i=1;i<=count;i++){Code[i].data= kind[i];strcpy(Code[i].code,HC[i]);fwrite(&Code[i],sizeof(struct charcode),1,fp);}fclose(fp);}void Transform() /*将字符文件翻译成代码文件*/ {FILE *fp1,*fp2,*fp3;int i;char c;晩?灦?潦数?字符代码.txt,b))==NULL){printf(cannot open this file\n);return ;}晩?灦?潦数?文本文件.txt,b))==NULL){printf(cannot open this file\n);return ;}晩?灦?潦数?码本文件.txt,wb))==NULL){ printf(cannot open this file\n);return ;}for(i=0;i<count;i++)fread(&d[i],sizeof(struct charcode),1,fp1);while(!feof(fp2)){c=fgetc(fp2);for(i=0;i<count;i++)if(d[i].data==c){fputs(d[i].code,fp3);break;}}fputc('#',fp3);fclose(fp3);}void Translate() /*将代码文件翻译成字符文件*/ {FILE *fp1,*fp2,*fp3;int i,j,k;char c[20]={'\0'};晩?灦?潦数?码本文件.txt,b))==NULL){printf(cannot open this file\n);return ;}晩?灦?潦数?字符代码.txt,b))==NULL){printf(cannot open this file\n);return ;}晩?灦?潦数?破译文件.txt,wb))==NULL){printf(cannot open this file\n);return ;}for(i=1;i<=count;i++)fread(&d[i],sizeof(struct charcode),1,fp2);i=0;c[i]=fgetc(fp1);while(c[i]!='#'){for(k=1;k<=count;k++)if(!strcmp(c,d[k].code)){fputc(d[k].data,fp3);for(j=0;j<=i;j++) c[j]='\0';i=-1;break;}i++;c[i]=fgetc(fp1);}fputc('#',fp3);fclose(fp1);fclose(fp2);fclose(fp3);}void print1(char *name) /*打印文件函数*/ {FILE *fp;char c;if((fp=fopen(name,b))==NULL){printf(cannot open this file\n);return ;}c=fgetc(fp);while(c!='#'){printf(%c,c);c=fgetc(fp);}}void print2(char *name) /*打印文件函数*/ {FILE *fp;int i=1;if((fp=fopen(name,b))==NULL){printf(cannot open this file\n);return ;}while(!feof(fp)){fread(&e[i],sizeof(struct charcode),1,fp);printf(%c--%s\n,e[i].data,e[i].code);i++;}}void main(){Creat_message();牰湩晴尨您输入的文本文件是:\n);牰湩?尨文本文件.txt);printf(\);Calculate();HuffmanCoding ();牰湩晴尨每个字符相应的哈夫曼编码是:\n);牰湩?尨字符代码.txt);printf(\);Transform();牰湩晴尨原文件的码本文件(即全为字符编码)为:\n);牰湩?尨码本文件.txt);printf(\);Translate();牰湩晴尨用码本文件和字符代码求出的破译文件为:\n); 牰湩?尨破译文件.txt);printf(\);}。

相关主题