使用C语言编写学生成绩管理系统
时间: 2023-09-21 18:10:56 浏览: 109
以下是一个简单的学生成绩管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[20];
int id;
int score;
};
void add_student(struct student *students, int *num_students) {
if (*num_students < MAX_STUDENTS) {
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter ID: ");
scanf("%d", &new_student.id);
printf("Enter score: ");
scanf("%d", &new_student.score);
students[*num_students] = new_student;
(*num_students)++;
printf("Student added successfully!\n");
} else {
printf("Maximum number of students reached.\n");
}
}
void remove_student(struct student *students, int *num_students) {
int id_to_remove, i;
printf("Enter ID of student to remove: ");
scanf("%d", &id_to_remove);
for (i = 0; i < *num_students; i++) {
if (students[i].id == id_to_remove) {
// shift all students after this one back by one
for (; i < *num_students - 1; i++) {
students[i] = students[i + 1];
}
(*num_students)--;
printf("Student removed successfully!\n");
return;
}
}
printf("Student not found.\n");
}
void print_student(struct student student) {
printf("%-20s %10d %10d\n", student.name, student.id, student.score);
}
void print_all_students(struct student *students, int num_students) {
int i;
printf("%-20s %10s %10s\n", "Name", "ID", "Score");
for (i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void save_students_to_file(struct student *students, int num_students, char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
int i;
for (i = 0; i < num_students; i++) {
fprintf(file, "%s %d %d\n", students[i].name, students[i].id, students[i].score);
}
fclose(file);
printf("Students saved to file successfully!\n");
}
void load_students_from_file(struct student *students, int *num_students, char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
int i = 0;
char name[20];
int id, score;
while (fscanf(file, "%s %d %d\n", name, &id, &score) != EOF) {
if (*num_students < MAX_STUDENTS) {
struct student new_student;
strcpy(new_student.name, name);
new_student.id = id;
new_student.score = score;
students[i] = new_student;
(*num_students)++;
i++;
} else {
break;
}
}
fclose(file);
printf("Students loaded from file successfully!\n");
}
int main() {
struct student students[MAX_STUDENTS];
int num_students = 0;
int choice;
char filename[20];
printf("Welcome to the Student Grade Management System!\n");
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Remove student\n");
printf("3. Print all students\n");
printf("4. Save students to file\n");
printf("5. Load students from file\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
remove_student(students, &num_students);
break;
case 3:
print_all_students(students, num_students);
break;
case 4:
printf("Enter filename: ");
scanf("%s", filename);
save_students_to_file(students, num_students, filename);
break;
case 5:
printf("Enter filename: ");
scanf("%s", filename);
load_students_from_file(students, &num_students, filename);
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
该程序实现了以下功能:
1. 添加学生:从用户输入中获取学生的姓名、学号和成绩,并将其添加到学生数组中。
2. 删除学生:从用户输入中获取要删除的学生的学号,然后在学生数组中查找并删除该学生。
3. 打印所有学生:按照姓名、学号和成绩的顺序打印所有学生的详细信息。
4. 将学生保存到文件:将所有学生的详细信息写入指定的文件中。
5. 从文件加载学生:从指定的文件中读取所有学生的详细信息,并将它们添加到学生数组中。
6. 退出程序:退出程序。
使用该程序,您可以轻松管理学生的成绩并将它们保存到文件中。
阅读全文