求助 这该怎么改才正确啊 哭了 帮帮孩子吧
知道了bug不知道怎么改#include<iostream>
#include<string.h>
using namespace std;
//哈夫曼树的存储
typedef struct{
int weight;//结点的权值
int parent,lchild,rchild;//双亲、左右孩子
}HTNode,*HuffmanTree;//动态分配数组存储哈夫曼树
int min(HuffmanTree HT,int m)
{
int i=0;
int min;
int min_weight;
while(HT.parent!=0)
i++;
min_weight=HT.weight;
min=i;
for(i=1;i<m;i++)
{
if(HT.weight<min_weight && HT.parent==0)
{
min_weight=HT.weight;
min=i;
}
}
HT.parent=1;
return min;
}
void Select(HuffmanTree HT,int m,int &s1,int &s2)
{
int min1,min2;
min1=min(HT,m);
min2=min(HT,m);
}
void CreatHuffmanTree(HuffmanTree &HT,int n)
{//构造哈夫曼树HT
if(n<=1) return;
int m = 2*n-1;
HT = new HTNode;
int s1,s2;
for(int i=1;i<=m;i++)
{
HT.parent=0;
HT.lchild=0;
HT.rchild=0;
}
for(int j=1;j<=n;++j)
cin>>HT.weight;
for(int k=n+1;k<=m;k++)
{
Select(HT,k-1,s1,s2);
HT.parent=k;
HT.parent=k;
HT.lchild=s1;
HT.rchild=s2;
HT.weight=HT.weight+HT.weight;
}
}
typedef char **HuffmanCode;//动态分配数组存储哈夫曼表
void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n)
{//从叶子到根逆向求每个字符的哈夫曼编码,存储在编码表HC中
HC=new char*;
char *cd=new char;
cd='\0';
for(int i=1;i<=n;i++)
{ int c,f;
int start=n-1;
c=i;f=HT.parent;
while(f!=0)
{
--start;
if(HT.lchild==c)
cd='0';
else
cd='1';
c=f;
f=HT.parent;
}
HC=new char;
strcpy(HC,&cd);
}
delete cd;
}
int main()
{
int n;
typedef char **HuffmanCode;
cout<<"请输入结点个数:"<<endl;
cin>>n;
HTNode *HuffmanTree=new HTNode;
int *weight=new int;
char **HC=new char*;
cout<<"请输入"<<n<<"个权值:";
CreatHuffmanTree(HuffmanTree,n);
CreatHuffmanCode(Huffmancode HC,n);
cout<<"输出哈夫曼编码:"<<endl;
return 0;
}
报错:
D:\vc6.0\vc6.0 (2)\Huffman tree.cpp(100) : error C2065: 'Huffmancode' : undeclared identifier
D:\vc6.0\vc6.0 (2)\Huffman tree.cpp(100) : error C2146: syntax error : missing ')' before identifier 'HC'
D:\vc6.0\vc6.0 (2)\Huffman tree.cpp(100) : error C2059: syntax error : ')' 本帖最后由 昨非 于 2020-11-8 11:38 编辑
下方第十四行的位置有语法错误
int main()
{
int n;
typedef char** HuffmanCode;
cout << "请输入结点个数:" << endl;
cin >> n;
HTNode* HuffmanTree = new HTNode;
int* weight = new int;
char** HC = new char* ;
cout << "请输入" << n << "个权值:";
CreatHuffmanTree(HuffmanTree, n);
CreatHuffmanCode(HuffmanTree, HC, n); //这行不对
cout << "输出哈夫曼编码:" << endl;
return 0;
} 昨非 发表于 2020-11-8 11:35
下方第十四行的位置有语法错误
好的 谢谢
页:
[1]