C语言编程问题,求帮忙调试!帮我看看这个程序错在哪里,谢谢
#include<stdio.h>#include<string.h>#defineN50#defineM2*N-1typedefstruct{chardata[5];i...
#include<stdio.h>
#include<string.h>
#define N 50
#define M 2*N-1
typedef struct{
char data[5];
int weight;
int parent;
int lchild;
int rchild;
} htnode;
typedef struct hcode{
char cd[N];
int start;
} hcode;
void createht(htnode ht[],int n){
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++) ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;i<2*n-1;i++){
min1=min2=32767;
lnode=rnode=-1;
for(k=0;k<=i-1;k++){
if(ht[k].parent==-1){
if(ht[k].weight<min1){
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
else if(ht[k].weight<min2){
min2=ht[k].weight;
rnode=k;
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
void createhcode(htnode ht[],hcode hcd[],int n){
int i,f,c;
hcode hc;
for(i=0;i<n;i++){
hc.start=n;
c=i;
f=ht[i].parent;
while(f!=-1){
if(ht[f].lchild==c) hc.cd[hc.start--]='0';
else hc.cd[hc.start--]='1';
c=f;
f=ht[f].parent;
}
hc.start++;
hcd[i]=hc;
}
}
void disphcode(htnode *ht,hcode *hcd,int n){
int i,k;
int sum=0,m=0,j;
printf("输出哈夫曼编码:\n");
for(i=0;i<n;i++){
j=0;
printf("%s:\t",ht[i].data);
for(k=hcd[i].start;k<=n;k++){
printf("%c",hcd[i].cd[k]);
j++;
}
m+=ht[i].weight;
sum+=ht[i].weight*j;
printf("\n");
}
printf("\n平均长度=%g\n",1.0*sum/m);
}
void main(){
int n=15,i;
char *str[]={"the","of","a","to","and"."in","that","he","is","at","on","for","his","are","be"};
int fnum[]={1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123};
htnode ht[M];
hcode hcd[N];
for(i=0;i<n;i++){
strcpy(ht[i].data,str[i]);
ht[i].weight=fnum[i];
}
printf("\n");
createht(ht,n);
createhcode(ht,hcd,n);
disphcode(ht,hcd,n);
printf("\n");
} 展开
#include<string.h>
#define N 50
#define M 2*N-1
typedef struct{
char data[5];
int weight;
int parent;
int lchild;
int rchild;
} htnode;
typedef struct hcode{
char cd[N];
int start;
} hcode;
void createht(htnode ht[],int n){
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++) ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;i<2*n-1;i++){
min1=min2=32767;
lnode=rnode=-1;
for(k=0;k<=i-1;k++){
if(ht[k].parent==-1){
if(ht[k].weight<min1){
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
else if(ht[k].weight<min2){
min2=ht[k].weight;
rnode=k;
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
void createhcode(htnode ht[],hcode hcd[],int n){
int i,f,c;
hcode hc;
for(i=0;i<n;i++){
hc.start=n;
c=i;
f=ht[i].parent;
while(f!=-1){
if(ht[f].lchild==c) hc.cd[hc.start--]='0';
else hc.cd[hc.start--]='1';
c=f;
f=ht[f].parent;
}
hc.start++;
hcd[i]=hc;
}
}
void disphcode(htnode *ht,hcode *hcd,int n){
int i,k;
int sum=0,m=0,j;
printf("输出哈夫曼编码:\n");
for(i=0;i<n;i++){
j=0;
printf("%s:\t",ht[i].data);
for(k=hcd[i].start;k<=n;k++){
printf("%c",hcd[i].cd[k]);
j++;
}
m+=ht[i].weight;
sum+=ht[i].weight*j;
printf("\n");
}
printf("\n平均长度=%g\n",1.0*sum/m);
}
void main(){
int n=15,i;
char *str[]={"the","of","a","to","and"."in","that","he","is","at","on","for","his","are","be"};
int fnum[]={1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123};
htnode ht[M];
hcode hcd[N];
for(i=0;i<n;i++){
strcpy(ht[i].data,str[i]);
ht[i].weight=fnum[i];
}
printf("\n");
createht(ht,n);
createhcode(ht,hcd,n);
disphcode(ht,hcd,n);
printf("\n");
} 展开
展开全部
运行上的问题有两个:
1, void createht(htnode ht[],int n) 这个函数对应的括号没有写
void createht(htnode ht[],int n){
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++) ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;i<2*n-1;i++){
min1=min2=32767;
lnode=rnode=-1;
for(k=0;k<=i-1;k++){
if(ht[k].parent==-1){
if(ht[k].weight<min1){
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
else if(ht[k].weight<min2){
min2=ht[k].weight;
rnode=k;
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
} //原程序少这个括号
2.
char *str[]={"the","of","a","to","and"."in","that","he","is","at","on","for","his","are","be"};
这里and和in中间没用逗号
解决这两个问题就可以运行了,但是结果好像不对
1, void createht(htnode ht[],int n) 这个函数对应的括号没有写
void createht(htnode ht[],int n){
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++) ht[i].parent=ht[i].lchild=ht[i].rchild=-1;
for(i=n;i<2*n-1;i++){
min1=min2=32767;
lnode=rnode=-1;
for(k=0;k<=i-1;k++){
if(ht[k].parent==-1){
if(ht[k].weight<min1){
min2=min1;
rnode=lnode;
min1=ht[k].weight;
lnode=k;
}
else if(ht[k].weight<min2){
min2=ht[k].weight;
rnode=k;
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
} //原程序少这个括号
2.
char *str[]={"the","of","a","to","and"."in","that","he","is","at","on","for","his","are","be"};
这里and和in中间没用逗号
解决这两个问题就可以运行了,但是结果好像不对
追问
结果是不对,有什么修正的方法么
追答
我不知道你是怎么构造哈弗曼树的,这个需要自己调,调试的过程也会收获很多的,网上有很多类似的例子,可以借鉴一下,比较一下优劣
展开全部
这么长的程序得有解释啊,估计过几天你自己都不知道是什么了吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
and 和 or 之间没得,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询