解题思路:
1、使用结构体指针存储学生信息
2、输入数据长度后使用malloc分配存储空间
注意事项:
参考代码:
#include<stdio.h> #include<malloc.h> typedef struct _student_info_ { char s_id[16]; //学号 char s_name[32]; //姓名 int s_score[3]; //三科成绩 }student_info; void input(student_info *L) { scanf("%s %s %d %d %d", L->s_id, L->s_name, &(L->s_score[0]), &(L->s_score[1]), &(L->s_score[2])); } void print(student_info *L) { printf("%s,%s,%d,%d,%d\n", L->s_id, L->s_name, L->s_score[0], L->s_score[1], L->s_score[2]); } int main() { student_info *list; int n = 0, i = 0; scanf("%d", &n); while(getchar() != '\n') continue; if(n > 0 && n < 100) list = (student_info *)malloc(sizeof(student_info)*n); else return 0; for(i = 0; i < n; i++) { input(&list[i]); //一行一行获取,忽略一行多余的数据 while(getchar() != '\n') continue; } for(i = 0; i < n; i++) { print(&list[i]); } free(list); return 0; }
0.0分
60 人评分
为啥定义的list指针,却可以使用 list【i】呢
零K沁雪 2019-10-30 18:01:20 |
list 只是指针的首地址,本题中 list 指向的内存地址后存在连续的多个student_info,使用 list[i]可以精确的指向每一个 student_info
编译没报错,运行错误,这么回事呀,求大神看看,解释一下好不 #include<stdio.h> struct student_information { char student_id[16]; char student_name[16]; int chinese; int mathematics; int english; }; void input(struct student_information * p,int stu_num) { int i; for(i=0;i<stu_num;i++) scanf("%s %s %d %d %d", (p+i)->student_id,(p+i)->student_name, &(p+i)->chinese,&(p+i)->mathematics,&(p+i)->english); } void print(struct student_information *p,int std_num) { int i; for(i=0;i<std_num;i++) printf("%s,%s,%d,%d,%d",p[i].student_id,p[i].student_name, p[i].chinese,
p0p08 2019-08-18 09:12:27 |
要加头文件string.h
哪里错啊 #include "stdio.h" #include "malloc.h" typedef struct Student { char id[10]; char a[10]; int ch; int ma; int en; }Student; void input(int n,Student *li) { int i; for(i=0;i<n;i++) { scanf("%s %s %d %d %d",&li->id,&li->a,&li->ch,&li->ma,&li->en); } } void print(Student *li) { printf("%s,%s,%d,%d,%d\n",li->id,li->a,li->ch,li->ma,li->en); } void main() { Student *list; int n,i; scanf("%d",&n); if(n>0&&n<100) list = (Student *)malloc(sizeof(Student)*n); for(i=0;i<n;i++) { input(n,&list[i]); } for(i=0;i<n;i++) { print(&list[i]);
请问while(getchar() != '\n') continue; 这两行具体有什么作用,可以不加吗?
弈 2019-02-24 17:23:28 |
我的是这样#include "stdio.h" #include "malloc.h" typedef struct Student { char id[10]; char a[10]; int ch; int ma; int en; }Student; void input(int n,Student *li) { int i; for(i=0;i<n;i++) { scanf("%s %s %d %d %d",&li->id,&li->a,&li->ch,&li->ma,&li->en); } } void print(Student *li) { printf("%s,%s,%d,%d,%d\n",li->id,li->a,li->ch,li->ma,li->en); } void main() { Student *list; int n,i; scanf("%d",&n); if(n>0&&n<100) list = (Student *)malloc(sizeof(Student)*n); for(i=0;i<n;i++) { input(n,&list[i]); } for(i=0;i<n;i++) { print(&list[i]); } free(list); }
#include<stdlib.h> #include<stdio.h> struct stu{ char xuehao[20]; char name[20]; int a; int b; int c; struct stu *next; }; struct stu *input(int n){ struct stu *head,*pb,*pf,*p; for(int i=0;i<n;i++){ pb=(struct stu *)malloc(sizeof(struct stu)); scanf("%s%s%d%d%d",&pb->xuehao,&pb->name,&pb->a,&pb->b,&pb->c); if(i==0){ pf=head=pb; }else{ pf->next=pb; pb->next=NULL; pf=pb; } } return(head); } void print(struct stu *head){ struct stu *p; for(p=head;p!=NULL;p=p->next){ printf("%s,%s,%d,%d,%d\n",p->xuehao,p->name,p->a,p->b,p->c); } } void main(){ int n; scanf("
豆豆哦 2019-02-19 20:43:43 |
补充void main(){ int n; scanf("%d",&n); print(input(n)); }
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:696 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:438 |
分糖果 (C++代码)浏览:1438 |
ASCII帮了大忙浏览:748 |
淘淘的名单 (C语言代码)答案错误???浏览:593 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:661 |
校门外的树 (C语言代码)浏览:692 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:522 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:511 |
字符串的输入输出处理 (C语言代码)浏览:984 |
code 2021-12-05 11:11:31 |
你可以试一下把这个代码删掉,然后就会发现错误了