学生成绩管理系统c语音
时间: 2024-01-03 11:22:44 浏览: 61
学生成绩管理系统是一个常见的项目,可以用来对学生的成绩进行增删改查和排序等操作。在C语言中,可以使用文件来存储学生的信息,并通过读写文件来实现对学生成绩的管理。
以下是一个简单的学生成绩管理系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int score;
};
void addStudent() {
struct Student student;
FILE *file = fopen("students.txt", "a");
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生成绩:");
scanf("%d", &student.score);
fprintf(file, "%s %d\n", student.name, student.score);
fclose(file);
printf("学生信息添加成功!\n");
}
void deleteStudent() {
char name[50];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
FILE *file = fopen("students.txt", "r");
FILE *tempFile = fopen("temp.txt", "w");
struct Student student;
while (fscanf(file, "%s %d", student.name, &student.score) != EOF) {
if (strcmp(student.name, name) != 0) {
fprintf(tempFile, "%s %d\n", student.name, student.score);
}
}
fclose(file);
fclose(tempFile);
remove("students.txt");
rename("temp.txt", "students.txt");
printf("学生信息删除成功!\n");
}
void displayStudents() {
FILE *file = fopen("students.txt", "r");
struct Student student;
while (fscanf(file, "%s %d", student.name, &student.score) != EOF) {
printf("姓名:%s,成绩:%d\n", student.name, student.score);
}
fclose(file);
}
void sortStudents() {
FILE *file = fopen("students.txt", "r");
struct Student students[100];
int count = 0;
while (fscanf(file, "%s %d", students[count].name, &students[count].score) != EOF) {
count++;
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 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("按成绩降序排序后的学生信息:\n");
for (int i = 0; i < count; i++) {
printf("姓名:%s,成绩:%d\n", students[i].name, students[i].score);
}
fclose(file);
}
int main() {
int choice;
while (1) {
printf("学生成绩管理系统\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 显示学生信息\n");
printf("4. 按成绩排序\n");
printf("5. 退出\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
displayStudents();
break;
case 4:
sortStudents();
break;
case 5:
exit(0);
default:
printf("无效的操作编号!\n");
break;
}
}
return 0;
}
```
这个示例代码实现了学生成绩管理系统的基本功能,包括添加学生信息、删除学生信息、显示学生信息和按成绩排序等操作。你可以根据自己的需求进行修改和扩展。
阅读全文