#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
void main()
{
struct student *input(void);
void print(struct student *p1);
struct student * head;
head=input();
print(head);
}
struct student *input(void)
{
struct student *head,*p1,*p2;
int x=0;head=NULL;
do
{
p1=(struct student *) malloc(LEN);
scanf("%1d,%s,%f,%f,%f",&p1->num,&p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
printf("\n");
if (x==0) head=p1;
else p2->next=p1;
p2=p1;x++;
}while(x<5);
p2->next=NULL;
return (head);
}
void print(struct student *p1)
{
printf("\nthere is 5 record are:\n");
if (p1!=NULL)
do {
printf("%1d,%s,%f,%f,%f",p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2]);
p1=p1->next;
printf("\n");
}while (p1!=NULL);
}
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
struct student *input(void); //函数声明最好在main函数外部
void print(struct student *p1);
void main()
{
struct student *head;
head=input();
print(head);
}
struct student *input(void)
{
struct student *head,*p1,*p2;
int x=0;head=NULL;
do
{
p1=(struct student *) malloc(LEN);
scanf("%d", &p1->num); //改成5次输入
scanf("%s", &p1->name);
scanf("%f", &p1->score[0]);
scanf("%f", &p1->score[1]);
scanf("%f", &p1->score[2]);
printf("\n");
if (x==0) head=p1;
else p2->next=p1;
p2=p1;
x++;
}while(x<5);
p2->next=NULL;
return (head);
}
void print(struct student *p1)
{
printf("\nthere is 5 record are:\n");
//if (p1!=NULL) //这句没必要
do {
printf("%1d,%s,%f,%f,%f",p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2]);
p1=p1->next;
printf("\n");
}while (p1!=NULL);
}
#include <stdlib.h>
#include <string.h>
#ifndef NULL
#define NULL 0
#endif
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
int main(void)
{
struct student *input(void);
void print(struct student *p1);
void delt(struct student *p);
struct student * head;
head=input();
print(head);
delt(head);
return 0;
}
struct student *input(void)
{
struct student *head = NULL, *p1, *p2;
int x = 0;
do
{
p1=(struct student *)malloc(LEN);
/* 去掉逗号,输入时以空格间隔,如果保留逗号,
* 输入时以逗号间隔,但是因为有个%s,会把逗号也默认为是字串.
*/
scanf("%1d%s%f%f%f", &p1->num, p1->name, /* 不是&p1->name */
&p1->score[0], &p1->score[1], &p1->score[2]);
printf("\n");
if (x == 0) head = p1;
else p2->next = p1;
p2 = p1;
} while(++x < 5);
p2->next=NULL;
return (head);
}
void print(struct student *p)
{
/* 这里不要直接使用传进的参数,这样会破坏原有数据结构 */
struct student *p1 = p;
printf("\nthere is 5 record are:\n");
if (p1 != NULL)
do {
printf("%1d,%s,%f,%f,%f", p1->num, p1->name, p1->score[0], p1->score[1], p1->score[2]);
p1 = p1->next;
printf("\n");
}while (p1 != NULL);
}
/* 因为程序里分配了内存,应该free掉 */
void delt(struct student *p)
{
struct student *p1 = p, *p2;
while (NULL != p1)
{
p2 = p1->next;
free(p1);
p1 = p2;
}
}本回答被提问者采纳
33,44,55
2,ffd
44,55,32
3,sfdf
45,234,55
4,try
55,64,77
5,hgj
55,65,334
there is 5 record are:
1,sff,33.000000,44.000000,55.000000
2,ffd,44.000000,55.000000,32.000000
3,sfdf,45.000000,234.000000,55.000000
4,try,55.000000,64.000000,77.000000
5,hgj,55.000000,65.000000,334.000000
Press any key to continue
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
void main()
{
struct student *input(void);
void print(struct student *p1);
struct student * head;
head=input();
print(head);
}
struct student *input(void)
{
struct student *head,*p1,*p2;
int x=0;head=NULL;
do
{
p1=(struct student *) malloc(LEN);
scanf("%1d,%s",&p1->num,&p1->name);
scanf("%f,%f,%f",&p1->score[0],&p1->score[1],&p1->score[2]);
printf("\n");
if (x==0) head=p1;
else p2->next=p1;
p2=p1;x++;
}while(x<5);
p2->next=NULL;
return (head);
}
void print(struct student *p1)
{
printf("\nthere is 5 record are:\n");
if (p1!=NULL)
do {
printf("%1d,%s,%f,%f,%f",p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2]);
p1=p1->next;
printf("\n");
}while (p1!=NULL);
}
错误在于
scanf("%1d,%s,%f,%f,%f",&p1->num,&p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
这个里面 %s会把从第一个逗号后面的连逗号全接受为字符串name
如果你输入
1,big,34,22,55
回车后 num 是1 name 是 big,34,22,55
明白了吗
有疑问请追问 满意记得采纳哦
#include <stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
struct student *input(void);
void print(struct student *p1);
void main()
{
struct student * head;
head=input();
print(head);
}
struct student *input(void)
{
struct student *head,*p1,*p2;
int x=0;head=NULL;
do
{
p1=(struct student *) malloc(LEN);
scanf("%1d,%[^,],%f,%f,%f",&p1->num,&p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
printf("\n");
if (x==0) head=p1;
else p2->next=p1;
p2=p1;x++;
}while(x<5);
p2->next=NULL;
return (head);
}
void print(struct student *p1)
{
printf("\nthere is 5 record are:\n");
if (p1!=NULL)
do {
printf("%1d,%s,%f,%f,%f",p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2]);
p1=p1->next;
printf("\n");
}while (p1!=NULL);
}
其实就改了一行代码。scanf("%1d,%[^,],%f,%f,%f",&p1->num,&p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);追问
%[^,]是什么意思啊?我看的教科书里没有这个格式,能简单介绍下嘛?
追答%[]表示格式输入。可以参考C99的标准。
[]中的内容用来生成输入的字符串,直到它遇到^后面的字符为止。
就不直接上C99了,你可以看C99的标准。