📄 subject_41213.htm
字号:
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:王磊~~~~~~~~~~~~~~~~~ 回复日期:2003-06-13 23:30:53
<br>内容:#include<stdio.h><BR>#include<string.h><BR>#include<stdlib.h><BR>#include<ctype.h><BR> int n;<BR> struct node{<BR> int w;<BR> int flag;<BR> char c;<BR> struct node *plink,*llink,*rlink;<BR> char code[50];<BR><BR> }*num[100],*root;<BR> FILE *fp;<BR> char tmpcode[50];<BR> int t=0;<BR><BR>int main(void)<BR>{<BR> int i;<BR> void settree(void); //建立树<BR> void code(void); //对文件编码<BR> void decode(void); // 译码<BR> void disp(void) ;<BR> root=(struct node*)malloc(sizeof(struct node));<BR> puts("*******************哈夫曼编/译码器演示******************************");<BR><BR> while(1){<BR>start:<BR><BR> puts("1. 初始化 2. 编码 3. 译码 4.显示编码表 5. 退出");<BR> while(scanf("%d",&i)!=1)<BR> {<BR> while(getchar()!='\n')<BR> continue;<BR> puts("输入错误!");<BR> puts("请重新输入!");<BR> puts("1. 初始化 2. 编码 3. 译码 4.显示编码表 5. 退出");<BR> }<BR> switch (i)<BR> {<BR> case 1:<BR> settree();<BR> break;<BR> case 2:<BR> code();<BR> break;<BR> case 3:<BR> decode();<BR> break;<BR> case 4:<BR> disp();<BR> break;<BR> case 5:<BR> exit(0);<BR> default:<BR> puts("输入错误!");<BR> puts("请重新输入!");<BR> goto start;<BR> }<BR> }<BR>}<BR>void settree(void)<BR>{<BR> int i,j,k;<BR> struct node *p1,*p2,*tmp,*p;<BR> void go(struct node *);<BR> void setcode(struct node *);//建立每一个字符的编码<BR> void printtree(struct node *);<BR> puts("输入字符集的大小:");<BR> scanf("%d",&n);<BR> while(getchar()!='\n')<BR> continue;<BR><BR> for(i=0;i<n;i++)<BR> {<BR> p=(struct node *)malloc(sizeof(struct node));<BR> puts("请输入一个字符");<BR> scanf("%c",&p->c);<BR> while(getchar()!='\n')<BR> continue;<BR> puts("请输入该字符的权值:");<BR> scanf("%d",&p->w);<BR> while(getchar()!='\n')<BR> continue;<BR><BR> p->plink=NULL;<BR> p->rlink=NULL;<BR> p->llink=NULL;<BR> num[i]=p;<BR> }<BR><BR> for(i=0;i<n-1;i++) //排序<BR> {<BR> for(j=i+1;j<n;j++)<BR> {<BR> if(num[i]->w>num[j]->w)<BR> {<BR> tmp=num[i];<BR> num[i]=num[j];<BR> num[j]=tmp;<BR> }<BR> }<BR> }<BR> /*******************************开始建立树***********************/<BR> num[n]=NULL; //结束标志<BR> k=n;<BR> while(num[1]!=NULL)<BR> {<BR> p=(struct node *)malloc(sizeof(struct node));<BR> p1=num[0];<BR> p2=num[1];<BR> p->llink=p1;<BR> p->rlink=p2;<BR> p->plink=NULL;<BR> p1->plink=p;<BR> p2->plink=p;<BR> p->w=p1->w+p2->w;<BR><BR> for(i=1;i<k;i++)<BR> {<BR> num[i]=num[i+1];<BR> }<BR><BR> k--;<BR> num[0]=p;<BR> for(i=0;i<k-1;i++) //排序<BR> {<BR> for(j=i+1;j<k;j++)<BR> {<BR> if(num[i]->w>num[j]->w)<BR> {<BR> tmp=num[i];<BR> num[i]=num[j];<BR> num[j]=tmp;<BR> }<BR> }<BR> }<BR> }<BR><BR> root=num[0];<BR> /*建立完毕*/<BR> /*写入文件,前序*/<BR> if((fp=fopen("c:\\hfmtree.wxl","wb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> setcode(root);<BR> go(root);<BR> fclose(fp);<BR>}<BR>void setcode(struct node *p)<BR>{<BR> if(p->llink==NULL&&p->rlink==NULL)<BR> {<BR> tmpcode[t]='\0';<BR> strcpy(p->code,tmpcode);<BR> }<BR> else<BR> {<BR> tmpcode[t++]='0';<BR> setcode(p->llink);<BR> t--;<BR> tmpcode[t++]='1';<BR> setcode(p->rlink);<BR> t--;<BR> }<BR>}<BR><BR><BR><BR>void go(struct node *p)<BR>{<BR><BR> if(p->llink==NULL&&p->rlink==NULL)<BR> {<BR> fputc('(',fp);<BR> fputc(p->c,fp);<BR> fputs(p->code,fp);<BR> fputc(')',fp);<BR> }<BR> else<BR> {<BR><BR> go(p->llink);<BR> go(p->rlink);<BR> }<BR>}<BR><BR>void code(void)<BR>{<BR> FILE *fp1,*fp2,*fp3;<BR> char ch1,ch2,c;<BR> if((fp1=fopen("c:\\hfmtree.wxl","rb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> if((fp2=fopen("c:\\tobetrans.txt","rb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> if((fp3=fopen("c:\\codefile.wxl","wb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR><BR> while((ch1=fgetc(fp2))!=EOF)<BR> {<BR> t=0;<BR><BR><BR> while((ch2=fgetc(fp1))!=EOF)<BR> {<BR> if(ch1==ch2)<BR> {<BR> while((c=fgetc(fp1))!=')')<BR> {<BR> tmpcode[t++]=c;<BR> }<BR> tmpcode[t]='\0';<BR> fputs(tmpcode,fp3);<BR> fputc('@',fp3);<BR> rewind(fp1);<BR> break;<BR> }<BR> }<BR> }<BR> fclose(fp1);<BR> fclose(fp2);<BR> fclose(fp3);<BR>}<BR><BR>void decode(void)<BR>{<BR> FILE *fp1,*fp2,*fp3;<BR> char ch1,ch2,ch3;<BR> char temp_3[20];<BR> char temp_1[20];<BR> int t1,t3;<BR> if((fp1=fopen("c:\\hfmtree.wxl","rb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> if((fp2=fopen("c:\\textfile.txt","wb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> if((fp3=fopen("c:\\codefile.wxl","rb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR><BR> while((ch3=fgetc(fp3))!=EOF)<BR> {<BR> t3=0;<BR> while(ch3!='@')<BR> {<BR> temp_3[t3++]=ch3;<BR> ch3=fgetc(fp3);<BR> }<BR> temp_3[t3]='\0';<BR> while((ch1=fgetc(fp1))!=EOF)<BR> {<BR> if(isalpha(ch1))<BR> {<BR> ch2=ch1;<BR> t1=0;<BR> while((ch1=fgetc(fp1))!=')')<BR> {<BR> temp_1[t1++]=ch1;<BR> }<BR> temp_1[t1]='\0';<BR><BR> if(strcmp(temp_1,temp_3)==0)<BR> {<BR> fputc(ch2,fp2);<BR> rewind(fp1);<BR> break;<BR> }<BR> }<BR> }<BR> }<BR> fclose(fp1);<BR> fclose(fp2);<BR> fclose(fp3);<BR>}<BR><BR><BR>void disp(void)<BR>{<BR> FILE *fp1,*fp2;<BR> char ch1,ch2;<BR> char tmp[20];<BR> int t;<BR> if((fp1=fopen("c:\\hfmtree.wxl","rb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> if((fp2=fopen("c:\\hfmcode.txt","wb"))==NULL)<BR> {<BR> puts("文件打开错误!");<BR> getchar();<BR> exit(0);<BR> }<BR> while((ch1=fgetc(fp1))!=EOF)<BR> {<BR> if(ch1=='(')<BR> {<BR> t=0;<BR> ch1=fgetc(fp1);<BR> ch2=ch1;<BR> while((ch1=fgetc(fp1))!=')')<BR> {<BR> tmp[t++]=ch1;<BR> }<BR> tmp[t]='\0';<BR> printf("%c-----%s\n",ch2,tmp);<BR> fputc(ch2,fp2);<BR> fputc('-',fp2);<BR> fputc('-',fp2);<BR> fputc('-',fp2);<BR> fputs(tmp,fp2);<BR> fputc('\n',fp2);<BR> }<BR> }<BR> fclose(fp1);<BR> fclose(fp2);<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -