c语言用结构体编程。某班有5名学生,每名学生的数据包括学号、姓名、了 门课程的成绩。从键盘输入5名学生数据,输出5名同学平均成绩,以及平均分最高的学生的数据 (包括学号、姓名、了门课成绩、平均成绩)
时间: 2023-06-24 13:06:05 浏览: 55
以下是用 C 语言结构体编程实现上述问题的代码:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 5
#define MAX_NAME_LENGTH 50
struct Student {
int id;
char name[MAX_NAME_LENGTH];
float grades[3];
float avg_grade;
};
void input_student_data(struct Student students[], int num_students) {
for (int i = 0; i < num_students; i++) {
printf("Enter data for student %d:\n", i+1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Grades (separated by spaces): ");
scanf("%f %f %f", &students[i].grades[0], &students[i].grades[1], &students[i].grades[2]);
students[i].avg_grade = (students[i].grades[0] + students[i].grades[1] + students[i].grades[2]) / 3.0;
}
}
void print_avg_grade(struct Student students[], int num_students) {
float sum = 0.0;
for (int i = 0; i < num_students; i++) {
sum += students[i].avg_grade;
}
printf("Average grade of all students: %.2f\n", sum / num_students);
}
void print_highest_avg_grade(struct Student students[], int num_students) {
int highest_index = 0;
float highest_avg_grade = students[0].avg_grade;
for (int i = 1; i < num_students; i++) {
if (students[i].avg_grade > highest_avg_grade) {
highest_index = i;
highest_avg_grade = students[i].avg_grade;
}
}
printf("\nStudent with highest average grade:\n");
printf("ID: %d\n", students[highest_index].id);
printf("Name: %s\n", students[highest_index].name);
printf("Grades: %.2f %.2f %.2f\n", students[highest_index].grades[0], students[highest_index].grades[1], students[highest_index].grades[2]);
printf("Average grade: %.2f\n", students[highest_index].avg_grade);
}
int main() {
struct Student students[NUM_STUDENTS];
input_student_data(students, NUM_STUDENTS);
print_avg_grade(students, NUM_STUDENTS);
print_highest_avg_grade(students, NUM_STUDENTS);
return 0;
}
```
代码中定义了一个 `Student` 结构体,包含学号、姓名、三门课程的成绩和平均成绩。`input_student_data` 函数从键盘输入学生数据,并计算每个学生的平均成绩。`print_avg_grade` 函数计算所有学生的平均成绩,并输出到屏幕上。`print_highest_avg_grade` 函数找到平均成绩最高的学生,并输出该学生的数据。最后在 `main` 函数中调用这些函数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)