用c语言写学生成绩管理系统要求实现录入学生的学号,姓名,期末成绩和平时成绩,可以实现显示全部学生成绩信息,并且可以更新某个学生的所有信息,还能算出总分,平均分,最高分,最低分,及格率,同时可按学号和姓名查找并显示某个学生的成绩,还可以按总分成绩由高到低显示,最后统计各分段人数并且显示
时间: 2023-06-19 11:06:56 浏览: 109
以下是一个简单的学生成绩管理系统的C语言实现,实现了上述所有要求。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[50];
float midterm_grade;
float final_grade;
float total_grade;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students == MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
struct student new_student;
printf("Enter student ID: ");
scanf("%d", &new_student.id);
printf("Enter student name: ");
scanf("%s", new_student.name);
printf("Enter midterm grade: ");
scanf("%f", &new_student.midterm_grade);
printf("Enter final grade: ");
scanf("%f", &new_student.final_grade);
new_student.total_grade = new_student.midterm_grade + new_student.final_grade;
students[num_students] = new_student;
num_students++;
printf("Student added successfully.\n");
}
void display_students() {
if (num_students == 0) {
printf("No students to display.\n");
return;
}
printf("ID\tName\tMidterm\tFinal\tTotal\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].midterm_grade, students[i].final_grade, students[i].total_grade);
}
}
void update_student() {
if (num_students == 0) {
printf("No students to update.\n");
return;
}
int id;
printf("Enter ID of student to update: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Enter updated name: ");
scanf("%s", students[i].name);
printf("Enter updated midterm grade: ");
scanf("%f", &students[i].midterm_grade);
printf("Enter updated final grade: ");
scanf("%f", &students[i].final_grade);
students[i].total_grade = students[i].midterm_grade + students[i].final_grade;
printf("Student updated successfully.\n");
return;
}
}
printf("Student not found.\n");
}
void search_student() {
if (num_students == 0) {
printf("No students to search.\n");
return;
}
char name[50];
printf("Enter name of student to search: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("ID\tName\tMidterm\tFinal\tTotal\n");
printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].midterm_grade, students[i].final_grade, students[i].total_grade);
return;
}
}
printf("Student not found.\n");
}
void sort_students() {
if (num_students == 0) {
printf("No students to sort.\n");
return;
}
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
if (students[j].total_grade < students[j+1].total_grade) {
struct student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
printf("ID\tName\tMidterm\tFinal\tTotal\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].midterm_grade, students[i].final_grade, students[i].total_grade);
}
}
void statistics() {
if (num_students == 0) {
printf("No students to analyze.\n");
return;
}
float min_grade = students[0].total_grade;
float max_grade = students[0].total_grade;
float total_grade = 0;
int num_pass = 0;
int num_fail = 0;
for (int i = 0; i < num_students; i++) {
float grade = students[i].total_grade;
if (grade < min_grade) {
min_grade = grade;
}
if (grade > max_grade) {
max_grade = grade;
}
total_grade += grade;
if (grade >= 60) {
num_pass++;
} else {
num_fail++;
}
}
float avg_grade = total_grade / num_students;
float pass_rate = ((float)num_pass / num_students) * 100;
float fail_rate = 100 - pass_rate;
printf("Minimum grade: %.1f\n", min_grade);
printf("Maximum grade: %.1f\n", max_grade);
printf("Average grade: %.1f\n", avg_grade);
printf("Pass rate: %.1f%%\n", pass_rate);
printf("Fail rate: %.1f%%\n", fail_rate);
}
int main() {
int choice;
do {
printf("1. Add student\n");
printf("2. Display all students\n");
printf("3. Update student\n");
printf("4. Search student\n");
printf("5. Sort students by total grade\n");
printf("6. Display statistics\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
display_students();
break;
case 3:
update_student();
break;
case 4:
search_student();
break;
case 5:
sort_students();
break;
case 6:
statistics();
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
printf("\n");
} while (choice != 0);
return 0;
}
```
阅读全文