两种解法: 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 人评分
矩形面积交 (Java代码)浏览:1277 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:475 |
C语言程序设计教程(第三版)课后习题9.1 (Java代码)浏览:480 |
程序员的表白 (C语言代码)浏览:696 |
校门外的树 (C语言代码)浏览:977 |
【亲和数】 (C语言代码)浏览:538 |
WU-整除问题 (C++代码)浏览:640 |
C语言程序设计教程(第三版)课后习题6.3 (C++代码)浏览:1006 |
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:611 |
用筛法求之N内的素数。 (C语言代码)浏览:705 |