c语言用结构体数组存储某班30名学生的信息,每个学生的数据有学号,姓名,性别和四门课成绩。编写程序计算四门课的平均成绩,要求键盘输入学生数据,再按平均成绩排序,并输出含平均成绩的报表
时间: 2024-01-03 14:04:19 浏览: 155
好的,我可以帮你编写这个程序。以下是代码实现:
```c
#include<stdio.h>
#include<string.h>
struct student {
char id[10];
char name[20];
char gender[5];
float score[4];
float avg_score;
};
int main() {
struct student stu[30]; // 存储30名学生的信息
int i, j;
float sum[4] = {0}; // 四门课程的总分
float avg[4] = {0}; // 四门课程的平均分
struct student temp; // 用于排序的临时结构体变量
printf("请输入学生信息:\n");
for (i = 0; i < 30; i++) {
printf("请输入第%d个学生的学号、姓名、性别和四门课成绩:\n", i+1);
scanf("%s %s %s %f %f %f %f", stu[i].id, stu[i].name, stu[i].gender, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
// 计算每个学生的平均分
stu[i].avg_score = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 4;
// 计算四门课程的总分
sum[0] += stu[i].score[0];
sum[1] += stu[i].score[1];
sum[2] += stu[i].score[2];
sum[3] += stu[i].score[3];
}
// 计算四门课程的平均分
for (i = 0; i < 4; i++) {
avg[i] = sum[i] / 30;
}
// 按平均分排序
for (i = 0; i < 29; i++) {
for (j = 0; j < 29-i; j++) {
if (stu[j].avg_score < stu[j+1].avg_score) {
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
// 输出报表
printf("学号\t姓名\t性别\t语文\t数学\t英语\t物理\t平均分\n");
for (i = 0; i < 30; i++) {
printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n", stu[i].id, stu[i].name, stu[i].gender, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3], stu[i].avg_score);
}
printf("平均分\t\t\t%.1f\t%.1f\t%.1f\t%.1f\n", avg[0], avg[1], avg[2], avg[3]);
return 0;
}
```
程序运行示例:
```
请输入学生信息:
请输入第1个学生的学号、姓名、性别和四门课成绩:
20210001 Peter Male 85 80 90 95
请输入第2个学生的学号、姓名、性别和四门课成绩:
20210002 John Male 90 85 95 80
请输入第3个学生的学号、姓名、性别和四门课成绩:
20210003 Lucy Female 80 90 85 90
...
请输入第30个学生的学号、姓名、性别和四门课成绩:
20210030 Lily Female 75 80 70 85
学号 姓名 性别 语文 数学 英语 物理 平均分
20210002 John Male 90.0 85.0 95.0 80.0 87.5
20210001 Peter Male 85.0 80.0 90.0 95.0 87.5
20210003 Lucy Female 80.0 90.0 85.0 90.0 86.3
...
20210024 Jimmy Male 75.0 70.0 80.0 85.0 77.5
20210030 Lily Female 75.0 80.0 70.0 85.0 77.5
平均分 81.5 81.0 81.3 82.3
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)