主题:[讨论]关于scanf
/*建立动态链表*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);getchar();
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("%id%f",&p1->num,&p1->score);//getchar();//这里为什么会有错,而将这一行改成下一行就没错了呢?
//scanf("%d",&p1->num);scanf("%f",&p1->score);
}
p2->next=NULL;
return (head);
}
//输出动态链表
void print(struct student *head)
{
struct student *p;
printf("\nNow,these is %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %.lf\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
int main()
{
struct student *stu;
stu=creat();
print(stu);
system("PAUSE");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);getchar();
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("%id%f",&p1->num,&p1->score);//getchar();//这里为什么会有错,而将这一行改成下一行就没错了呢?
//scanf("%d",&p1->num);scanf("%f",&p1->score);
}
p2->next=NULL;
return (head);
}
//输出动态链表
void print(struct student *head)
{
struct student *p;
printf("\nNow,these is %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %.lf\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
int main()
{
struct student *stu;
stu=creat();
print(stu);
system("PAUSE");
return 0;
}