栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

结构体练习---从键盘输入学生信息并打印指定学生信息

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

结构体练习---从键盘输入学生信息并打印指定学生信息

题目:有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号,姓名、3门课程成绩、平均分数)。

 

#include
#include
#include

struct Student
{
	int num;
	char name[20];
	double score[3];
};

Student* Input10(Student* Stu, int len)
{
	assert(Stu != NULL);
	printf("请输入学生信息,编号 姓名 课程1成绩,课程2成绩,课程3成绩n");
	for (int i = 0; i < len; i++)
	{
		scanf_s("%d %s", &Stu[i].num, &Stu[i].name, 20);
		for (int j = 0; j < 3; j++)
		{
			scanf_s("%lf", &Stu[i].score[j]);
		}

	}
	return Stu;
}
void Print10(Student* Stu, int len)
{
	int count = 0;
	double tmp = 0;
	double max = 0;
	double tmp1 = 0;
	double tmp2 = 0;
	double tmp3 = 0;
	double* arr = (double*)malloc(sizeof(double) * len);
	double* brr = (double*)malloc(sizeof(double) * len);
	double* crr = (double*)malloc(sizeof(double) * len);
	double* drr = (double*)malloc(sizeof(double) * len);
	for (int i = 0; i < len; i++)
	{	
		tmp1 += Stu[i].score[0];
		tmp2 += Stu[i].score[1];
		tmp3 += Stu[i].score[2];
		for (int j = 0; j < 3; j++)
		{
			tmp += Stu[i].score[j];			 
		}	
		arr[i] = tmp;
        tmp = 0;
	}
	for (int i = 0; i < len; i++)
	{
		if (max < arr[i])
		{
			max = arr[i];
			count = i;
		}
	}
	tmp1 = tmp1 / len;
	tmp2 = tmp2 / len;
	tmp3 = tmp3 / len;
	printf("课程1平均成绩:%f 课程2平均成绩: %f 课程3平均成绩: %fn", tmp1, tmp2, tmp3);
	printf("最高分学生数据为: %d %s ", Stu[count].num, Stu[count].name);
	for (int i = 0; i < 3; i++)
	{
		printf("%f ", Stu[count].score[i]);
	}
	printf("%fn", max/3);
 
 //释放动态内存
	free(arr);
	free(brr);
	free(crr);
	free(drr);
}
int main()
{
	
	Student Stu1[10] = { 0 };
	Input10(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));
	Print10(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));
	

	return 0;
}

结果如下:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号