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

相识的人2022-10-04 11:39:541条回答

A+Bl类问题 ACM 求指教.1 5 10 20 Sample Output 6 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)==2);(这里怎么结束)
p2->next=NULL;
return (head);
}
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);
}
void main()
{ struct ab *p;
p=creat();
printf(p);
}

已提交,审核后显示!提交回复

共1条回复
HandGunMan 共回答了19个问题 | 采纳率94.7%
你这程序编译能通过?
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);
}
1年前

相关推荐