用c语言做一个学生成绩管理系统,功能包含添加,删除,修改,查询,按平均分大小排序,最后存档
时间: 2024-04-30 14:18:26 浏览: 21
好的,下面是一个简单的学生成绩管理系统的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[20];
int id;
float score;
} Student;
void add_student(Student students[], int *num_students) {
if (*num_students >= MAX_STUDENTS) {
printf("The maximum number of students has been reached.\n");
return;
}
Student new_student;
printf("Enter the name of the student: ");
scanf("%s", new_student.name);
printf("Enter the ID of the student: ");
scanf("%d", &new_student.id);
printf("Enter the score of the student: ");
scanf("%f", &new_student.score);
students[*num_students] = new_student;
(*num_students)++;
printf("The student has been added.\n");
}
void delete_student(Student students[], int *num_students) {
int id;
printf("Enter the ID of the student you want to delete: ");
scanf("%d", &id);
for (int i = 0; i < *num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < *num_students - 1; j++) {
students[j] = students[j + 1];
}
(*num_students)--;
printf("The student has been deleted.\n");
return;
}
}
printf("Student not found.\n");
}
void modify_student(Student students[], int num_students) {
int id;
printf("Enter the ID of the student you want to modify: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Enter the new name of the student: ");
scanf("%s", students[i].name);
printf("Enter the new score of the student: ");
scanf("%f", &students[i].score);
printf("The student has been modified.\n");
return;
}
}
printf("Student not found.\n");
}
void query_student(Student students[], int num_students) {
int id;
printf("Enter the ID of the student you want to query: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Name: %s\nID: %d\nScore: %.2f\n", students[i].name, students[i].id, students[i].score);
return;
}
}
printf("Student not found.\n");
}
void sort_students(Student students[], int num_students) {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].score < students[j].score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("The students have been sorted by score.\n");
}
void save_students(Student students[], int num_students) {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s %d %.2f\n", students[i].name, students[i].id, students[i].score);
}
fclose(fp);
printf("The students have been saved to the file.\n");
}
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
while (1) {
printf("1. Add a new student\n");
printf("2. Delete a student\n");
printf("3. Modify a student\n");
printf("4. Query a student\n");
printf("5. Sort the students by score\n");
printf("6. Save the students to a file\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
delete_student(students, &num_students);
break;
case 3:
modify_student(students, num_students);
break;
case 4:
query_student(students, num_students);
break;
case 5:
sort_students(students, num_students);
break;
case 6:
save_students(students, num_students);
break;
case 7:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
这个程序使用了一个 `Student` 结构体来存储每个学生的信息,包括姓名、学号和分数。在主函数中,使用一个 `while` 循环来不断显示菜单,根据用户的选择执行相应的功能。其中,函数 `add_student` 用于添加学生,函数 `delete_student` 用于删除学生,函数 `modify_student` 用于修改学生信息,函数 `query_student` 用于查询学生信息,函数 `sort_students` 用于按照分数大小对学生进行排序,函数 `save_students` 用于将学生信息保存到文件中。
这是一个简单的学生成绩管理系统,可以根据实际需要进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)