parent
5e3f03cec7
commit
dcf8eaa447
@ -0,0 +1,201 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 100
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
char name[50];
|
||||
int score;
|
||||
} Student;
|
||||
|
||||
int displayMenu() {
|
||||
printf("\n学生成绩管理系统\n");
|
||||
printf("1. 录入学生信息\n");
|
||||
printf("2. 显示所有学生信息\n");
|
||||
printf("3. 插入学生信息\n");
|
||||
printf("4. 删除学生信息\n");
|
||||
printf("5. 修改学生信息\n");
|
||||
printf("6. 查询学生信息\n");
|
||||
printf("7. 退出系统\n");
|
||||
printf("请选择操作: ");
|
||||
int choice;
|
||||
scanf("%d", &choice);
|
||||
return choice;
|
||||
}
|
||||
|
||||
void addStudent(Student students[], int *count) {
|
||||
if (*count >= MAX_STUDENTS) {
|
||||
printf("学生信息已满,无法添加更多学生。\n");
|
||||
return;
|
||||
}
|
||||
printf("请输入学生ID: ");
|
||||
scanf("%d", &students[*count].id);
|
||||
printf("请输入学生姓名: ");
|
||||
scanf("%s", students[*count].name);
|
||||
printf("请输入学生成绩: ");
|
||||
scanf("%d", &students[*count].score);
|
||||
(*count)++;
|
||||
printf("学生信息录入成功。\n");
|
||||
}
|
||||
|
||||
|
||||
void displayStudents(const Student students[], int count) {
|
||||
if (count == 0) {
|
||||
printf("没有学生信息。\n");
|
||||
return;
|
||||
}
|
||||
printf("学生ID\t姓名\t成绩\n");
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("%d\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void insertStudent(Student students[], int *count) {
|
||||
if (*count >= MAX_STUDENTS) {
|
||||
printf("学生信息已满,无法添加更多学生。\n");
|
||||
return;
|
||||
}
|
||||
Student newStudent;
|
||||
printf("请输入学生ID: ");
|
||||
scanf("%d", &newStudent.id);
|
||||
printf("请输入学生姓名: ");
|
||||
scanf("%s", newStudent.name);
|
||||
printf("请输入学生成绩: ");
|
||||
scanf("%d", &newStudent.score);
|
||||
|
||||
int index = *count;
|
||||
while (index > 0 &&students[index - 1].score < newStudent.score) {
|
||||
students[index] = students[index - 1];
|
||||
index--;
|
||||
}
|
||||
students[index] = newStudent;
|
||||
(*count)++;
|
||||
printf("学生信息插入成功。\n");
|
||||
}
|
||||
|
||||
|
||||
void deleteStudent(Student students[], int *count) {
|
||||
if (*count == 0) {
|
||||
printf("没有学生信息。\n");
|
||||
return;
|
||||
}
|
||||
int id;
|
||||
printf("请输入要删除的学生ID: ");
|
||||
scanf("%d", &id);
|
||||
|
||||
int index = -1;
|
||||
for (int i = 0; i < *count; i++) {
|
||||
if (students[i].id == id) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1) {
|
||||
printf("未找到指定ID的学生。\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = index; i < *count - 1; i++) {
|
||||
students[i] = students[i + 1];
|
||||
}
|
||||
(*count)--;
|
||||
printf("学生信息删除成功。\n");
|
||||
}
|
||||
int compareStudents(const void *a, const void *b) {
|
||||
return ((Student *)b)->score - ((Student *)a)->score;
|
||||
}
|
||||
void modifyStudent(Student students[], int count) {
|
||||
if (count == 0) {
|
||||
printf("没有学生信息。\n");
|
||||
return;
|
||||
}
|
||||
int id;
|
||||
printf("请输入要修改的学生ID: ");
|
||||
scanf("%d", &id);
|
||||
|
||||
int index = -1;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (students[i].id == id) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1) {
|
||||
printf("未找到指定ID的学生。\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("请输入新的学生姓名: ");
|
||||
scanf("%s", students[index].name);
|
||||
printf("请输入新的学生成绩: ");
|
||||
scanf("%d", &students[index].score);
|
||||
printf("学生信息修改成功。\n");
|
||||
|
||||
// 重新排序
|
||||
qsort(students, count, sizeof(Student), compareStudents);
|
||||
}
|
||||
void queryStudent(const Student students[], int count) {
|
||||
if (count == 0) {
|
||||
printf("没有学生信息。\n");
|
||||
return;
|
||||
}
|
||||
int id;
|
||||
printf("请输入要查询的学生ID: ");
|
||||
scanf("%d", &id);
|
||||
|
||||
int found = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (students[i].id == id) {
|
||||
printf("学生ID: %d, 姓名: %s, 成绩: %d\n", students[i].id, students[i].name, students[i].score);
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("未找到指定ID的学生。\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Student students[MAX_STUDENTS];
|
||||
int count = 0;
|
||||
int choice;
|
||||
|
||||
do {
|
||||
choice = displayMenu();
|
||||
switch (choice) {
|
||||
case 1:
|
||||
addStudent(students, &count);
|
||||
break;
|
||||
case 2:
|
||||
displayStudents(students, count);
|
||||
break;
|
||||
case 3:
|
||||
insertStudent(students, &count);
|
||||
break;
|
||||
case 4:
|
||||
deleteStudent(students, &count);
|
||||
break;
|
||||
case 5:
|
||||
modifyStudent(students, count);
|
||||
break;
|
||||
case 6:
|
||||
queryStudent(students, count);
|
||||
break;
|
||||
case 7:
|
||||
printf("退出系统。\n");
|
||||
break;
|
||||
default:
|
||||
printf("无效的选择,请重新输入。\n");
|
||||
}
|
||||
} while (choice != 7);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in new issue