这个代码哪里错了?
这是原代码:#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
void print(struct student *head);
struct student
{
long num;
float score;
struct student *next;
};
main()
{
struct student *stu;
stu=creat();
print(stu);
printf("\n\n");
system("pause");
}
struct student *creat()
{
int n;
struct student *p1,*p2,*head;
p2=p1=(struct student *)malloc(LEN);
head=NULL;
while(0!=p1->num)
{
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("请输入学号");
scanf("%d",&p1->num);
printf("请输入成绩");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(NULL!=head)
{
do{
printf("学号是%d的成绩是:%f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
我输出的结果是这样的:
请问怎样改才能将这个去掉
“学号是11492784的成绩是:0.000000”
这个怎么去掉 #include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *create();
void print(struct student *head);
struct student
{
long num;
float score;
struct student *next;
};
int main()
{
struct student *stu;
stu=create();
print(stu);
return 0;
}
struct student *create()
{
int n = 0;
struct student *p1,*p2,*head;
p2=p1=(struct student *)malloc(LEN);
head=NULL;
printf("请输入学号");
scanf("%ld",&p1->num);
printf("请输入成绩");
scanf("%f",&p1->score);
while(0!=p1->num)
{
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("请输入学号");
scanf("%ld",&p1->num);
printf("请输入成绩");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(NULL!=head)
{
do{
printf("学号是%ld的成绩是:%f\n",p->num,p->score);
p=p->next;
}while(p);
}
} while(0!=p1->num)
之前就应该输入。你不输入,谁知道p1->num是多少? 超凡天赐 发表于 2017-5-24 17:12
之前就应该输入。你不输入,谁知道p1->num是多少?
明白了,谢谢 守望的摩天轮 发表于 2017-5-24 20:16
明白了,谢谢
那请亲把我设为最佳答案
页:
[1]