完整的程序是这样的:
bool compare_huff(const HUFF & m1, const HUFF & m2)
{
return m1.prob > m2.prob;
}
int main(int argc, char* argv[])
{
vector<HUFF> vhuff;
char code[100]=m_shuru;
for(int i=0;i<strlen(code);i++)
{
bool newchar = true;
for(int j=0;j<vhuff.size();j++)
{
if(vhuff[j].c == code[i])
{
vhuff[j].prob += 1.0;
newchar = false;
break;
}
}
if(newchar)
{
HUFF huff;
huff.c = code[i];
huff.prob = 1.0;
vhuff.push_back(huff);
}
}
for(int j=0;j<vhuff.size();j++)
{
m_shchu=vhuff[j].c +" 出现的次数为: "+vhuff[j].prob+"\r\n";
}
sort(vhuff.begin(),vhuff.end(),compare_huff);
for(j=0;j<vhuff.size();j++)
{
vhuff[j].flag = j;
}
int leastIndex = vhuff.size()-1;
for(i=1;i<vhuff.size();i++)
{
vhuff[leastIndex-1].prob += vhuff[leastIndex].prob;
vhuff[leastIndex].prob = 0;
int tflag1 = vhuff[leastIndex-1].flag;
int tflag2 = vhuff[leastIndex].flag;
for(int k=0;k<vhuff.size();k++)
{
if(vhuff[k].flag == tflag1)
vhuff[k].hcode.push_back('1');
if(vhuff[k].flag == tflag2)
{
vhuff[k].hcode.push_back('0');
vhuff[k].flag = tflag1;
}
}
sort(vhuff.begin(),vhuff.end()-i,compare_huff);
for(j=0;j<vhuff.size()-i;j++)
{
int tflag = vhuff[j].flag;
for(int k=0;k<vhuff.size();k++)
{
if(vhuff[k].flag == tflag)
{
vhuff[k].flag = j+50;
}
}
}
for(k=0;k<vhuff.size();k++)
{
if(vhuff[k].flag >= 50)
{
vhuff[k].flag -= 50;
}
}
leastIndex--;
}
for(i=0;i<vhuff.size();i++)
{
reverse(vhuff[i].hcode.begin(),vhuff[i].hcode.end());
for(int j=0;j<vhuff[i].hcode.size();j++)
m_bianma="字符"+vhuff[i].c+" 的编码是: "+vhuff[i].hcode[j]+"\r\n";
}
return 0
}
编译出错:D:\程序设计\HUFFMAN编码\HUFFMAN编码Dlg.cpp(196) : error C2601: 'compare_huff' : local function definitions are illegal
D:\程序设计\HUFFMAN编码\HUFFMAN编码Dlg.cpp(202) : error C2601: 'main' : local function definitions are illegal
执行 cl.exe 时出错.