A+Bl类问题 ACM 求指教.1 5 10 20 Sample Output 6 30 先输入,最后输出.
1个回答

你这程序编译能通过?

while(scanf("%d %d",&p1->a,&p1->b)==2);改为:

while(scanf("%d %d",&p1->a,&p1->b)!=EOF);

这样在linux下输入ctrl+D结束,在windows下输入ctrl+z结束.

还有

void printf(struct ab * head)

{struct ab *put;

put=head;

if(head!=NULL)

do

{printf("%ld %ld %ldn",put->a,put->b,put->sum);

put=put->next;

}

while(put!=NULL);

}

你这个定义的这个printf函数和系统函数名冲突,改成其它名字,如print

我改了下你的代码,可以跑通.输入 1 5 10 20,输出为

1 5 6

10 20 30

代码如下:

#include

#include

/*#define NULL 0*/

#define LEN sizeof(struct ab)

int n;

char ch;

struct ab

{int a;

int b;

int sum;

struct ab * next;

};

struct ab * creat(void)

{struct ab * head;

struct ab *p1,*p2;

n=0;

p1=p2=(struct ab *)malloc(LEN);

scanf("%d %d",&p1->a,&p1->b);

head=NULL;

do

{n=n+1;

ch=getchar();

p1->sum=p1->a+p1->b;

if(n==1)head=p1;

else p2->next=p1;

p2=p1;

p1=(struct ab *)malloc(LEN);

}while(scanf("%d %d",&p1->a,&p1->b)!=EOF);

p2->next=NULL;

return (head);

}

void print(struct ab * head)

{struct ab *put;

put=head;

if(head!=NULL)

do

{printf("%ld %ld %ldn",put->a,put->b,put->sum);

put=put->next;

}

while(put!=NULL);

}

void main()

{ struct ab *p;

p=creat();

print(p);

}