解题思路:

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;
}


点赞(13)
 

0.0分

50 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 37 条评论

豆豆哦 6年前 回复TA
#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("
阿啸 6年前 回复TA
#include<stdio.h>
#define M 100
struct stu
{
    char num[M];
    char name[M];
    int score[3];
} stu1[M];
int input(struct stu stu1[],int N)
{
    int i;
    for(i=0; i<N; i++)
        scanf("%s%s%d%d%d",&stu1[i].num,&stu1[i].name,&stu1[i].score[0],&stu1[i].score[1],&stu1[i].score[2]);
    return 0;
}
int output(struct stu stu1[],int N)
{
    int i;
    for(i=0; i<N; i++)
        printf("%s,%s,%d,%d,%d\n",stu1[i].num,stu1[i].name,stu1[i].score[0],stu1[i].score[1],stu1[i].score[2]);
    return 0;
}
int main()
{
    int N;
    scanf("%d",&N);
    input(stu1,N);
    output(stu1,N);
    return
核弹头 6年前 回复TA
@核弹头 感觉我的做法还是有点麻烦大佬有什么改进意见没
核弹头 6年前 回复TA
#include <stdio.h>、
struct student
{
	char num[10];
	char name[20];
	int w,x,y;
};
void input(struct student *d,int n);
void print(struct student *s,int n);
int main()
{
	struct student *p;
	int n;
	scanf("%d",&n);
	p=(struct student *)malloc(n*sizeof(struct student));
	input(p,n);
	print(p,n);
	free(p);
	return 0;
}
void input(struct student *d,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		scanf("%s %s %d %d %d",d[i].num,d[i].name,&d[i].w,&d[i].x,&d[i].y);
	}
}
void print(struct student *s,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
	printf("%s,%s,%d,%d,%d\n",s[i].num,s[i].name,s[i].w,s[i].x,s[i].y);
	}
Christ 6年前 回复TA
@Christ 尽管这个时候我已经解决这个问题了,但还是谢谢你的热心
零K沁雪 6年前 回复TA
@Christ 首先你的name是个字符串,你应该用%s接收,你却用了%c。%c是接受单个字符的。你的num和name已经是字符串了,不应该用&
Christ 6年前 回复TA
#include<stdio.h>
int main()
{
	int n, i;
	struct student
	{
		char num[22];
		char name[22];
		int p1, p2, p3;
	};
	struct student stu[33];
	scanf_s("%d", &n);
	for (i = 0; i < n; i ++ )
	{
		scanf_s("%s%c%d%d%d", &stu[i].num, &stu[i].name, &stu[i].p1, &stu[i].p2, &stu[i].p3);
	}
	for (i = 0; i < n; i++)
	{
		printf("%s,%c,%d,%d,%d\n", stu[i].num, stu[i].name, stu[i].p1, stu[i].p2, stu[i].p3);
	}
}


谁帮我看看问题在哪?谢了!