用c语言实现 学生成绩管理系统 实现功能:输入、输出、插入、删除、查找、追加、读入、显示、保存、排序、分类统计、退出。
时间: 2024-01-02 11:22:38 浏览: 181
以下是用C语言实现学生成绩管理系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int id;
char name[50];
float scores[3];
float average;
};
struct Student students[MAX_STUDENTS];
int numStudents = 0;
void inputStudent() {
struct Student newStudent;
printf("请输入学生的学号:");
scanf("%d", &newStudent.id);
printf("请输入学生的姓名:");
scanf("%s", newStudent.name);
printf("请输入学生的3门课成绩:");
for (int i = 0; i < 3; i++) {
scanf("%f", &newStudent.scores[i]);
}
newStudent.average = (newStudent.scores[0] + newStudent.scores[1] + newStudent.scores[2]) / 3;
students[numStudents++] = newStudent;
printf("学生信息已添加。\n");
}
void outputStudent(struct Student student) {
printf("学号:%d\n", student.id);
printf("姓名:%s\n", student.name);
printf("成绩:");
for (int i = 0; i < 3; i++) {
printf("%.2f ", student.scores[i]);
}
printf("\n");
printf("平均成绩:%.2f\n", student.average);
}
void displayStudents() {
if (numStudents == 0) {
printf("没有学生信息。\n");
return;
}
for (int i = 0; i < numStudents; i++) {
printf("第%d个学生信息:\n", i + 1);
outputStudent(students[i]);
}
}
void deleteStudent() {
int studentId;
printf("请输入要删除的学生的学号:");
scanf("%d", &studentId);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == studentId) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该学生。\n");
return;
}
for (int i = index; i < numStudents - 1; i++) {
students[i] = students[i + 1];
}
numStudents--;
printf("学生信息已删除。\n");
}
void searchStudent() {
int studentId;
printf("请输入要查找的学生的学号:");
scanf("%d", &studentId);
int index = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == studentId) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该学生。\n");
return;
}
printf("找到该学生的信息:\n");
outputStudent(students[index]);
}
void saveStudents() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("保存失败。\n");
return;
}
for (int i = 0; i < numStudents; i++) {
fprintf(file, "%d %s %.2f %.2f %.2f\n", students[i].id, students[i].name, students[i].scores[0], students[i].scores[1], students[i].scores[2]);
}
fclose(file);
printf("学生信息已保存。\n");
}
void loadStudents() {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("加载失败。\n");
return;
}
numStudents = 0;
while (fscanf(file, "%d %s %f %f %f", &students[numStudents].id, students[numStudents].name, &students[numStudents].scores[0], &students[numStudents].scores[1], &students[numStudents].scores[2]) != EOF) {
students[numStudents].average = (students[numStudents].scores[0] + students[numStudents].scores[1] + students[numStudents].scores[2]) / 3;
numStudents++;
}
fclose(file);
printf("学生信息已加载。\n");
}
void sortStudents() {
for (int i = 0; i < numStudents - 1; i++) {
for (int j = 0; j < numStudents - i - 1; j++) {
if (students[j].average < students[j + 1].average) {
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("学生信息已排序。\n");
}
void calculateAverage() {
for (int i = 0; i < numStudents; i++) {
students[i].average = (students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3;
}
printf("学生平均成绩已计算。\n");
}
int main() {
int choice;
while (1) {
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("9. 退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
inputStudent();
break;
case 2:
displayStudents();
break;
case 3:
deleteStudent();
break;
case 4:
searchStudent();
break;
case 5:
saveStudents();
break;
case 6:
loadStudents();
break;
case 7:
sortStudents();
break;
case 8:
calculateAverage();
break;
case 9:
printf("谢谢使用,再见!\n");
exit(0);
default:
printf("无效的选择。\n");
break;
}
}
return 0;
}
```
阅读全文