题目七:学生成绩管理系统设计
学生成绩信息包括:学期,学号,班别,姓名,四门课程成绩(语文、数学、英语和计算机)等。
主要功能:
- 能按学期、按班级完成对学生成绩的录入、修改。
- 能按班级统计学生的成绩,求学生的总分及平均分。
- 能查询学生成绩,不及格科目及学生名单。
- 能按班级输出学生的成绩单。
要求: 使用二进制文件方式存储数据,系统以菜单方式工作。
studentPerformance.h
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STUDENTS_FILE "students.dat"
#define MAX_STUDENTS 100
typedef struct {
char term[20]; // 学期
char student_id[20]; // 学号
char class_name[20]; // 班级
char name[50]; // 姓名
int chinese; // 语文成绩
int math; // 数学成绩
int english; // 英语成绩
int computer; // 计算机成绩
} Student;
// 保存学生信息
void saveStudentsToFile(Student students[], int count);
// 加载学生信息
void loadStudentsFromFile(Student students[], int* count);
// 添加学生信息
void addStudent(Student students[], int* count);
// 修改学生信息
void modifyStudent(Student students[], int count);
// 统计学生成绩
void calculateStatistics(Student students[], int count);
// 查询学生成绩
void searchStudent(Student students[], int count);
// 输出学生成绩单
void printReportCard(Student students[], int count);
// 函数菜单
void mainMenu(Student students[], int* count);
studentPerformance.c
#include"studentPerformance.h"
// 确定Win/Linux操作系统,清屏
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 保存学生信息
void saveStudentsToFile(Student students[], int count) {
FILE* file = fopen(STUDENTS_FILE, "wb");
if (file == NULL) {
printf("无法打开文件 %s!\n", STUDENTS_FILE);
return;
}
fwrite(students, sizeof(Student), count, file);
fclose(file);
}
// 加载学生信息
void loadStudentsFromFile(Student students[], int* count) {
FILE* file = fopen(STUDENTS_FILE, "rb");
if (file == NULL) {
printf("无法打开文件 %s!\n", STUDENTS_FILE);
*count = 0;
return;
}
size_t numRead = fread(students, sizeof(Student), MAX_STUDENTS, file);
*count = (int)numRead;
fclose(file);
}
// 添加学生信息
void addStudent(Student students[], int* count) {
if (*count >= MAX_STUDENTS) {
printf("学生信息已满,无法继续添加。\n");
return;
}
Student newStudent;
printf("请输入学期: ");
scanf("%s", newStudent.term);
printf("请输入学号: ");
scanf("%s", newStudent.student_id);
printf("请输入班级: ");
scanf("%s", newStudent.class_name);
printf("请输入姓名: ");
scanf("%s", newStudent.name);
printf("请输入语文成绩: ");
scanf("%d", &newStudent.chinese);
printf("请输入数学成绩: ");
scanf("%d", &newStudent.math);
printf("请输入英语成绩: ");
scanf("%d", &newStudent.english);
printf("请输入计算机成绩: ");
scanf("%d", &newStudent.computer);
students[*count] = newStudent;
(*count)++;
saveStudentsToFile(students, *count);
printf("学生信息已保存。\n");
}
// 修改学生信息
void modifyStudent(Student students[], int count) {
char student_id[20];
printf("请输入要修改的学号: ");
scanf("%s", student_id);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].student_id, student_id) == 0) {
printf("找到的学生信息:\n");
printf("学期: %s\n", students[i].term);
printf("学号: %s\n", students[i].student_id);
printf("班级: %s\n", students[i].class_name);
printf("姓名: %s\n", students[i].name);
printf("语文成绩: %d\n", students[i].chinese);
printf("数学成绩: %d\n", students[i].math);
printf("英语成绩: %d\n", students[i].english);
printf("计算机成绩: %d\n", students[i].computer);
printf("请输入新的学期: ");
scanf("%s", students[i].term);
printf("请输入新的班级: ");
scanf("%s", students[i].class_name);
printf("请输入新的姓名: ");
scanf("%s", students[i].name);
printf("请输入新的语文成绩: ");
scanf("%d", &students[i].chinese);
printf("请输入新的数学成绩: ");
scanf("%d", &students[i].math);
printf("请输入新的英语成绩: ");
scanf("%d", &students[i].english);
printf("请输入新的计算机成绩: ");
scanf("%d", &students[i].computer);
saveStudentsToFile(students, count);
printf("学生信息已更新。\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该学号的学生信息。\n");
}
}
// 统计学生成绩
void calculateStatistics(Student students[], int count) {
char class_name[20];
printf("请输入要统计的班级: ");
scanf("%s", class_name);
int totalStudents = 0;
double totalScore = 0;
double averageScore = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].class_name, class_name) == 0) {
int total = students[i].chinese + students[i].math + students[i].english + students[i].computer;
totalScore += total;
totalStudents++;
}
}
if (totalStudents > 0) {
averageScore = totalScore / totalStudents;
printf("班级 %s 的总人数: %d\n", class_name, totalStudents);
printf("班级 %s 的总分: %.2f\n", class_name, totalScore);
printf("班级 %s 的平均分: %.2f\n", class_name, averageScore);
}
else {
printf("未找到该班级的学生信息。\n");
}
}
// 查询学生成绩
void searchStudent(Student students[], int count) {
char student_id[20];
printf("请输入要查询的学号: ");
scanf("%s", student_id);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(students[i].student_id, student_id) == 0) {
printf("\n找到的学生信息:\n");
printf("学期: %s\n", students[i].term);
printf("学号: %s\n", students[i].student_id);
printf("班级: %s\n", students[i].class_name);
printf("姓名: %s\n", students[i].name);
printf("语文成绩: %d\n", students[i].chinese);
printf("数学成绩: %d\n", students[i].math);
printf("英语成绩: %d\n", students[i].english);
printf("计算机成绩: %d\n", students[i].computer);
int total = students[i].chinese + students[i].math + students[i].english + students[i].computer;
printf("总分: %d\n", total);
printf("不及格科目:\n");
if (students[i].chinese < 60) printf("语文\n");
if (students[i].math < 60) printf("数学\n");
if (students[i].english < 60) printf("英语\n");
if (students[i].computer < 60) printf("计算机\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该学号的学生信息。\n");
}
}
// 输出学生成绩单
void printReportCard(Student students[], int count) {
char class_name[20];
printf("请输入要输出成绩单的班级: ");
scanf("%s", class_name);
int found = 0;
printf("\n+-----------------------------------+\n");
printf("| 学生成绩单 |\n");
printf("+-----------------------------------+\n");
printf("| 学期 | 学号 | 班级 | 姓名 | 语文 | 数学 | 英语 | 计算机 |\n");
printf("+-----------------------------------+\n");
for (int i = 0; i < count; i++) {
if (strcmp(students[i].class_name, class_name) == 0) {
printf("| %-6s | %-6s | %-6s | %-6s | %-3d | %-3d | %-3d | %-3d |\n",
students[i].term, students[i].student_id, students[i].class_name,
students[i].name, students[i].chinese, students[i].math,
students[i].english, students[i].computer);
found = 1;
}
}
if (!found) {
printf("未找到该班级的学生信息。\n");
}
printf("+-----------------------------------+\n");
}
// 函数菜单
void mainMenu(Student students[], int* count) {
int choice;
do {
clearScreen();
printf("学生成绩管理系统\n");
printf("1. 录入学生成绩\n");
printf("2. 修改学生成绩\n");
printf("3. 统计学生成绩\n");
printf("4. 查询学生成绩\n");
printf("5. 输出学生成绩单\n");
printf("6. 退出系统\n");
printf("请选择功能: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, count);
break;
case 2:
modifyStudent(students, *count);
break;
case 3:
calculateStatistics(students, *count);
break;
case 4:
searchStudent(students, *count);
break;
case 5:
printReportCard(students, *count);
break;
case 6:
printf("退出系统。\n");
break;
default:
printf("无效的选择,请重新输入。\n");
break;
}
getchar(); // 清除输入缓冲区中的换行符
system("pause"); // 暂停程序,等待用户按键
} while (choice != 6);
}
test.c
#include"studentPerformance.h"
int main() {
int count = 0;
Student students[MAX_STUDENTS];
loadStudentsFromFile(students, &count);
mainMenu(students, &count);
saveStudentsToFile(students, count);
return 0;
}