解题思路:
注意事项:
//fgets(p->s_num,sizeof(p->s_num),stdin);//全部接收32
//fgets(p->name,sizeof(p->name), stdin);//此处应该使用scanf(因为scanf遇到32(space)或13(enter)就结束,并保留32和13)
参考代码:
#include <stdio.h> #include <malloc.h> typedef struct student { char s_num[10]; char name[15]; int subject1; int subject2; int subject3; struct student* next; }stu; stu* input(stu* head); void print(stu* head); int main() { stu* head = NULL,*p1 = NULL,*p2 = NULL; int n = 0,i = 0; scanf("%d", &n); p1 = head = (stu*)malloc(sizeof(stu)); if (head == NULL) { puts("Fail to memery"); exit(-1); } while (i < n) { p2 = (stu*)malloc(sizeof(stu)); if (p2 == NULL) { puts("Fail to memery"); exit(-1); } else { p2->next = NULL; p1->next = p2; p1 = p2; } i++; } head = input(head); print(head); } stu *input(stu*head) { stu* p = head; while (p->next != NULL) { getchar(); scanf("%s",p->s_num); scanf("%s",p->name); //fgets(p->s_num,sizeof(p->s_num),stdin);//全部接收32 //fgets(p->name,sizeof(p->name), stdin); scanf("%d", &p->subject1); scanf("%d", &p->subject2); scanf("%d", &p->subject3); p = p->next; } return head; } void print(stu* head) { stu* p = head; while (p->next) { printf("%s, %s, %d, %d, %d\n",p->s_num,p->name,p->subject1,p->subject2,p->subject3); p = p->next; } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:581 |
C语言考试练习题_一元二次方程 (C语言代码)浏览:737 |
简单的a+b (C++语言代码)浏览:860 |
C语言程序设计教程(第三版)课后习题11.8 (C语言代码)浏览:886 |
WU-整数平均值 (C++代码)浏览:1245 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:594 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:863 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:492 |
1126题解浏览:595 |
字符串比较 (C语言代码)浏览:685 |