在c语言中.从键盘输入9个学生的数据,包括:学号、姓名、性别、年龄、身高、体重,和体育课百米、跳远、跳高三项目的成绩。要求输出9人各项总平均成绩,及最高成绩学生的数据(学号、姓名、三项目成绩)。
时间: 2023-12-16 19:28:31 浏览: 69
以下是C语言实现从键盘输入9个学生数据并计算各项总平均成绩以及最高成绩学生数据的代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
char id[20];
char name[20];
char sex[5];
int age;
float height;
float weight;
float sprint;
float long_jump;
float high_jump;
float total_score;
};
int main() {
struct student stu[9];
float sprint_sum = 0, long_jump_sum = 0, high_jump_sum = 0, total_score_sum = 0;
float sprint_avg, long_jump_avg, high_jump_avg, total_score_avg;
int max_index = 0;
float max_score = 0;
// 从键盘输入9个学生的数据
for (int i = 0; i < 9; i++) {
printf("请输入第%d个学生的数据:\n", i + 1);
printf("学号:");
scanf("%s", stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("性别:");
scanf("%s", stu[i].sex);
printf("年龄:");
scanf("%d", &stu[i].age);
printf("身高:");
scanf("%f", &stu[i].height);
printf("体重:");
scanf("%f", &stu[i].weight);
printf("百米成绩:");
scanf("%f", &stu[i].sprint);
printf("跳远成绩:");
scanf("%f", &stu[i].long_jump);
printf("跳高成绩:");
scanf("%f", &stu[i].high_jump);
// 计算总成绩
stu[i].total_score = stu[i].sprint + stu[i].long_jump + stu[i].high_jump;
// 计算各项总分数
sprint_sum += stu[i].sprint;
long_jump_sum += stu[i].long_jump;
high_jump_sum += stu[i].high_jump;
total_score_sum += stu[i].total_score;
// 找到最高成绩学生的数据
if (stu[i].total_score > max_score) {
max_score = stu[i].total_score;
max_index = i;
}
}
// 计算各项平均分数
sprint_avg = sprint_sum / 9;
long_jump_avg = long_jump_sum / 9;
high_jump_avg = high_jump_sum / 9;
total_score_avg = total_score_sum / 9;
// 输出各项平均分数
printf("各项平均分数:\n");
printf("百米平均成绩:%.2f\n", sprint_avg);
printf("跳远平均成绩:%.2f\n", long_jump_avg);
printf("跳高平均成绩:%.2f\n", high_jump_avg);
printf("总平均成绩:%.2f\n", total_score_avg);
// 输出最高成绩学生的数据
printf("最高成绩学生的数据:\n");
printf("学号:%s\n", stu[max_index].id);
printf("姓名:%s\n", stu[max_index].name);
printf("百米成绩:%.2f\n", stu[max_index].sprint);
printf("跳远成绩:%.2f\n", stu[max_index].long_jump);
printf("跳高成绩:%.2f\n", stu[max_index].high_jump);
return 0;
}
```
阅读全文