马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
知道了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[i].parent!=0)
i++;
min_weight=HT[i].weight;
min=i;
for(i=1;i<m;i++)
{
if(HT[i].weight<min_weight && HT[i].parent==0)
{
min_weight=HT[i].weight;
min=i;
}
}
HT[min].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[m+1];
int s1,s2;
for(int i=1;i<=m;i++)
{
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(int j=1;j<=n;++j)
cin>>HT[j].weight;
for(int k=n+1;k<=m;k++)
{
Select(HT,k-1,s1,s2);
HT[s1].parent=k;
HT[s2].parent=k;
HT[k].lchild=s1;
HT[k].rchild=s2;
HT[k].weight=HT[s1].weight+HT[s2].weight;
}
}
typedef char **HuffmanCode;//动态分配数组存储哈夫曼表
void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n)
{//从叶子到根逆向求每个字符的哈夫曼编码,存储在编码表HC中
HC=new char*[n+1];
char *cd=new char[n];
cd[n-1]='\0';
for(int i=1;i<=n;i++)
{ int c,f;
int start=n-1;
c=i;f=HT[i].parent;
while(f!=0)
{
--start;
if(HT[f].lchild==c)
cd[start]='0';
else
cd[start]='1';
c=f;
f=HT[f].parent;
}
HC[i]=new char[n-start];
strcpy(HC[i],&cd[start]);
}
delete cd;
}
int main()
{
int n;
typedef char **HuffmanCode;
cout<<"请输入结点个数:"<<endl;
cin>>n;
HTNode *HuffmanTree=new HTNode[2*n-1];
int *weight=new int[n];
char **HC=new char*[n];
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[2 * n - 1];
int* weight = new int[n];
char** HC = new char* [n];
cout << "请输入" << n << "个权值:";
CreatHuffmanTree(HuffmanTree, n);
CreatHuffmanCode(HuffmanTree, HC, n); //这行不对
cout << "输出哈夫曼编码:" << endl;
return 0;
}
|