|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
|
|
|
// 定义学生结构体
|
|
|
typedef struct Student {
|
|
|
char id[20];
|
|
|
char clas[20];
|
|
|
char name[20];
|
|
|
double score1;
|
|
|
double score2;
|
|
|
double score3;
|
|
|
double score;
|
|
|
struct Student* next;
|
|
|
} Student;
|
|
|
|
|
|
// 全局变量,指向学生链表的头结点和尾结点
|
|
|
Student* head = NULL;
|
|
|
Student* tail = NULL;
|
|
|
|
|
|
// 函数声明
|
|
|
void showMenu();
|
|
|
void inputStudent();
|
|
|
void deleteStudent();
|
|
|
void selectStudent();
|
|
|
void orderStudent();
|
|
|
void outputStudent();
|
|
|
void quit();
|
|
|
|
|
|
// 主函数
|
|
|
int main() {
|
|
|
showMenu();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 显示菜单
|
|
|
void showMenu() {
|
|
|
printf("1. 输入学生信息\n");
|
|
|
printf("2. 删除学生信息\n");
|
|
|
printf("3. 查询学生信息\n");
|
|
|
printf("4. 排序学生信息\n");
|
|
|
printf("5. 输出学生信息\n");
|
|
|
printf("6. 退出\n");
|
|
|
printf("请选择操作:");
|
|
|
|
|
|
int option;
|
|
|
scanf("%d", &option);
|
|
|
|
|
|
switch (option) {
|
|
|
case 1:
|
|
|
inputStudent();
|
|
|
break;
|
|
|
case 2:
|
|
|
deleteStudent();
|
|
|
break;
|
|
|
case 3:
|
|
|
selectStudent();
|
|
|
break;
|
|
|
case 4:
|
|
|
orderStudent();
|
|
|
break;
|
|
|
case 5:
|
|
|
outputStudent();
|
|
|
break;
|
|
|
case 6:
|
|
|
quit();
|
|
|
break;
|
|
|
default:
|
|
|
printf("无效的选项,请重新选择。\n");
|
|
|
showMenu();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 输入学生信息
|
|
|
void inputStudent() {
|
|
|
printf("请输入学生信息(学号 班级 姓名 成绩1 成绩2 成绩3):\n");
|
|
|
|
|
|
Student* newStudent = (Student*)malloc(sizeof(Student));
|
|
|
scanf("%s %s %s %lf %lf %lf", newStudent->id, newStudent->clas, newStudent->name,
|
|
|
&newStudent->score1, &newStudent->score2, &newStudent->score3);
|
|
|
newStudent->score = newStudent->score1 + newStudent->score2 + newStudent->score3;
|
|
|
newStudent->next = NULL;
|
|
|
|
|
|
if (head == NULL) {
|
|
|
head = newStudent;
|
|
|
tail = newStudent;
|
|
|
} else {
|
|
|
tail->next = newStudent;
|
|
|
tail = newStudent;
|
|
|
}
|
|
|
|
|
|
printf("学生信息录入成功。\n");
|
|
|
showMenu();
|
|
|
}
|
|
|
|
|
|
// 删除学生信息
|
|
|
void deleteStudent() {
|
|
|
if (head == NULL) {
|
|
|
printf("没有学生信息可供删除。\n");
|
|
|
showMenu();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
printf("请输入要删除的学生的学号或姓名:\n");
|
|
|
char search[20];
|
|
|
scanf("%s", search);
|
|
|
|
|
|
Student* current = head;
|
|
|
Student* previous = NULL;
|
|
|
int count = 0;
|
|
|
|
|
|
while (current != NULL) {
|
|
|
if (strcmp(current->id, search) == 0 || strcmp(current->name, search) == 0) {
|
|
|
if (previous == NULL) {
|
|
|
head = current->next;
|
|
|
} else {
|
|
|
previous->next = current->next;
|
|
|
}
|
|
|
|
|
|
free(current);
|
|
|
count++;
|
|
|
} else {
|
|
|
previous = current;
|
|
|
}
|
|
|
|
|
|
current = current->next;
|
|
|
}
|
|
|
|
|
|
if (count == 0) {
|
|
|
printf("未找到匹配的学生信息。\n");
|
|
|
} else {
|
|
|
printf("成功删除 %d 条学生信息。\n", count);
|
|
|
}
|
|
|
|
|
|
showMenu();
|
|
|
}
|
|
|
|
|
|
// 查询学生信息
|
|
|
void selectStudent() {
|
|
|
if (head == NULL) {
|
|
|
printf("没有学生信息可供查询。\n");
|
|
|
showMenu();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
printf("请输入要查询的学生的学号或班级:\n");
|
|
|
char search[20];
|
|
|
scanf("%s", search);
|
|
|
|
|
|
Student* current = head;
|
|
|
int count = 0;
|
|
|
|
|
|
while (current != NULL) {
|
|
|
if (strcmp(current->id, search) == 0 || strcmp(current->clas, search) == 0) {
|
|
|
printf("学号:%s\n", current->id);
|
|
|
printf("班级:%s\n", current->clas);
|
|
|
printf("姓名:%s\n", current->name);
|
|
|
printf("成绩1:%lf\n", current->score1);
|
|
|
printf("成绩2:%lf\n", current->score2);
|
|
|
printf("成绩3:%lf\n", current->score3);
|
|
|
printf("总成绩:%lf\n", current->score);
|
|
|
printf("\n");
|
|
|
count++;
|
|
|
}
|
|
|
|
|
|
current = current->next;
|
|
|
}
|
|
|
|
|
|
if (count == 0) {
|
|
|
printf("未找到匹配的学生信息。\n");
|
|
|
}
|
|
|
|
|
|
showMenu();
|
|
|
}
|
|
|
|
|
|
// 排序学生信息
|
|
|
void orderStudent() {
|
|
|
if (head == NULL) {
|
|
|
printf("没有学生信息可供排序。\n");
|
|
|
showMenu();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
int swapped;
|
|
|
Student* current;
|
|
|
Student* previous = NULL;
|
|
|
|
|
|
do {
|
|
|
swapped = 0;
|
|
|
current = head;
|
|
|
|
|
|
while (current->next != NULL) {
|
|
|
if (strcmp(current->clas, current->next->clas) > 0 ||
|
|
|
(strcmp(current->clas, current->next->clas) == 0 &&
|
|
|
current->score < current->next->score)) {
|
|
|
if (previous == NULL) {
|
|
|
head = current->next;
|
|
|
} else {
|
|
|
previous->next = current->next;
|
|
|
}
|
|
|
|
|
|
Student* temp = current->next->next;
|
|
|
current->next->next = current;
|
|
|
current->next = temp;
|
|
|
|
|
|
swapped = 1;
|
|
|
}
|
|
|
|
|
|
previous = current;
|
|
|
current = current->next;
|
|
|
}
|
|
|
} while (swapped);
|
|
|
|
|
|
printf("学生信息排序成功。\n");
|
|
|
showMenu();
|
|
|
}
|
|
|
|
|
|
// 输出学生信息
|
|
|
void outputStudent() {
|
|
|
if (head == NULL) {
|
|
|
printf("没有学生信息可供输出。\n");
|
|
|
showMenu();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Student* current = head;
|
|
|
|
|
|
while (current != NULL) {
|
|
|
printf("学号:%s\n", current->id);
|
|
|
printf("班级:%s\n", current->clas);
|
|
|
printf("姓名:%s\n", current->name);
|
|
|
printf("成绩1:%lf\n", current->score1);
|
|
|
printf("成绩2:%lf\n", current->score2);
|
|
|
printf("成绩3:%lf\n", current->score3);
|
|
|
printf("总成绩:%lf\n", current->score);
|
|
|
printf("\n");
|
|
|
|
|
|
current = current->next;
|
|
|
}
|
|
|
|
|
|
showMenu();
|
|
|
}
|
|
|
|
|
|
// 退出程序
|
|
|
void quit() {
|
|
|
printf("感谢使用学生成绩管理系统,再见!\n");
|
|
|
}
|