请帮我用C语言编写一个学生成绩统计管理的代码
时间: 2023-09-10 20:06:57 浏览: 98
学生成绩统计管理C语言实现源代码
3星 · 编辑精心推荐
好的,以下是一个简单的学生成绩统计管理程序的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_STUDENT_NUM 10
#define MAX_SUBJECT_NUM 5
typedef struct {
char name[20];
float score[MAX_SUBJECT_NUM];
float average_score;
} Student;
float calculate_average_score(float scores[], int count) {
float sum = 0;
for (int i = 0; i < count; i++) {
sum += scores[i];
}
return sum / count;
}
void calculate_all_average_scores(Student students[], int student_count) {
for (int i = 0; i < student_count; i++) {
students[i].average_score = calculate_average_score(students[i].score, MAX_SUBJECT_NUM);
}
}
void find_highest_score(Student students[], int student_count, char *subject_name) {
float max_score = -1;
int max_score_student_index = -1;
for (int i = 0; i < student_count; i++) {
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
if (strcmp(subject_name, "all") != 0 && strcmp(subject_name, "All") != 0 && strcmp(subject_name, "ALL") != 0 && strcmp(subject_name, "全部") != 0) {
if (strcmp(subject_name, "语文") == 0 && j != 0) {
continue;
} else if (strcmp(subject_name, "数学") == 0 && j != 1) {
continue;
} else if (strcmp(subject_name, "英语") == 0 && j != 2) {
continue;
} else if (strcmp(subject_name, "物理") == 0 && j != 3) {
continue;
} else if (strcmp(subject_name, "化学") == 0 && j != 4) {
continue;
}
}
if (students[i].score[j] > max_score) {
max_score = students[i].score[j];
max_score_student_index = i;
}
}
}
if (max_score_student_index != -1) {
printf("最高分数为%.2f,对应学生是%s,对应科目是%s\n", max_score, students[max_score_student_index].name, subject_name);
}
}
float calculate_average_score_variance(Student students[], int student_count) {
float sum = 0;
float average_scores[MAX_STUDENT_NUM];
for (int i = 0; i < student_count; i++) {
average_scores[i] = students[i].average_score;
sum += average_scores[i];
}
float average = sum / student_count;
float variance = 0;
for (int i = 0; i < student_count; i++) {
variance += pow(average_scores[i] - average, 2);
}
return variance / student_count;
}
int main() {
Student students[MAX_STUDENT_NUM];
int student_count = 0;
char subject_name[10];
printf("请输入学生个数(不超过%d个):", MAX_STUDENT_NUM);
scanf("%d", &student_count);
for (int i = 0; i < student_count; i++) {
printf("请输入第%d个学生的名字:", i + 1);
scanf("%s", students[i].name);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("请输入%s的%s成绩:", students[i].name, j == 0 ? "语文" : (j == 1 ? "数学" : (j == 2 ? "英语" : (j == 3 ? "物理" : "化学"))));
scanf("%f", &students[i].score[j]);
}
}
calculate_all_average_scores(students, student_count);
for (int i = 0; i < student_count; i++) {
printf("%s的平均分为%.2f\n", students[i].name, students[i].average_score);
}
printf("请输入要查询的科目名(输入“全部”或“all”或“All”或“ALL”查询所有科目):");
scanf("%s", subject_name);
find_highest_score(students, student_count, subject_name);
float variance = calculate_average_score_variance(students, student_count);
printf("平均分方差为%.2f\n", variance);
return 0;
}
```
该程序实现了以下功能:
1. 输入学生个数和每个学生的成绩,并计算每个学生的平均分;
2. 输出每个学生的平均分;
3. 输入要查询的科目名,输出该科目中所有学生的最高分数对应的学生和课程;
4. 计算所有学生的平均分方差,并输出结果。
请注意,该程序中的科目名只支持输入“语文”、“数学”、“英语”、“物理”和“化学”,如果需要增加或修改科目名,请修改find_highest_score函数中的代码。
阅读全文