用c语言完成如下内容为主菜单的“学生成绩管理程序”。 学生成绩管理系统 1.输入学生成绩 2.显示所有学生数据 3.按学号查找学生数据 4.查找最高分 5.添加学生数据 6.按学号删除学生数据 7.成绩排序 8.推出程序 请输入选项编号(0一7) (本实验题目中,输入的数据必需以文件方式保存,实验时输入的数据可为 3~5条,完成后数据不得少于 30条。每功能完成后必需暂停,后回到主菜单〉
时间: 2024-03-14 07:48:59 浏览: 94
好的,我可以帮您解答这个问题。以下是一个简单的学生成绩管理程序的代码,使用了文件方式保存数据,实现了主菜单中的各个功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char id[20];
char name[50];
int score;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void save_data() {
FILE *fp = fopen("students.dat", "wb");
if (!fp) {
printf("Error: cannot open file students.dat!\n");
return;
}
fwrite(&num_students, sizeof(int), 1, fp);
fwrite(students, sizeof(struct student), num_students, fp);
fclose(fp);
printf("Data saved to file students.dat.\n");
}
void load_data() {
FILE *fp = fopen("students.dat", "rb");
if (!fp) {
printf("Error: cannot open file students.dat!\n");
return;
}
fread(&num_students, sizeof(int), 1, fp);
fread(students, sizeof(struct student), num_students, fp);
fclose(fp);
printf("Data loaded from file students.dat.\n");
}
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: maximum number of students reached!\n");
return;
}
printf("Enter student ID: ");
scanf("%s", students[num_students].id);
printf("Enter student name: ");
scanf("%s", students[num_students].name);
printf("Enter student score: ");
scanf("%d", &students[num_students].score);
num_students++;
save_data();
}
void show_all_students() {
if (num_students == 0) {
printf("No students found!\n");
return;
}
printf("ID\tName\tScore\n");
printf("----------------------------------\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
}
printf("----------------------------------\n");
}
void find_student_by_id() {
char id[20];
printf("Enter student ID: ");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("ID\tName\tScore\n");
printf("----------------------------------\n");
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
printf("----------------------------------\n");
return;
}
}
printf("No student with ID %s found!\n", id);
}
void find_highest_score() {
int highest_score = 0;
int highest_score_index = -1;
for (int i = 0; i < num_students; i++) {
if (students[i].score > highest_score) {
highest_score = students[i].score;
highest_score_index = i;
}
}
if (highest_score_index == -1) {
printf("No students found!\n");
return;
}
printf("ID\tName\tScore\n");
printf("----------------------------------\n");
printf("%s\t%s\t%d\n", students[highest_score_index].id, students[highest_score_index].name, students[highest_score_index].score);
printf("----------------------------------\n");
}
void delete_student_by_id() {
char id[20];
printf("Enter student ID: ");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j+1];
}
num_students--;
save_data();
printf("Student with ID %s deleted.\n", id);
return;
}
}
printf("No student with ID %s found!\n", id);
}
void sort_students_by_score() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
if (students[j].score < students[j+1].score) {
struct student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
printf("Students sorted by score.\n");
save_data();
}
int main() {
int choice;
load_data();
while (1) {
printf("\n");
printf("学生成绩管理系统\n");
printf("1.输入学生成绩\n");
printf("2.显示所有学生数据\n");
printf("3.按学号查找学生数据\n");
printf("4.查找最高分\n");
printf("5.添加学生数据\n");
printf("6.按学号删除学生数据\n");
printf("7.成绩排序\n");
printf("8.退出程序\n");
printf("请输入选项编号(1~8):");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_all_students();
break;
case 3:
find_student_by_id();
break;
case 4:
find_highest_score();
break;
case 5:
add_student();
break;
case 6:
delete_student_by_id();
break;
case 7:
sort_students_by_score();
break;
case 8:
printf("程序已退出。");
exit(0);
default:
printf("无效的选项,请重新输入。\n");
break;
}
printf("\n按任意键继续...");
getchar(); // clear newline character
getchar();
}
return 0;
}
```
该程序使用了一个 `struct student` 结构体来存储每个学生的信息,使用了一个全局数组 `students` 来存储学生数据,使用了一个全局变量 `num_students` 来记录当前学生数。程序中的各个函数分别实现了主菜单中的各个选项,其中 `save_data` 函数和 `load_data` 函数分别用于保存和加载学生数据到文件 `students.dat` 中。程序中使用了一个简单的冒泡排序算法来实现成绩排序功能。在程序运行过程中,每次执行完一个选项后,都会暂停并等待用户按下任意键才会回到主菜单。
阅读全文