读书人

下面这段代码用VC6.0如何通过过

发布时间: 2012-03-15 11:50:38 作者: rapoo

下面这段代码用VC6.0怎么通过过?
把C++改为C就出错了,错在哪里了呢?


#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <memory.h>


struct node
{
int value;
struct node * left;
struct node * right;
struct node * next;
};


struct node * listsort(struct node* head)
{
int i,min;
int len=0;
struct node* p=head,*phead,*tp=head,*p2;

while(p) len++,p=p-> next;

for(i=1;i <=len;i++)
{
min=tp-> value;
p=tp-> next;
while(p)
{
if(p-> value <min)
{
min=p-> value;
}
p=p-> next;
}

if(tp-> value==min)
{
if(i==1) {
head=tp;
phead=head;
tp=tp-> next;
phead-> next=NULL;
} else {
phead-> next=tp;
phead=tp;
tp=tp-> next;
phead-> next=NULL;
}


} else {
p=tp;
while (p)
{
if(p-> next-> value==min)
break;
p=p-> next;
}
p2=p-> next;
p-> next=p2-> next;
p2-> next=NULL;
if(i==1) {
head=p2;
phead=head;
} else {
phead-> next=p2;
phead=phead-> next;
}

}

}

return head;
}

int getlen(struct node* head, int level)
{
level++;
if(head-> left==NULL && head-> right==NULL)
return level*head-> value;
else
return getlen(head-> left,level)+getlen(head-> right,level);
};


int gennum(int in[])
{
int i,len=0;

i=0;
while (!in[i])
i++;
struct node * head=(struct node *)malloc(sizeof(struct node));
head-> next=NULL;
head-> right=NULL;
head-> left=NULL;
head-> value=in[i];
struct node *phead = head;

for(i=i+1;i <27;i++)
if(in[i])
{
struct node *p=(struct node *)malloc(sizeof(struct node));
p-> value = in[i];
p-> next = NULL;
p-> right = NULL;
p-> left = NULL;
phead-> next = p;
phead = p;
//num[len++]=num[i];
}

// for(i=0;i <len;i++)
// insert(num[i])
while (head-> next)
{
head=listsort(head);
struct node* p2=(struct node *)malloc(sizeof(struct node));
phead=head-> next;
p2-> value=head-> value+phead-> value;
p2-> next=phead-> next;
p2-> left=head;
p2-> right=phead;
head=p2;

}
if(head-> left==NULL && head-> right==NULL)
return head-> value;
// 10
return getlen(head,-1);
}


main()
{
char s[1000];
int i,num[27];;
while (scanf( "%s ",s)&&!strcmp(s, "END "))
{
memset(num,0,sizeof(num));


for(i=0;i <strlen(s);i++) {
int place=s[i]- 'A ';
if(place> =0&&place <25)
num[place]++;
else
num[26]++;
}

int len=gennum(num);
printf( "%d %d %.1f ",strlen(s)*8, len, strlen(s)*8.0/len);
}
return 0;
}

[解决办法]
你用到了
strcmp
strlen
那就要包含头文件 string.h
[解决办法]

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <memory.h>

#include <string.h>


struct node
{
int value;
struct node * left;
struct node * right;
struct node * next;
};


struct node * listsort(struct node* head)
{
int i,min;
int len=0;
struct node* p=head,*phead,*tp=head,*p2;

while(p) len++,p=p-> next;

for(i=1;i <=len;i++)
{
min=tp-> value;
p=tp-> next;
while(p)
{
if(p-> value <min)
{
min=p-> value;
}
p=p-> next;
}

if(tp-> value==min)
{
if(i==1) {
head=tp;
phead=head;
tp=tp-> next;
phead-> next=NULL;
} else {
phead-> next=tp;
phead=tp;
tp=tp-> next;
phead-> next=NULL;
}


} else {
p=tp;
while (p)
{
if(p-> next-> value==min)
break;
p=p-> next;
}
p2=p-> next;
p-> next=p2-> next;
p2-> next=NULL;
if(i==1) {
head=p2;
phead=head;
} else {
phead-> next=p2;
phead=phead-> next;
}

}

}

return head;
}

int getlen(struct node* head, int level)
{
level++;
if(head-> left==NULL && head-> right==NULL)
return level*head-> value;
else
return getlen(head-> left,level)+getlen(head-> right,level);
};


int gennum(int in[])
{
int i,len=0;
struct node * head;//
struct node * phead;//
struct node* p;
struct node* p2;

i=0;
while (!in[i])
i++;
//error
//struct node * head=(struct node *)malloc(sizeof(struct node));
head=(struct node *)malloc(sizeof(struct node));
head-> next=NULL;
head-> right=NULL;
head-> left=NULL;
head-> value=in[i];
//struct node * phead = head;//error
phead = head;

for(i=i+1;i <27;i++)
if(in[i])
{
p=(struct node *)malloc(sizeof(struct node));
p-> value = in[i];
p-> next = NULL;
p-> right = NULL;
p-> left = NULL;
phead-> next = p;
phead = p;
//num[len++]=num[i];
}

// for(i=0;i <len;i++)
// insert(num[i])
while (head-> next)
{
head=listsort(head);
//error
//struct node* p2=(struct node *)malloc(sizeof(struct node));
p2=(struct node *)malloc(sizeof(struct node));

phead=head-> next;
p2-> value=head-> value+phead-> value;
p2-> next=phead-> next;
p2-> left=head;
p2-> right=phead;
head=p2;

}
if(head-> left==NULL && head-> right==NULL)
return head-> value;
// 10
return getlen(head,-1);
}


main()
{
int len;

char s[1000];
int i,num[27];;
while ( (scanf( "%s ",s)) && (!strcmp(s, "END ")) )


{
memset(num,0,sizeof(num));
for(i=0; i <(signed)(strlen(s)) ;i++) ////error这里需要转换一下
{
int place=s[i]- 'A ';
if(place> =0&&place <25)
num[place]++;
else
num[26]++;
}
//int len=gennum(num);//error
len=gennum(num);
printf( "%d %d %.1f ",strlen(s)*8, len, strlen(s)*8.0/len);
}
return 0;
}


读书人网 >C语言

热点推荐