1、编写一个菜单驱动的学生成绩管理程序。实现如下管理功能: 能输入并显示 n 个学生的 m 门考试科目的成绩、总分和平均分。 按总分进行排序。 按学号进行排序。 任意输入一个学号,能显示该学生的姓名、各门功课的成绩。 将输入的学生成绩数据保存到文件中。 从文件中读出学生成绩数据。
时间: 2024-02-28 18:53:54 浏览: 141
好的,那么以下是一个简单的C语言代码示例,实现了您提出的这些功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM_STUDENTS 100
#define MAX_NAME_LENGTH 50
#define MAX_NUM_SUBJECTS 10
// 定义学生结构体
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
int scores[MAX_NUM_SUBJECTS];
int total_score;
float avg_score;
} Student;
// 定义全局变量
Student students[MAX_NUM_STUDENTS];
int num_students = 0;
int num_subjects = 0;
// 函数声明
void input_scores();
void sort_by_total_score();
void sort_by_id();
void display_student();
void save_scores();
void read_scores();
// 主函数
int main() {
int choice;
printf("Welcome to the Student Score Management System!\n");
while (1) {
printf("\nPlease select an option:\n");
printf("1. Input scores\n");
printf("2. Sort by total score\n");
printf("3. Sort by student ID\n");
printf("4. Display student information\n");
printf("5. Save scores to file\n");
printf("6. Read scores from file\n");
printf("0. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1: input_scores(); break;
case 2: sort_by_total_score(); break;
case 3: sort_by_id(); break;
case 4: display_student(); break;
case 5: save_scores(); break;
case 6: read_scores(); break;
case 0: exit(0);
default: printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
// 输入学生成绩
void input_scores() {
printf("Please enter the number of students: ");
scanf("%d", &num_students);
printf("Please enter the number of subjects: ");
scanf("%d", &num_subjects);
for (int i = 0; i < num_students; i++) {
printf("Please enter the name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Please enter the ID of student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Please enter the scores of student %d: ", i + 1);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float) students[i].total_score / num_subjects;
}
printf("Scores have been input.\n");
}
// 按总分排序
void sort_by_total_score() {
for (int i = 0; i < num_students; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Scores have been sorted by total score.\n");
}
// 按学号排序
void sort_by_id() {
for (int i = 0; i < num_students; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].id > students[j].id) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Scores have been sorted by student ID.\n");
}
// 显示学生信息
void display_student() {
int id;
printf("Please enter the student ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
found = 1;
printf("Name: %s\n", students[i].name);
printf("Total score: %d\n", students[i].total_score);
printf("Average score: %.2f\n", students[i].avg_score);
printf("Scores for each subject:\n");
for (int j = 0; j < num_subjects; j++) {
printf("Subject %d: %d\n", j + 1, students[i].scores[j]);
}
break;
}
}
if (!found) {
printf("Student with ID %d not found.\n", id);
}
}
// 将成绩数据保存到文件中
void save_scores() {
FILE *fp;
fp = fopen("scores.txt", "w");
fprintf(fp, "%d %d\n", num_students, num_subjects);
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s %d ", students[i].name, students[i].id);
for (int j = 0; j < num_subjects; j++) {
fprintf(fp, "%d ", students[i].scores[j]);
}
fprintf(fp, "\n");
}
fclose(fp);
printf("Scores have been saved to file.\n");
}
// 从文件中读出成绩数据
void read_scores() {
FILE *fp;
fp = fopen("scores.txt", "r");
if (fp == NULL) {
printf("File not found.\n");
return;
}
fscanf(fp, "%d %d", &num_students, &num_subjects);
for (int i = 0; i < num_students; i++) {
fscanf(fp, "%s %d", students[i].name, &students[i].id);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
fscanf(fp, "%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float) students[i].total_score / num_subjects;
}
fclose(fp);
printf("Scores have been read from file.\n");
}
```
这个程序使用了结构体来存储每个学生的姓名、学号、成绩等信息,然后按照您的要求实现了各种功能,比如排序、查询、存储和读取数据等等。请注意,这只是一个简单的示例代码,您可以根据自己的需要进行适当的修改和优化。
阅读全文