ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-11 19:01:53  阅读:162  来源: 互联网

标签:并打印 int double len --- Stu score 键盘输入 sizeof


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

 

#include<stdio.h>
#include<malloc.h>
#include<assert.h>

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平均成绩: %f\n", 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("%f\n", 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;
}

结果如下:

 

 

标签:并打印,int,double,len,---,Stu,score,键盘输入,sizeof
来源: https://blog.csdn.net/qq_42795061/article/details/121876332

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有