标题:急求高手帮忙
只看楼主
姬野々萨西摩尔
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-20
 问题点数:0 回复次数:2 
急求高手帮忙
主函数
#include<iomanip.h>
#include<iostream.h>
#include<string.h>
#define MAX 99
char cha[MAX];
char hc[MAX-1][MAX];
int s1,s2; //设置全局变量,以便在方法(函数)select中返回两个变量

typedef struct //huffman树存储结构
{
unsigned int weight;//权值
int lchild,rchild,parent;
//int huffTree[];
}huftree;


void main()
{
    
int i=0,n=0;
int *w,weight[MAX];
char code[MAX];
huftree tree[MAX];
w=weight;
char in;

while (in!='5')
{
cout<<"              ---------------哈夫曼编码-------------"<<endl<<endl;
cout<<"1 建立初始化哈夫曼树    2 输出哈夫曼编码   3 编码    4 译码   5 退出"<<endl<<endl;
cout<<"请输入(1--5):";cin>>in;
cout<<endl;
switch (in)
{
case '1': cout<<"请输入待编码字符个数:";
   cin>>n;
   cout<<"请输入字符及对应权值:"<<endl;
   for(i=1;i<=n;i++)
   {
    cout<<">>>";
    cin>>cha[i]>>weight[i];
   }
   huffman(tree,w,n); break; //生成huffman树
case '2':huffmancode(tree,code,n);break;
case '3':tohuffmancode(n);break;
case '4':decode(cha,tree,n);break;

}
}
}


 C++类
class huffman
{

public:
    select(huftree tree[],int k) ;//找寻parent为0,权最小的两个节点
    huffman(huftree tree[],int *w,int n) ;//生成huffman树
    ~huffman();
    huffmancode(huftree tree[],char code[],int n);//输出huffman编码
    tohuffmancode(int n);//编码部分
    decode(char ch[],huftree tree[],int n);//译码
private:
    huftree tree[];
    int k,int *w,int n;
    har code[],char ch[];
    typedef struct //huffman树存储结构
{
unsigned int weight;//权值
int lchild,rchild,parent;
//int huffTree[];
}huftree;
}
huffman::select(huftree tree[],int k)
{
int i;
for (i=1;i<=k && tree[i].parent!=0 ;i++); s1=i;//初始化s1
for (i=1;i<=k;i++)
    if (tree[i].parent==0 && tree[i].weight<tree[s1].weight) s1=i;//把最小值赋给s1
for (i=1; i<=k ; i++)
    if (tree[i].parent==0 && i!=s1) break; s2=i;//初始化s2
for (i=1;i<=k;i++)
    if ( tree[i].parent==0 && i!=s1 && tree[i].weight<tree[s2].weight) s2=i;//把最小值赋给s2
}

huffman::huffman( element huffmantree tree[],int *w,int n) //生成huffman树
{ int m,i;
   if (n<=1) return;
   m=2*n-1;
for (i=1;i<=n;i++)//给tree中每个结点权值赋值,且分别给左右孩子及双亲初始化
   { tree[i].weight=w[i]; tree[i].parent=0;
     tree[i].lchild=0;    tree[i].rchild=0; }
for (i=n+1;i<=m;i++)//给除了叶子结点下的其它结点初始化
   { tree[i].weight=0;   tree[i].parent=0;
     tree[i].lchild=0;   tree[i].rchild=0; }
   for (i=n+1;i<=m;i++)//最终结果
   { select(tree, i-1);
         tree[s1].parent=i;
         tree[s2].parent=i;
         tree[i].lchild=s1;
         tree[i].rchild=s2;   
         tree[i].weight =tree[s1]. weight+ tree[s2].weight;
      }
}
huffman::huffmancode(huftree tree[],char code[],int n)//输出huffman编码
{
int start,c,i,f;
cout<<"哈夫曼树:"<<endl;//输出hufftree
   for(i=1;i<=2*n-1;i++)
   cout<<setw(5)<<i
       <<setw(5)<<tree[i].weight
    <<setw(5)<<tree[i].parent
    <<setw(5)<<tree[i].lchild
    <<setw(5)<<tree[i].rchild
    <<endl;
code[n-1]='\0';//
cout<<"哈夫曼编码:"<<endl;
for(i=1;i<=n;i++)
{start=n-1;
for(c=i,f=tree[i].parent;f!=0;c=f,f=tree[f].parent)//输出huffman编码
{if(tree[f].lchild==c)code[--start]='0';//把编码存入code
else code[--start]='1';}
strcpy(hc[i],&code[start]);//把code分别复制给hc
cout<<cha[i]<<"-->"<<hc[i]<<endl;//分别输出编码                                                                  for是满足条件进入
}
}
huffman::tohuffmancode(int n)//编码部分
{
   int i=0,j;
   char anychar[9999];
   cout<<"请输入你要编码的字符串:"<<endl<<">>>";
   cin>>anychar;
   cout<<"编码为:";
   for (;anychar[i]!='\0';i++)
   {
      j=0;
      for(;anychar[i]!=cha[j]&&j<=n;) j++;
   if (j<=n) cout<<hc[j];
   }
   cout<<endl;
}

void decode(char ch[],huftree tree[],int n)//译码
{
int i,j,m;char b;
m=2*n-1;
i=m;
cout<<"请输入编码:"<<endl<<">>>";
cin>>b;
cout<<"译码为:";
while(b!=10)   //遇到回车时,结束
{

if(b=='0')i=tree[i].lchild;
else i=tree[i].rchild;
if(tree[i].lchild==0)
{cout<<ch[i];
   j=i,i=m;
}
cin.get(b);
}
if(tree[j].lchild!=0)
cout<<endl<<"ERROR"<<endl;
cout<<endl<<endl;
}





有4一个一样的错误:
error C2065: 'huffman' : undeclared identifier
445555445\1.cpp(44) : error C2065: 'huffmancode' : undeclared identifier
445555445\1.cpp(45) : error C2065: 'tohuffmancode' : undeclared identifier
445555445\1.cpp(46) : error C2065: 'decode' : undeclared identifier
执行 cl.exe 时出错.
请高手看看啊,交课程设计用啊
搜索更多相关主题的帖子: include parent 
2008-06-20 19:17
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
得分:0 
汗一个,C的基本概念都不懂……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-06-20 19:21
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
得分:0 
额,不过为人还很诚实……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-06-20 19:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-220289-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.247926 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved