题目描述
从键盘上输入n个学生的信息:学号,姓名, 数学, 英语, 语文的成绩。再从键盘上输入一个学号, 用顺序查找的方法在链表中查找该学生的信息。若找到,输出该学生的信息,没有找到,输出No such person
输入
输入学生的信息:学号,姓名, 数学, 英语, 语文的成绩, 输入学号为0时终止输入;
再输入学号进行查询.
输出
若找到,输出该学生的信息,成绩保留一位小数,没有找到,输出No such person
样例输入
20130610 Guojin 90.5 89.5 70
20140612 Huangrong 99.5 100 90.5
20150548 HuangYaoshi 89 78 56
0 0 0 0 0
20140612
样例输出
20140612 Huangrong 99.5 100.0 90.5
1 #include <stdio.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 //创建STUDENT类型变量 包括学号、姓名、数学, 英语, 语文的成绩 6 typedef struct student { 7 char number[20]; 8 char name[50]; 9 float math; 10 float english; 11 float chinese; 12 struct student *pNext; 13 }STUDENT; 14 //创建链表 15 int iCount; 16 STUDENT *Create() { 17 STUDENT *pHead=NULL, *pEnd, *pNew ; 18 19 pNew = pEnd = (STUDENT*)malloc(sizeof(STUDENT)); 20 scanf("%s%s%f%f%f", pNew->number, pNew->name, &pNew->math, &pNew->english, &pNew->chinese); 21 while (strcmp(pNew->number,"0")!=0) 22 { 23 iCount++; 24 if(iCount==1) 25 { 26 pNew->pNext = pHead; 27 pEnd = pNew; 28 pHead = pNew; 29 } 30 else 31 { 32 pNew->pNext = NULL; 33 pEnd->pNext = pNew; 34 pEnd = pNew; 35 } 36 pNew =(STUDENT*)malloc(sizeof(STUDENT)); 37 scanf("%s%s%f%f%f", pNew->number, pNew->name, &pNew->math, &pNew->english, &pNew->chinese); 38 39 } 40 free(pNew); 41 return pHead; 42 } 43 int main() { 44 int flag = 1; 45 char number[20]; STUDENT *p; 46 p = Create(); 47 scanf("%s", number); 48 while (p->pNext != NULL) 49 { 50 p = p->pNext; 51 if (strcmp(p->number, number) == 0) 52 { 53 printf("%s %s %0.1f %0.1f %0.1f", p->number, p->name, p->math, p->english, p->chinese); flag =0; 54 } 55 56 } 57 if (flag == 1) printf("No such person"); 58 free(p); 59 return 0; 60 }