24小时热门版块排行榜    

查看: 2623  |  回复: 12
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

好好努力

金虫 (正式写手)

[求助] 谭浩强C语言教材,P298页照着输入都不对!求原因

#include
#include
#define NULL 0
#define LEN sizeof(struct student)

struct student
{
        long num;
        float score;
        struct student *next;


};
int n;
struct student *creat(void)
{
   struct student *head,*p1,*p2;
   n=0;
   p1=p2=(struct student *)malloc(LEN);
   scanf("%ld%f",&p1->num,&p1->score);
   head=NULL;
   while(p1->num!=0)
   {
           n++;
           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;
  return head;



}

void main()
{
  struct student *lin1=creat();

}

//scanf("%ld%f",&p1->num,&p1->score);好像是这一句出错,但是将float score改成int score就不会出错了,下面的%f相应的改成%d就没错了!真搞不懂了,望高手赐教!
回复此楼
好好就好!!!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

★ ★ ★ ★ ★
微尘、梦想(金币+5): 谢谢参与应助! 2011-05-06 19:50:24
好好努力(金币+1): 2011-05-06 23:31:55
dev-c++4.9 编译通过,毫无问题,
#define NULL 0这句不需要,但是使用float没有错误
CODE:
#include
#include

#define LEN sizeof(struct student)

struct student
{
        long num;
        float score;
        struct student *next;


};
int n;
struct student *creat(void)
{
   struct student *head,*p1,*p2;
   n=0;
   p1=p2=(struct student *)malloc(LEN);
   printf("Please input num and score:");
   scanf("%ld%f",&p1->num,&p1->score);
   printf("%d,%f\n",p1->num,p1->score);

   head=NULL;
   while(p1->num!=0)
   {
           n++;
           if(n==1)   head=p1;
           else   p2->next=p1;
           p2=p1;
           p1=(struct student *)malloc(LEN);
           printf("Please input num and score:");
           scanf("%ld%f",&p1->num,&p1->score);
           printf("%d,%f\n",p1->num,p1->score);
           //printf("%f\n",fabs(p1->num));
   
   }

  p2->next=NULL;
  return head;



}

int main(int args, char* argv[])
{
  struct student *lin1=creat();
  
  system("PAUSE");
  return 0;

}

结果
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2011-05-06 16:36:12
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 好好努力 的主题更新
信息提示
请填处理意见