主题:菜鸟求助,霍夫曼树的构造及编码问题
谁能帮我看看这个程序应该怎么修改?谢谢了!
#include<iostream>
#include<malloc.h>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct{
unsigned int weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;
void HuffmanCoding(HuffmanTree&HT,HuffmanCode&HC,int *w,int n){
if(n<1)return;
int m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
int i;
HuffmanTree p;//p应该是什么类型?
for(p=HT+1,i=1;i<=n;++i,++p,++w) *p={*w,0,0,0};
for(;i<m;++i,++p) *p={0,0,0,0};
//找出权值最小的两个结点
int min1=513,min2=513;
for(i=n+1;i<m;++i){
if(w[i]<w[min1]){min2=min1;min1=i;}
else if(w[i]<w[min2])
min2=i;
HT[min1].parent=i;HT[min2].parent=i;
HT[i].lchild=min1;HT[i].rchild=min2;
HT[i].weight=HT[min1].weight+HT[min2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
char* cd;//cd应该是什么类型
cd=(char*)malloc(n*sizeof(char));
cd[n-1]="\0";
int c,f;
for(i=1;i<=n;++i){
int start;
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f;f=HT[f].parent)
if(HT[i].lchild==c) cd[--start]="0";
else cd[--start]="1";
HC[i]=(char *)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
delete cd;
}
//出错信息
Build started: Project: Huffman Tree, Configuration: Debug Win32 ------
1>Compiling...
1>Huffman Tree.cpp
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(37) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2146: syntax error : missing ')' before identifier 'f'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ';'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ')'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2143: syntax error : missing ';' before 'if'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(46) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C2146: syntax error : missing ')' before identifier ')malloc'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C3861: ')malloc': identifier not found
1>Build log was saved at "file://c:\Users\HP\Documents\Visual Studio 2008\Projects\Huffman Tree\Huffman Tree\Debug\BuildLog.htm"
1>Huffman Tree - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include<iostream>
#include<malloc.h>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef struct{
unsigned int weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;
void HuffmanCoding(HuffmanTree&HT,HuffmanCode&HC,int *w,int n){
if(n<1)return;
int m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
int i;
HuffmanTree p;//p应该是什么类型?
for(p=HT+1,i=1;i<=n;++i,++p,++w) *p={*w,0,0,0};
for(;i<m;++i,++p) *p={0,0,0,0};
//找出权值最小的两个结点
int min1=513,min2=513;
for(i=n+1;i<m;++i){
if(w[i]<w[min1]){min2=min1;min1=i;}
else if(w[i]<w[min2])
min2=i;
HT[min1].parent=i;HT[min2].parent=i;
HT[i].lchild=min1;HT[i].rchild=min2;
HT[i].weight=HT[min1].weight+HT[min2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
char* cd;//cd应该是什么类型
cd=(char*)malloc(n*sizeof(char));
cd[n-1]="\0";
int c,f;
for(i=1;i<=n;++i){
int start;
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f;f=HT[f].parent)
if(HT[i].lchild==c) cd[--start]="0";
else cd[--start]="1";
HC[i]=(char *)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
delete cd;
}
//出错信息
Build started: Project: Huffman Tree, Configuration: Debug Win32 ------
1>Compiling...
1>Huffman Tree.cpp
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(17) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2059: syntax error : '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '{'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(18) : error C2143: syntax error : missing ';' before '}'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(37) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2146: syntax error : missing ')' before identifier 'f'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ';'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(44) : error C2059: syntax error : ')'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2143: syntax error : missing ';' before 'if'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(45) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(46) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C2146: syntax error : missing ')' before identifier ')malloc'
1>c:\users\hp\documents\visual studio 2008\projects\huffman tree\huffman tree\huffman tree.cpp(47) : error C3861: ')malloc': identifier not found
1>Build log was saved at "file://c:\Users\HP\Documents\Visual Studio 2008\Projects\Huffman Tree\Huffman Tree\Debug\BuildLog.htm"
1>Huffman Tree - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========