设计一个利用哈夫曼算法的编码系统,重复地显示并处理以下项目,直到选择退出为止。【基本要求】1)将权值数据存放在数据文件(文件名为data.txt,位于执行程序的当前目录中)2)初始化:键盘输入字符集大小n、n个字符和n个权值,建立哈夫曼树;3)编码:利用建好的哈夫曼树生成哈夫曼编码;4)输出(1)各个字符的编码;(2)1111111;;;;;;#includestdio.h#includestring.h#includemalloc.h#defineM50#defineMAX1000;typedefstruct{intweight;//结点权值intparent,lchild,rchild;}HTNODE,*HUFFMANTREE;typedefchar**HUFFMANCODE;//动态分配数组存储哈夫曼编码表HUFFMANTREEhuffmantree(intn,intweight[])//构建哈夫曼树{intm1,m2,k;inti,j,x1,x2;HUFFMANTREEht;ht=(HUFFMANTREE)malloc((2*n)*sizeof(HTNODE));for(i=1;i(2*n);i++)//初始化哈夫曼树中各结点的数据,没初始值的赋值为0{ht[i].parent=ht[i].lchild=ht[i].rchild=0;if(i=n)ht[i].weight=weight[i];elseht[i].weight=0;}for(i=1;in;i++)//每一重循环从森林中选择最小的两棵树组建成一颗新树{m1=m2=MAX;x1=x2=0;for(j=1;j(n+i);j++){if((ht[j].weightm1)&&(ht[j].parent==0)){m2=m1;x2=x1;m1=ht[j].weight;x1=j;}elseif((ht[j].weightm2)&&(ht[j].parent==0)){m2=ht[j].weight;x2=j;}}k=n+i;ht[x1].parent=ht[x2].parent=k;ht[k].weight=m1+m2;ht[k].lchild=x1;ht[k].rchild=x2;}returnht;}voidhuffmancoding(constintn,HUFFMANCODEhc,HUFFMANTREEht,charstr[]){inti,start,child,father;char*cd;hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间cd[n-1]='\0';//编码结束符for(i=1;i=n;++i)//逐个字符求哈夫曼编码{start=n-1;for(child=i,father=ht[i].parent;father!=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/if(ht[father].lchild==child)cd[--start]='0';elsecd[--start]='1';hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc}free(cd);//释放工作空间for(i=1;i=n;++i){printf(%c的编码:,str[i]);printf(%s\n,hc[i]);}}voidmain(){inti,j,k;charstr[50];intweight[50];printf(请输入字符(一次性连续输入所求的字符):);/*如:abcjhjg不要输成abcjhig,即字符间不加空格*/gets(str);for(j=0;j50;j++){if(str[j]=='\0')break;}constintn=j;for(j=n;j0;j--)str[j]=str[j-1];str[n+1]='\0';for(k=0;kn;k++){printf(请输入%c的权值:,str[k+1]);scanf(%d,&weight[k]);}for(k=n;k0;k--)weight[k]=weight[k-1];weight[0]=0;HUFFMANCODEhc=NULL;HUFFMANTREEht;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);}补充:噢,我忘了你需要存档。现在太晚,明天再改一下。补充:这个是带有存盘的程序。#includestdio.h#includestring.h#includemalloc.h#defineM50#defineMAX1000;typedefintElempe;typedefstruct{intweight;//结点权值intparent,lchild,rchild;}HTNODE,*HUFFMANTREE;typedefchar**HUFFMANCODE;//动态分配数组存储哈夫曼编码表HUFFMANTREEhuffmantree(intn,intweight[])//构建哈夫曼树{intm1,m2,k;inti,j,x1,x2;HUFFMANTREEht;ht=(HUFFMANTREE)malloc((2*n)*sizeof(HTNODE));for(i=1;i(2*n);i++)//初始化哈夫曼树中各结点的数据,没初始值的赋值为0{ht[i].parent=ht[i].lchild=ht[i].rchild=0;if(i=n)ht[i].weight=weight[i];elseht[i].weight=0;}for(i=1;in;i++)//每一重循环从森林中选择最小的两棵树组建成一颗新树{m1=m2=MAX;x1=x2=0;for(j=1;j(n+i);j++){if((ht[j].weightm1)&&(ht[j].parent==0)){m2=m1;x2=x1;m1=ht[j].weight;x1=j;}elseif((ht[j].weightm2)&&(ht[j].parent==0)){m2=ht[j].weight;x2=j;}}k=n+i;ht[x1].parent=ht[x2].parent=k;ht[k].weight=m1+m2;ht[k].lchild=x1;ht[k].rchild=x2;}returnht;}voidhuffmancoding(constintn,HUFFMANCODEhc,HUFFMANTREEht,charstr[]){inti,start,child,father;char*cd;hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间cd[n-1]='\0';//编码结束符for(i=1;i=n;++i)//逐个字符求哈夫曼编码{start=n-1;for(child=i,father=ht[i].parent;father!=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/if(ht[father].lchild==child)cd[--start]='0';elsecd[--start]='1';hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc}free(cd);//释放工作空间for(i=1;i=n;++i){printf(%c的编码:,str[i]);printf(%s\n,hc[i]);}}voidSave(inta[],constint&n){FILE*fp;inti,count=0,flag=1;fp=fopen(data.txt,wb+);for(i=1;i=n;i++){if(fwrite(&a[i],sizeof(1),1,fp)==1){count++;}else{flag=0;printf(数据保存失败!\n);break;}}if(flag)printf(\n数据保存成功!(有%d个权值已保存)\n,count);fclose(fp);}voidmain(){FILEfp;inti,j,k;charstr[50];intweight[50];printf(请输入字符(一次性连续输入所求的字符):);/*如:abcjhjg不要输成abcjhig,即字符间不加空格*/gets(str);for(j=0;j50;j++){if(str[j]=='\0')break;}constintn=j;for(j=n;j0;j--)str[j]=str[j-1];str[n+1]='\0';for(k=0;kn;k++){printf(请输入%c的权值:,str[k+1]);scanf(%d,&weight[k]);}for(k=n;k0;k--)weight[k]=weight[k-1];weight[0]=0;HUFFMANCODEhc=NULL;HUFFMANTREEht;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);Save(weight,n);}2222222222;;;;;;;#includestdio.h#includestdlib.h#definemax50structa{intweight;intparent,lchild,rchild;};structb{charcd[max];intstart;};voidmain(){structaht[2*max];structbhcd[max],d;inti,k,n,c,s1,s2,m1,m2,f;printf(输入n:);scanf(%d,&n);for(i=1;i=n;i++){printf(输入权值:);scanf(%d,&ht[i].weight);ht[i].parent=0;}for(;i=2*n-1;i++)ht[i].parent=ht[i].lchild=ht[i].rchild=0;for(i=n+1;i=2*n-1;i++){m1=m2=30000;s1=s2=0;for(k=1;k=i-1;k++){if(ht[k].parent==0&&ht[k].weightm1){m2=m1;s2=s1;m1=ht[k].weight;s1=k;}elseif(ht[k].parent==0&&ht[k].weightm2){m2=ht[k].weight;s2=k;}}ht[s1].parent=ht[s2].parent=i;ht[i].lchild=s1;ht[i].rchild=s2;ht[i].weight=ht[s1].weight+ht[s2].weight;}for(i=1;i=n;i++){d.start=n-1;c=i;f=ht[i].parent;while(f){if(ht[f].lchild==c)d.cd[--d.start]='0';elsed.cd[--d.start]='1';c=f;f=ht[f].parent;}hcd[i]=d;}printf(输出哈夫编码:);for(i=1;i=n;i++){printf(%d,ht[i].weight);for(k=hcd[i].sta