C6-从键盘输入一组学生信息,计算每个学生3门课程的平均成绩,并按照平均成绩由高到低排序(输入以三门课的成绩都为0结束)

使用C语言编程,实现从键盘输入学生各科成绩,计算平均分,并按平均成绩降序排列。输入三门课程成绩全为0时结束输入。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#include<stdio.h>

struct Student
{
   
	int num;
	char name[20];
	float score[3],average
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以定义如下的结构体和共用体: ```c #include <stdio.h> #include <string.h> #define COURSE_NUM 7 // 定义学生信息结构体 struct student { long id; // 学号 char name[20]; // 姓名 int age; // 年龄 union { float course[COURSE_NUM]; // 课程成绩 struct { float c1, c2, c3, c4, c5, c6, c7; }; }; float total_score; // 总分 float avg_score; // 平均分 }; // 读入单个学生信息 void input_student(struct student *stu) { printf("请输入学生学号:"); scanf("%ld", &stu->id); printf("请输入学生姓名:"); scanf("%s", stu->name); printf("请输入学生年龄:"); scanf("%d", &stu->age); printf("请输入学生课程1成绩:"); scanf("%f", &stu->c1); printf("请输入学生课程2成绩:"); scanf("%f", &stu->c2); printf("请输入学生课程3成绩:"); scanf("%f", &stu->c3); printf("请输入学生课程4成绩:"); scanf("%f", &stu->c4); printf("请输入学生课程5成绩:"); scanf("%f", &stu->c5); printf("请输入学生课程6成绩:"); scanf("%f", &stu->c6); printf("请输入学生课程7成绩:"); scanf("%f", &stu->c7); } // 计算单个学生的总分和平均分 void calc_score(struct student *stu) { stu->total_score = stu->c1 + stu->c2 + stu->c3 + stu->c4 + stu->c5 + stu->c6 + stu->c7; stu->avg_score = stu->total_score / COURSE_NUM; } // 输出单个学生的信息 void print_student(struct student *stu) { printf("%ld\t%s\t%.2f\t%.2f\n", stu->id, stu->name, stu->total_score, stu->avg_score); } int main() { // 定义3个学生的数组 struct student students[3]; // 读入3个学生的信息 for (int i = 0; i < 3; i++) { printf("请输入第%d个学生的信息:\n", i + 1); input_student(&students[i]); calc_score(&students[i]); } // 输出所有学生的信息 printf("学号\t姓名\t总分\t平均分\n"); for (int i = 0; i < 3; i++) { print_student(&students[i]); } return 0; } ``` 这个程序中,我们首先定义了一个结构体`student`来存储每个学生的信息,其中包括学号、姓名、年龄、课程1~7的成绩、总分和平均分。为了方便计算总分和平均分,我们使用了一个共用体`course`来存储所有课程成绩,同时也定义了一个结构体`c1~c7`来访问每个课程成绩。在`input_student`函数中,我们通过指针来修改传入的学生信息,然后在`calc_score`函数中计算总分和平均分。最后,在`main`函数中,我们读入3个学生的信息,计算总分和平均分,然后输出所有学生的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向上Claire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值