C语言——>用结构体和函数处理学生成绩

  1. 用input函数来输入数据和求各学生的平均成绩
  2. 用max函数来找平均分最高的学生
  3. 用print函数输出成绩最高学生的信息

 

#include <stdio.h>
#define N 3     //学生数为3
struct Student {       //建立结构体类型
	int num;           //学号
	char name[20];      //姓名  
	float score[3];      //3门课成绩
	float aver;         //平均分
};


int main()

{
	void input(struct Student stu[]);       //函数声明
	struct Student max(struct Student stu[]);    //函数声明
	void print(struct Student stud);              //函数声明


	struct Student stu[N], *p = stu;       //定义结构体数组和结构体指针
	input(p);                                //调用函数
	print(max(p));
	
	return 0;

}
void input(struct Student stu[])       //定义输入函数
{
	int i;
	printf("请输入各个学生的信息:学号、姓名、3门课成绩:\n");
	for (i = 0; i < N; i++)
	{
		scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;  //根据输入成绩求平均分


	}

}

struct Student max(struct Student stu[])
{
	int i, m=0;                      //用m存放成绩最高的学生在数组中的序号
	for (i = 0; i < N; i++)
		if (stu[i].aver > stu[m].aver)            //找出平均分最高的学生在数组中的序号
			m = i;
	return stu[m];                 //返回包含该生信息的结构体元素
}

void print(struct Student stud)       //定义输出函数
{
	printf("\n 成绩最高的学生是:\n");
	printf("学号:%d\n 姓名:%s\n 三门课成绩:%5.lf,%5.lf,%5.lf\n 平均成绩:%5.lf\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver);



}

 

 

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值