qq_36188625 2016-12-06 15:56 采纳率: 0%
浏览 598

新手哈弗曼树访问出错求大神改一下

#include#include"stdio.h"#include#include#include#include using namespace std;const int Ml = 10;const int Mv = 10;const int Mn = Ml * 2 - 1;const int Mb = 100;typedef struct { string s; int weight; int parent; int lchild; int rchild; string code;}HNodeType;struct assist{ int weight; int num; };HNodeType HaffmanTree(int n, HNodeType HuffNode[]){ int i, j, N; N = n; for (i = 0; i<2 * n - 1; i++){ /*HuffNode[i].s = '0';/ HuffNode[i].weight = 0; HuffNode[i].parent = -1; HuffNode[i].lchild = -1; HuffNode[i].rchild = -1; HuffNode[i].code = '1'; } cout << "请输入" << n << "个权值:" << endl; for (i = 0; i> HuffNode[i].weight; //输入n个叶子结点的权值 } ofstream outfile; outfile.open("e:\hftree.dat"); if (!outfile){ cerr << "打开文件失败!" << endl; return 0; } cout << "请输入" << n << "个字符:" << endl; string str; getchar(); getline(cin, str,'\n'); for (i = 0; i < n; i++){ HuffNode[i].s = str[i]; } outfile << str; outfile.close(); int temp; string temp2; for (j = 0; j < n - 1; j++){ for (i = 0; i < n - 1; i++){ if (HuffNode[i].weight=1){ HuffNode[n].weight = a[m].weight + a[m - 1].weight; HuffNode[n].lchild = a[m - 1].num; HuffNode[n].rchild = a[m].num; HuffNode[a[m].num].parent = n; HuffNode[a[m - 1].num].parent = n; m--; a[m].weight = HuffNode[n].weight; a[m].num = n; n++; for (j = 0; j <= m; j++){ for (i = 0; i <= m; i++){ if (a[i].weight < a[i + 1].weight){ temp3 = a[i]; a[i] = a[i + 1]; a[i + 1] = temp3; } } } } //-------------------------------------------------------------------------------- /*for (i = n - 1; i > 0; i--, n++){ HuffNode[n].weight = HuffNode[n - 1].weight + HuffNode[i - 1].weight; HuffNode[n].rchild = n - 1; HuffNode[n].lchild = i - 1; HuffNode[n - 1].parent = n; HuffNode[i - 1].parent = n; }*/ cout << "哈弗曼树结构:" << endl; for (i = 0; i < 7; i++) cout << HuffNode[i].code << '\t' << HuffNode[i].s << '\t' << HuffNode[i].weight << '\t' << HuffNode[i].lchild << '\t' << HuffNode[i].rchild << '\t' << HuffNode[i].parent << endl; return HuffNode;}void Hfcode(HNodeType a[],int n){ vector cd(n);//叶子节点编码 int i = 0; while (i != n){ int b = 1; int j = a[0].parent; while(b<=i){ j = a[j].rchild; cd[i] += a[j].code; b++; } if(a[j].lchild!=-1) cd[i] += '0'; else cd[i] += '1'; i++; } ofstream outfile; outfile.open("e:\hfcode.dat"); if (!outfile){ cerr << "打开文件失败!" << endl; } for (i = 0; i < n; i++){ outfile << cd[i]; } outfile.close();}void Translate(HNodeType a[],int n){ ifstream infile; string ss; infile.open("e:\hfcode.dat"); infile >> ss; //cout << ss; infile.close(); cout << '\n' << "从e:\hfcode.dat中获取编码串为:" << ss << ",将其译码:" << '\n' << endl; cout << "字符编码" << '\t' << "译码" << endl; int i = 0, m = ss.size(), j = a[0].parent; while (i> n; HNodeType HuffNode[Mn]; HaffmanTree(n, HuffNode); Hfcode(HuffNode, n); Translate(HuffNode,n); _getch();}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-06 16:06
    关注

    所有的代码都堆在一起连个换行都没有,这种提问的态度好霸气,好像提问的才是爷一样,人家回答你的问题天经地义

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛