第一范文网 - 专业文章范例文档资料分享平台

数据结构课程设计哈夫曼编码

来源:用户分享 时间:2025/7/23 21:08:18 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Select(HT,i-1,&p1,&p2); HT[p1].parent=i;HT[p2].parent=i; HT[i].lchild=p1;HT[i].rchild=p2; HT[i].weight=HT[p1].weight+HT[p2].weight; } HC=(hfmcode)malloc((n+1)*sizeof(char *)); cd=(char *)malloc(n*sizeof(char)); cd[n-1]='\\0'; for(i=1;i<=n;++i) //给n个字符编码 { start=n-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((n-start)*sizeof(char)); strcpy(HC[i],&cd[start]); } free(cd); }

int main(){ char code[100],h[100],hl[100]; int n,i,j,k,l; ifstream input_file; //文件输入输出流 ofstream output_file; char choice,str[100]; hfmtree HT; hfmcode HC; cout<<\ cout<<\ \计算机(3)班\ \ \ while(choice!='Q'&&choice!='q') //当choice的值不为q且不为Q时循环 {

11

cout<<\ \赫夫曼编码/译码器*************************\\n\ cout<<\ \ \ \ \ cout<<\请输入您要操作的步骤:\ cin>>choice; if(choice=='I'||choice=='i') //初始化赫夫曼树 { cout<<\请输入字符个数:\ cin>>n; hfmcoding(HT,HC,n); for(i=1;i<=n;++i) { cout<

12

for(i=0;i>code; cout<<\编码码值为:\ input_file.close(); } else if(choice=='D'||choice=='d') //读入CodeFile.txt中的编码进行译码,将译出来的字符放入Textfile.txt中 { input_file.open(\ if(!input_file){ cout<<\ return 1; } input_file>>h; input_file.close(); output_file.open(\ if(!output_file) { cout<<\ return 1; } k=0; while(h[k]!='\\0') //先用编码中的前几个和字符的编码相比较,然后往后移 { for(i=1;i<=n;i++){ l=k; for(j=0;j

13

}

hl[j]=h[l]; } hl[j]='\\0'; if(strcmp(HC[i],hl)==0) { output_file<>h; cout<

else if(choice=='Q'||choice=='q') //退出程序 { exit(0); }

else //如果选了选项之外的就让用户重新选择 { cout<<\您没有输入正确的步骤,请重新输入!\ } cout<

return 0;

14

六.调试分析

编码

、译码

、退出

15

搜索更多关于: 数据结构课程设计哈夫曼编码 的文档
数据结构课程设计哈夫曼编码.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c0a0gv1723y3gzju6v8pp_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top