帮忙解释下输入元素后错误的原因。

thinkboy234 2009-01-13 04:00:58
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct xueji)
#define GESHI "%d%s%s%f%f%f%f%f"
struct xueji
{
int num;
char name[20];
char sex;
float english;
float chinese;
float math;
float computer;
float total;
struct xueji *next;
};
void *print(struct xueji *head)
{for(;head;head=head->next)
printf("%d%s%s%f%f%f%f%f",head->num,head->name,head->sex,head->english,head->chinese,head->math,head->computer,head->total);
return 0;
}
struct xueji *creat();
main()
{
struct xueji *head;
head=NULL;
printf("please input students` message and the enter a student NO.0.\n");
head=creat();
print(head);
return 0;
}
struct xueji *creat()
{
struct xueji *head,*p1,*p2,*p0;
p1=p2=(struct xueji *)malloc(LEN);
head=p1;
p1->next=NULL;
p0=p1;
scanf(GESHI,&p1->num,p1->name,&p1->sex,&p1->english,&p1->chinese,&p1->math,&p1->computer,&p1->total);
while(p0->num!=0)
{
p0=(struct xueji *)malloc(LEN);
scanf(GESHI,&p0->num,p0->name,&p0->sex,&p0->english,&p0->chinese,&p0->math,&p0->computer,&p0->total);
p1=head;
if(p0->num<p1->num)
{
head=p0;p0->next=p1;
}
else if(p1->next==NULL)
{
p1->next=p0;p0->next=NULL;
}
else
{
while(p0->num>=p1->num)
{
p2=p1;p1=p1->next;
}
p2->next=p0;
p0->next=p1;
}
}
return(head);
}

输入:5 asd t 5 8 9 3 4
回车后提示出现:floating point formates not linked
abnormal program termination
...全文
103 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinkboy234 2009-01-14
  • 打赏
  • 举报
回复
哦。。。
brookmill 2009-01-14
  • 打赏
  • 举报
回复
至于这个循环
while(p0->num!=0)
{
p0=(struct xueji *)malloc(LEN);
......
是不是要判断,就不一定了,因为p0被malloc赋值之后就没改动过
不过判断了更安全
while (p0 && p0->num!=0)
{ ......

严格来说,每次调用malloc之后,都要检查返回值是不是NULL
brookmill 2009-01-14
  • 打赏
  • 举报
回复
while(p0->num>=p1->num)
{
p2=p1;p1=p1->next;
if (p1 == NULL)
break;
}
thinkboy234 2009-01-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Zolou 的回复:]
printf("%d%s%s%f%f%f%f%f",head->num,head->name,head->sex,head->english,head->chinese,head->math,head->computer,head->total);
程序将head->sex作为字符串的首地址处理,即访问0x00~0xFF间的地址,你说呢...
另外,两个while都不判断地址是否有效?
[/Quote]
怎么判断地址有效?
thinkboy234 2009-01-14
  • 打赏
  • 举报
回复
改成%c 还是不行。
brookmill 2009-01-14
  • 打赏
  • 举报
回复
还有,用scanf读入total可能不太妥当,因为total可以通过累加得到,这样就有冗余数据,而且有可能输入的total和算出来的结果不一致
这个要看具体需求了
brookmill 2009-01-14
  • 打赏
  • 举报
回复
据说这是TC常有的问题。可以试试这些解决方法:
1. 在main刚开头的地方加一行 float a=0.0;
2. scanf到一个中间变量再赋值,比如:float english; scanf("%f", &english); p0->english = english;
3. 换编译器

xidianxiancai 2009-01-14
  • 打赏
  • 举报
回复
做了点小改动
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct xueji)
#define GESHI "%d %s %c %f %f %f %f %f" /*这里最好加上','不然输入的时候会有毛病*/
struct xueji
{
int num;
char name[20];
char sex;
float english;
float chinese;
float math;
float computer;
float total;
struct xueji *next;
};
void print(struct xueji *head)
{
for(;head!=NULL;head=head->next)
printf("%d %s %c %f %f %f %f %f\n",head->num,head->name,head->sex,head->english,head->chinese,head->math,head->computer,head->total);
}
struct xueji *creat();
main()
{
struct xueji *head;
head=NULL;
printf("please input students` message and the enter a student NO.0.\n");
head=creat();
print(head);
return 0;
}
struct xueji *creat()
{
struct xueji *head,*p1,*p2,*p0;
p1=p2=(struct xueji *)malloc(LEN);
head=p1;
p1->next=NULL;
p0=p1;
scanf(GESHI,&p1->num,p1->name,&p1->sex,&p1->english,&p1->chinese,&p1->math,&p1->computer,&p1->total);
while(p0->num!=0)
{
p0=(struct xueji *)malloc(LEN);
scanf(GESHI,&p0->num,p0->name,&p0->sex,&p0->english,&p0->chinese,&p0->math,&p0->computer,&p0->total);
if(p0->num==0)break; /*加一个判断的条件*/
p1=head;
if(p0->num < p1->num) /*插入第一个元素*/
{
head=p0;p0->next=p1;
}
else if(p1->next == NULL)
{
p1->next=p0;p0->next=NULL;
}
else
{
while(p0->num >= p1->num)
{
p2=p1;p1=p1->next;
}
p2->next=p0;
p0->next=p1;
}
}
return(head);
}
Zolou 2009-01-13
  • 打赏
  • 举报
回复
printf("%d%s%s%f%f%f%f%f",head->num,head->name,head->sex,head->english,head->chinese,head->math,head->computer,head->total);
程序将head->sex作为字符串的首地址处理,即访问0x00~0xFF间的地址,你说呢...
另外,两个while都不判断地址是否有效?
liushiyuyi 2009-01-13
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct xueji)
#define GESHI "%d%s %c %f%f%f%f%f" //正确格式struct xueji
{
int num;
char name[20];
char sex;
float english;
float chinese;
float math;
float computer;
float total;
struct xueji *next;
};
void *print(struct xueji *head) //
{
for(; head; head=head->next )
printf("%d%s%c%f%f%f%f%f",head->num,head->name,head->sex,head->english,head->chinese,head->math,head->computer,head->total);
return 0; //按照你的定义,这里应返回空指针
}
struct xueji *creat();
main()
{
struct xueji *head;
head=NULL;
printf("please input students` message and the enter a student NO.0.\n");
head=creat();
print(head);
return 0;
}
struct xueji *creat()
{
struct xueji *head,*p1,*p2,*p0;
p1=p2=(struct xueji *)malloc(LEN);
head=p1;
p1->next=NULL;
p0=p1;
scanf(GESHI,&p1->num,p1->name,&p1->sex,&p1->english,&p1->chinese,&p1->math,&p1->computer,&p1->total);
while(p0->num!=0)
{
p0=(struct xueji *)malloc(LEN);
scanf(GESHI,&p0->num,p0->name,&p0->sex,&p0->english,&p0->chinese,&p0->math,&p0->computer,&p0->total);
p1=head;

if(p0->num < p1->num)
{
head=p0;
p0->next=p1;
}
else
if(p1->next==NULL)
{
p1->next=p0;
p0->next=NULL;
}
else
{
while(p0->num >= p1->num) //when p0->num >= p1->num and p1->next==NULL is a bug
{
p2=p1;
p1=p1->next;
}

p2->next=p0;
p0->next=p1;
}
}
return(head);
}

//输入:5 asd t 5 8 9 3 4
//回车后提示出现:floating point formates not linked
//abnormal program termination

nineforever 2009-01-13
  • 打赏
  • 举报
回复
你在用TC?
随便调用个浮点函数就好了。因为默认不会链浮点库,而你scan %f的时候需要调浮点库里的东西。

http://www.cnblogs.com/qixin622/archive/2007/05/01/734006.html
wyswyg63 2009-01-13
  • 打赏
  • 举报
回复
#define GESHI "%d%s%s%f%f%f%f%f"
改成
#define GESHI "%d%s%c%f%f%f%f%f"
试试,
char sex;是一个字符,应该用%c读而不是%s

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧