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

#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个回答

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

qq_36188625
qq_36188625 不好意思用手机上才下得app第一次用,
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问