| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 464 人关注过本帖
标题:[求助]链表中实数输入问题
只看楼主 加入收藏
wuzhi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-12-16
收藏
 问题点数:0 回复次数:1 
[求助]链表中实数输入问题
#include <malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
main()
{struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
printf("n=%d\n",n);
do
{printf("%ld %5.2f\n",head->num,head->score);
head=head->next;
} while(head!=NULL);
getch();
}

这个程序能成功编译,但在执行scanf 语句时被终止,提示:scanf : floating point formats not linked, abnormal program termination
如果将程序中的float score 更改为 int score 其他地方做相应更改,则能达到预期的结果.
为什么?

[此贴子已经被作者于2006-12-16 22:20:59编辑过]

搜索更多相关主题的帖子: 输入 实数 链表 
2006-12-16 22:16
wuzhi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-12-16
收藏
得分:0 

发完之后发现论坛里有同样的问题,http://http://bbs.bc-cn.net/dispbbs.asp?boardid=5&replyid=108012&id=19959&page=1&skin=0&Star=1
经过14楼的提醒 [float数据输入的问题。
解决办法是在struct student * creat(void)中增加一个中间变量,让编译器知道score是float型的。]
我修改如下, 可以得到预期的结果,不知道修改的对不?
#include <malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
main()
{struct student
{long num;
float score;
struct student *next;
};
float tmp;
int n;
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&tmp);
p1->score=tmp ;
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&tmp);
p1->score=tmp;
}
p2->next=NULL;
printf("n=%d\n",n);
do
{printf("%ld %5.2f\n",head->num,head->score);
head=head->next;
} while(head!=NULL);
getch();
}






2006-12-16 22:57
快速回复:[求助]链表中实数输入问题
数据加载中...
 
   



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

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