用C语言代码设计一个学生成绩查询统计系统
时间: 2023-06-17 15:05:44 浏览: 128
C语言课程设计——学生成绩管理系统(源代码+详细注释).zip
5星 · 资源好评率100%
好的,以下是一个简单的学生成绩查询统计系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 20
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
int score;
} Student;
void input_students(Student students[], int *num_students);
void print_students(Student students[], int num_students);
float calculate_average_score(Student students[], int num_students);
int find_highest_score(Student students[], int num_students);
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
float average_score;
int highest_score_index;
input_students(students, &num_students);
printf("\n");
print_students(students, num_students);
average_score = calculate_average_score(students, num_students);
printf("\nThe average score is: %.2f\n", average_score);
highest_score_index = find_highest_score(students, num_students);
printf("\nThe student with the highest score is %s with a score of %d\n", students[highest_score_index].name, students[highest_score_index].score);
return 0;
}
void input_students(Student students[], int *num_students) {
printf("Enter the number of students: ");
scanf("%d", num_students);
for (int i = 0; i < *num_students; i++) {
printf("\nEnter the name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter the ID of student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter the score of student %d: ", i + 1);
scanf("%d", &students[i].score);
}
}
void print_students(Student students[], int num_students) {
printf("Name\tID\tScore\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%d\n", students[i].name, students[i].id, students[i].score);
}
}
float calculate_average_score(Student students[], int num_students) {
float sum = 0;
for (int i = 0; i < num_students; i++) {
sum += students[i].score;
}
return sum / num_students;
}
int find_highest_score(Student students[], int num_students) {
int highest_score_index = 0;
for (int i = 1; i < num_students; i++) {
if (students[i].score > students[highest_score_index].score) {
highest_score_index = i;
}
}
return highest_score_index;
}
```
该程序定义了一个 `Student` 结构体,其中包含学生的姓名、ID 和成绩信息。程序还定义了一些帮助函数,包括输入学生信息、打印学生信息、计算平均分和查找最高分的函数。在 `main` 函数中,程序首先调用 `input_students` 函数以输入学生信息,然后调用 `print_students` 函数以打印学生信息。接着,程序调用 `calculate_average_score` 函数以计算平均分,并打印该值。最后,程序调用 `find_highest_score` 函数以查找最高分的学生,并打印该学生的姓名和分数。
阅读全文