应用《C语言程序设计》和《数据结构》课程的内容,编写一个完整的学生成绩管理系统程序,实现以下题目的要求: 1.学生信息包括学号、姓名、3 门考试成绩 2.学生成绩录入功能(成绩信息用文件保存) 3.统计所有学生的总分及平均分 4.统计所有课程的平均分 5.按平均分进行排序 6.输入一个学生学号输出其各科成绩及平均成绩 7.修改、删除学生信息
时间: 2024-02-11 20:08:51 浏览: 100
好的,这是一个比较复杂的程序,需要涉及到文件的读写、数据结构的使用等内容。下面是一个简单的示例程序,可以供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENTS 100
typedef struct {
int id;
char name[MAX_NAME_LEN];
int scores[3];
float total_score;
float avg_score;
} Student;
int read_students(Student students[]) {
FILE *fp;
int i = 0;
char filename[] = "students.dat";
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Cannot open file %s\n", filename);
return 0;
}
while (fread(&students[i], sizeof(Student), 1, fp) == 1) {
i++;
}
fclose(fp);
return i;
}
void write_students(Student students[], int num_students) {
FILE *fp;
char filename[] = "students.dat";
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Cannot open file %s\n", filename);
return;
}
fwrite(students, sizeof(Student), num_students, fp);
fclose(fp);
}
void add_student(Student students[], int *num_students) {
if (*num_students >= MAX_STUDENTS) {
printf("Cannot add more students\n");
return;
}
printf("Enter student ID: ");
scanf("%d", &students[*num_students].id);
printf("Enter student name: ");
scanf("%s", students[*num_students].name);
printf("Enter scores for three exams:\n");
scanf("%d %d %d", &students[*num_students].scores[0], &students[*num_students].scores[1], &students[*num_students].scores[2]);
students[*num_students].total_score = students[*num_students].scores[0] + students[*num_students].scores[1] + students[*num_students].scores[2];
students[*num_students].avg_score = students[*num_students].total_score / 3.0;
(*num_students)++;
write_students(students, *num_students);
}
void print_student(Student student) {
printf("%d\t%s\t%d\t%d\t%d\t%.2f\t%.2f\n", student.id, student.name, student.scores[0], student.scores[1], student.scores[2], student.total_score, student.avg_score);
}
void print_all_students(Student students[], int num_students) {
int i;
printf("ID\tName\tExam 1\tExam 2\tExam 3\tTotal\tAverage\n");
for (i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void compute_scores(Student students[], int num_students) {
int i, j;
float exam_totals[3] = {0.0};
float class_total = 0.0;
for (i = 0; i < num_students; i++) {
class_total += students[i].total_score;
for (j = 0; j < 3; j++) {
exam_totals[j] += students[i].scores[j];
}
}
printf("Class average:\n");
for (j = 0; j < 3; j++) {
printf("Exam %d: %.2f\n", j+1, exam_totals[j] / num_students);
}
printf("Total: %.2f\n", class_total / num_students);
}
void sort_by_average(Student students[], int num_students) {
int i, j;
Student temp;
for (i = 0; i < num_students-1; i++) {
for (j = i+1; j < num_students; j++) {
if (students[i].avg_score < students[j].avg_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void find_student(Student students[], int num_students) {
int id, i;
printf("Enter student ID: ");
scanf("%d", &id);
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("ID\tName\tExam 1\tExam 2\tExam 3\tTotal\tAverage\n");
print_student(students[i]);
return;
}
}
printf("No student with ID %d found\n", id);
}
void modify_student(Student students[], int num_students) {
int id, i;
printf("Enter student ID: ");
scanf("%d", &id);
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Enter new scores for three exams:\n");
scanf("%d %d %d", &students[i].scores[0], &students[i].scores[1], &students[i].scores[2]);
students[i].total_score = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];
students[i].avg_score = students[i].total_score / 3.0;
write_students(students, num_students);
printf("Student %d updated\n", id);
return;
}
}
printf("No student with ID %d found\n", id);
}
void delete_student(Student students[], int *num_students) {
int id, i, j;
printf("Enter student ID: ");
scanf("%d", &id);
for (i = 0; i < *num_students; i++) {
if (students[i].id == id) {
for (j = i; j < *num_students-1; j++) {
students[j] = students[j+1];
}
(*num_students)--;
write_students(students, *num_students);
printf("Student %d deleted\n", id);
return;
}
}
printf("No student with ID %d found\n", id);
}
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
num_students = read_students(students);
while (1) {
printf("Choose an option:\n");
printf("1. Add a student\n");
printf("2. Print all students\n");
printf("3. Compute scores\n");
printf("4. Sort by average\n");
printf("5. Find a student\n");
printf("6. Modify a student\n");
printf("7. Delete a student\n");
printf("8. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
print_all_students(students, num_students);
break;
case 3:
compute_scores(students, num_students);
break;
case 4:
sort_by_average(students, num_students);
print_all_students(students, num_students);
break;
case 5:
find_student(students, num_students);
break;
case 6:
modify_student(students, num_students);
break;
case 7:
delete_student(students, &num_students);
break;
case 8:
write_students(students, num_students);
exit(0);
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
```
这段代码实现了一个简单的学生成绩管理系统,包含了添加学生、打印所有学生、统计所有学生的总分及平均分、按平均分进行排序、输入一个学生学号输出其各科成绩及平均成绩、修改、删除学生信息等功能。代码中使用了结构体来存储学生信息,使用了文件来保存数据,并且使用了一些简单的数据结构和算法来实现排序等功能。
阅读全文