原题链接:[编程入门]结构体之成绩记录
两种解法: 1】: 》》单纯链表知识 #include #include 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; } } 2】: #include #include typedef struct student { char s_num[10]; char name[15]; int subject[3]; }stu; void input(stu* head); void print(stu* head); int main() { int n = 0, i = 0; scanf("%d",&n); stu *list = (stu*)malloc(n*sizeof(stu));//此时C自动将其分配好 for(i = 0;i<n;input(&list[i]),i++); for(i = 0;i<n;print(&list[i]),i++); } void input(stu*part) { scanf("%s%s%d%d%d",part->s_num,part->name,&part->subject[0],&part->subject[1],&part->subject[2]); while(getchar()!='\n') continue; } void print(stu* part) { printf("%s,%s,%d,%d,%d\n",part->s_num,part->name,part->subject[0],part->subject[1],part->subject[2]); }
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复