首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

这里有个小疑点

2012-06-01 
这里有个小问题#includestdio.h#includestring.htypedef struct//////////////////////定义哈夫曼的类

这里有个小问题
#include<stdio.h>
#include<string.h>
typedef struct //////////////////////定义哈夫曼的类型
{
int weight; //////////////////////权值
char data; //////////////////////输入字符
int parent,lchild,rchild; /////////////////////树的下标
}hufmtree;
////////////////////////////////////////////////////
typedef struct
{
char ch;
char bits[7];
}codenode;
typedef codenode huffmancode[6];

//////////////////////////////////////////////////////
void select(hufmtree tree[],int n,int &p1,int &p2)
{
int i;
  p1=p2=1;
while(tree[p1].parent!=0) //领p1指向未被使用的结点
{p1++;}  
for(i=2;i<=n;i++) //找出第一个最小权值的值p1
{
  if(tree[i].weight<tree[p1].weight && tree[i].parent==0)
p1=i;
}
tree[p1].parent=1; //
while(tree[p2].parent!=0) //领p2指向未被使用的结点
{p2++;}
if(p1==1) //找出第二个最小权值的值p2
{p2=2;} //初始p2=p2=1; 当p1=1为最小的时候 p2也只会是1所以要令p2=2开始
for(i=2;i<=n;i++)
{
   
  if(tree[i].weight<tree[p2].weight && tree[i].parent==0)
  p2=i;
}
tree[p2].parent=1;



/////////////////////////////////////////////////////
void creathuffman(hufmtree tree[],int n)
{
int m,i,p1,p2,f;
char c;
if(n<=1)
return;
m=2*n;
for(i=1;i<m;i++) //
{
tree[i].parent=0; ///////////////////////////////对数组初始化,下标为0的单元不用
tree[i].lchild=0; //
tree[i].rchild=0;
tree[i].weight=0;
}
printf("请输入权值和字符\n");
  for(i=1;i<=n;i++)
{
scanf("%d,%c",&f,&c);
  tree[i].weight=f;
  tree[i].data=c;
}

for(i=n+1;i<m;i++)
{
select(tree,i-1,p1,p2); ////////////////////////////////进行合并,产生n-1个节点
  tree[p1].parent=i;
  tree[p2].parent=i;
tree[i].lchild=p1;
tree[i].rchild=p2;
tree[i].weight=tree[p1].weight+tree[p2].weight;
}
}
////////////////////////////////////////////////////////////
void print(hufmtree tree[],int n)
{
int i;
printf("data weight parent lchild rchild\n");
for(i=1;i<=n;i++)
{
printf("%3c %3d %3d %3d %3d\n",tree[i].data,tree[i].weight,tree[i].parent,tree[i].lchild,tree[i].rchild);
}
}

///////////////////////////////////////////////////////////////
void huffmancoding(hufmtree tree[],huffmancode h[])
{
int start,c,p,i;
char cd[6];
cd[6]='\0';
for(i=0;i<6;i++)
{
h[i].ch=tree[i];
start=6;
c=i;
while(p=tree[c].parent)
{
if(tree[p].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
c=p;
}
strcpy(h[i].bits,cd[start]);
}
}
////////////////////////////////////////////////////////////////////////////////
int main()
{
  int i=0,f,n;
  char ch;
  hufmtree tree[100]; /////////////这是一个数组
  huffmancode h[6];
  printf("请输入节点数\n");
  scanf("%d",&n);
  creathuffman(tree, n);
  print(tree,n);
  huffmancoding( tree, h);
  return 0;
}
////////////////////////////////////////////////////////////////



C:\Users\Administrator\Desktop\1111\ghf\fh.cpp(108) : error C2228: left of '.bits' must have class/struct/unC:\Users\Administrator\Desktop\1111\ghf\fh.cpp(97) : error C2228: left of '.ch' must have class/struct/union type


ion type

编译器说出现了这两个错误,这错哪里了?定义结点好像没错

[解决办法]
它说左边必须是个结构体,看这两行代码typedef codenode huffmancode[6];huffmancode h[6];
h会是 6*6二维的,访问的时候要 h[i][j]访问

热点排行