用c语言设计一个信息管理系统,实现对学生姓名出生年月学号数学语文英语三门课成绩的输入(学生不超过四十人),同时实现对学生各科成绩的升序或者降序排列,输出学生个人总分和语文,数学,英语三门课的班级平均分,
时间: 2023-11-25 16:04:16 浏览: 187
用c语言实现的学生成绩管理信息系统
好的,以下是用C语言实现的学生信息管理系统。主要是通过结构体数组来存储学生信息,然后实现对学生信息的添加、排序和统计等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 40
struct Student {
char name[20];
char birth[20];
char id[20];
float math;
float chinese;
float english;
float total;
};
struct Student students[MAX_STUDENT_NUM];
int student_num = 0;
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数量已达上限,无法添加新学生!\n");
return;
}
struct Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生出生年月:");
scanf("%s", student.birth);
printf("请输入学生学号:");
scanf("%s", student.id);
printf("请输入学生数学成绩:");
scanf("%f", &student.math);
printf("请输入学生语文成绩:");
scanf("%f", &student.chinese);
printf("请输入学生英语成绩:");
scanf("%f", &student.english);
student.total = student.math + student.chinese + student.english;
students[student_num] = student;
student_num++;
printf("添加成功!\n");
}
int compare_student(const void* a, const void* b, void* key) {
int index = *(int*)key;
struct Student* sa = (struct Student*)a;
struct Student* sb = (struct Student*)b;
if (index < 0) {
return sa->total < sb->total ? -1 : 1;
} else {
float va = *(float*)((char*)sa + index);
float vb = *(float*)((char*)sb + index);
return va < vb ? -1 : 1;
}
}
void sort_students(int index, int reverse) {
qsort_r(students, student_num, sizeof(struct Student), compare_student, &index);
if (reverse) {
int i, j;
for (i = 0, j = student_num - 1; i < j; i++, j--) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
printf("姓名\t出生年月\t学号\t数学\t语文\t英语\t总分\n");
int i;
for (i = 0; i < student_num; i++) {
printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n", students[i].name, students[i].birth, students[i].id, students[i].math, students[i].chinese, students[i].english, students[i].total);
}
}
void calculate_avg() {
float math_sum = 0, chinese_sum = 0, english_sum = 0;
int i;
for (i = 0; i < student_num; i++) {
math_sum += students[i].math;
chinese_sum += students[i].chinese;
english_sum += students[i].english;
}
float math_avg = math_sum / student_num;
float chinese_avg = chinese_sum / student_num;
float english_avg = english_sum / student_num;
printf("数学平均分:%.1f,语文平均分:%.1f,英语平均分:%.1f\n", math_avg, chinese_avg, english_avg);
}
int main() {
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生信息\n");
printf("2. 按总分排序\n");
printf("3. 按数学成绩排序\n");
printf("4. 按语文成绩排序\n");
printf("5. 按英语成绩排序\n");
printf("6. 计算班级平均分\n");
printf("0. 退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 0:
printf("再见!\n");
return 0;
case 1:
add_student();
break;
case 2:
sort_students(-1, 0);
break;
case 3:
sort_students(offsetof(struct Student, math), 0);
break;
case 4:
sort_students(offsetof(struct Student, chinese), 0);
break;
case 5:
sort_students(offsetof(struct Student, english), 0);
break;
case 6:
calculate_avg();
break;
default:
printf("请输入正确的操作编号!\n");
break;
}
}
return 0;
}
```
注意,这里使用了C标准库中的`qsort_r`函数进行排序,这个函数比较复杂,需要传入一个自定义的比较函数和一个关键字参数。具体实现可以参考上面的代码。
另外,为了简化代码,这里没有对输入的数据进行格式检查和异常处理,实际开发中需要考虑这些情况。
阅读全文