现有10名学生,每名学生的信息包括:学号、姓名、数学、外语和程序设计基础3门课程的成绩。本次综合训练任务要求如下:1.用结构体存放学生信息;2.从键盘输人10个学生数据;3.输出3门课程总平均成绩;4.最高分的学生的数据用c语言
时间: 2024-12-16 11:21:28 浏览: 4
在C语言中,我们可以使用结构体(struct)来存储学生的个人信息,以及定义函数来完成输入、计算平均分和找到最高分学生等任务。下面是一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
// 定义一个结构体表示学生信息
typedef struct {
int student_id;
char name[50];
int math_score;
int foreign_language_score;
int programming_score;
} Student;
// 函数声明
void input_student_data(Student students[], int num_students);
double calculate_average(int scores[]);
void find_highest_student(Student students[], int num_students);
int main() {
Student students[10]; // 存放10名学生的信息
int num_students = 10;
// 输入学生数据
input_student_data(students, num_students);
// 计算并输出平均成绩
printf("Average scores for Math, Foreign Language, and Programming:\n");
double avg_scores[] = calculate_average(&students[0].math_score, &students[0].foreign_language_score, &students[0].programming_score);
for (int i = 0; i < 3; i++) {
printf("Course %d average: %.2f\n", i + 1, avg_scores[i]);
}
// 找到最高分的学生
printf("\nHighest scoring student details:\n");
find_highest_student(students, num_students);
return 0;
}
// 输入函数
void input_student_data(Student students[], int num_students) {
for (int i = 0; i < num_students; i++) {
scanf("%d", &students[i].student_id);
getchar(); // 消耗回车符
scanf("%s", students[i].name);
scanf("%d%d%d", &students[i].math_score, &students[i].foreign_language_score, &students[i].programming_score);
}
}
// 平均分计算函数
double calculate_average(int* scores, ...) {
va_list args;
double sum = 0;
int count = 0;
va_start(args, scores); // 获取可变参数列表
while (scores != NULL) {
sum += *scores++;
count++;
}
va_end(args);
return sum / count;
}
// 找最高分学生函数
void find_highest_student(Student students[], int num_students) {
int highest_score = students[0].math_score, max_index = 0;
for (int i = 1; i < num_students; i++) {
if (students[i].math_score > highest_score) {
highest_score = students[i].math_score;
max_index = i;
}
}
printf("Student with highest Math score: ID: %d, Name: %s, Score: %d\n",
students[max_index].student_id, students[max_index].name, highest_score);
}
```
阅读全文