You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
301 lines
7.3 KiB
301 lines
7.3 KiB
# SIMS-LITE
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_STUDENTS 100
|
|
|
|
typedef struct {
|
|
char id[20]; // 学号
|
|
char name[50]; // 姓名
|
|
float score; // 成绩
|
|
} Student;
|
|
|
|
typedef struct {
|
|
Student students[MAX_STUDENTS];
|
|
int length; // 当前学生数量
|
|
} StudentList;
|
|
|
|
// 初始化学生列表
|
|
void InitList(StudentList *L) {
|
|
L->length = 0;
|
|
}
|
|
|
|
// (1) 逐个输入学生信息
|
|
void InputStudents(StudentList *L) {
|
|
int n;
|
|
printf("请输入学生人数(不超过%d): ", MAX_STUDENTS);
|
|
scanf("%d", &n);
|
|
|
|
if (n <= 0 || n > MAX_STUDENTS) {
|
|
printf("输入人数不合法!\n");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
printf("请输入第%d个学生的信息:\n", i+1);
|
|
printf("学号: ");
|
|
scanf("%s", L->students[L->length].id);
|
|
printf("姓名: ");
|
|
scanf("%s", L->students[L->length].name);
|
|
printf("成绩: ");
|
|
scanf("%f", &L->students[L->length].score);
|
|
L->length++;
|
|
}
|
|
printf("输入完成!\n");
|
|
}
|
|
|
|
// (2) 显示所有学生信息
|
|
void DisplayStudents(StudentList *L) {
|
|
if (L->length == 0) {
|
|
printf("没有学生信息!\n");
|
|
return;
|
|
}
|
|
|
|
printf("学号\t姓名\t成绩\n");
|
|
printf("----------------------------\n");
|
|
for (int i = 0; i < L->length; i++) {
|
|
printf("%s\t%s\t%.1f\n",
|
|
L->students[i].id,
|
|
L->students[i].name,
|
|
L->students[i].score);
|
|
}
|
|
}
|
|
|
|
// (3) 在指定位置插入学生信息
|
|
void InsertStudent(StudentList *L) {
|
|
if (L->length >= MAX_STUDENTS) {
|
|
printf("学生列表已满,无法插入!\n");
|
|
return;
|
|
}
|
|
|
|
int pos;
|
|
printf("请输入要插入的位置(1-%d): ", L->length + 1);
|
|
scanf("%d", &pos);
|
|
|
|
if (pos < 1 || pos > L->length + 1) {
|
|
printf("位置不合法!\n");
|
|
return;
|
|
}
|
|
|
|
Student s;
|
|
printf("请输入学生信息:\n");
|
|
printf("学号: ");
|
|
scanf("%s", s.id);
|
|
printf("姓名: ");
|
|
scanf("%s", s.name);
|
|
printf("成绩: ");
|
|
scanf("%f", &s.score);
|
|
|
|
// 将pos-1之后的元素后移
|
|
for (int i = L->length; i >= pos; i--) {
|
|
L->students[i] = L->students[i-1];
|
|
}
|
|
|
|
L->students[pos-1] = s;
|
|
L->length++;
|
|
printf("插入成功!\n");
|
|
}
|
|
|
|
// (4) 删除指定位置的学生记录
|
|
void DeleteStudent(StudentList *L) {
|
|
if (L->length == 0) {
|
|
printf("学生列表为空,无法删除!\n");
|
|
return;
|
|
}
|
|
|
|
int pos;
|
|
printf("请输入要删除的位置(1-%d): ", L->length);
|
|
scanf("%d", &pos);
|
|
|
|
if (pos < 1 || pos > L->length) {
|
|
printf("位置不合法!\n");
|
|
return;
|
|
}
|
|
|
|
printf("删除的学生信息: %s %s %.1f\n",
|
|
L->students[pos-1].id,
|
|
L->students[pos-1].name,
|
|
L->students[pos-1].score);
|
|
|
|
// 将pos之后的元素前移
|
|
for (int i = pos; i < L->length; i++) {
|
|
L->students[i-1] = L->students[i];
|
|
}
|
|
|
|
L->length--;
|
|
printf("删除成功!\n");
|
|
}
|
|
|
|
// (5) 统计学生个数
|
|
void CountStudents(StudentList *L) {
|
|
printf("当前学生人数: %d\n", L->length);
|
|
}
|
|
|
|
// 直接插入排序 - 按姓名
|
|
void InsertionSortByName(StudentList *L) {
|
|
int i, j;
|
|
Student temp;
|
|
|
|
for (i = 1; i < L->length; i++) {
|
|
temp = L->students[i];
|
|
j = i - 1;
|
|
|
|
while (j >= 0 && strcmp(L->students[j].name, temp.name) > 0) {
|
|
L->students[j+1] = L->students[j];
|
|
j--;
|
|
}
|
|
|
|
L->students[j+1] = temp;
|
|
}
|
|
printf("按姓名排序完成!\n");
|
|
}
|
|
|
|
// 快速排序分区函数 - 按学号
|
|
int Partition(StudentList *L, int low, int high) {
|
|
Student pivot = L->students[low];
|
|
|
|
while (low < high) {
|
|
while (low < high && strcmp(L->students[high].id, pivot.id) >= 0) {
|
|
high--;
|
|
}
|
|
L->students[low] = L->students[high];
|
|
|
|
while (low < high && strcmp(L->students[low].id, pivot.id) <= 0) {
|
|
low++;
|
|
}
|
|
L->students[high] = L->students[low];
|
|
}
|
|
|
|
L->students[low] = pivot;
|
|
return low;
|
|
}
|
|
|
|
// 快速排序递归函数 - 按学号
|
|
void QuickSort(StudentList *L, int low, int high) {
|
|
if (low < high) {
|
|
int pivotpos = Partition(L, low, high);
|
|
QuickSort(L, low, pivotpos - 1);
|
|
QuickSort(L, pivotpos + 1, high);
|
|
}
|
|
}
|
|
|
|
// (6) 排序功能
|
|
void SortStudents(StudentList *L) {
|
|
if (L->length == 0) {
|
|
printf("没有学生信息,无法排序!\n");
|
|
return;
|
|
}
|
|
|
|
int choice;
|
|
printf("请选择排序方式:\n");
|
|
printf("1. 按姓名排序(直接插入排序)\n");
|
|
printf("2. 按学号排序(快速排序)\n");
|
|
printf("请选择: ");
|
|
scanf("%d", &choice);
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
InsertionSortByName(L);
|
|
break;
|
|
case 2:
|
|
QuickSort(L, 0, L->length - 1);
|
|
printf("按学号排序完成!\n");
|
|
break;
|
|
default:
|
|
printf("无效选择!\n");
|
|
}
|
|
}
|
|
|
|
// (7) 折半查找 - 按学号
|
|
void BinarySearchById(StudentList *L) {
|
|
if (L->length == 0) {
|
|
printf("没有学生信息,无法查找!\n");
|
|
return;
|
|
}
|
|
|
|
// 先确保按学号有序
|
|
QuickSort(L, 0, L->length - 1);
|
|
|
|
char targetId[20];
|
|
printf("请输入要查找的学号: ");
|
|
scanf("%s", targetId);
|
|
|
|
int low = 0, high = L->length - 1, mid;
|
|
while (low <= high) {
|
|
mid = (low + high) / 2;
|
|
|
|
int cmp = strcmp(L->students[mid].id, targetId);
|
|
if (cmp == 0) {
|
|
printf("查找成功!\n");
|
|
printf("学号: %s, 姓名: %s, 成绩: %.1f\n",
|
|
L->students[mid].id,
|
|
L->students[mid].name,
|
|
L->students[mid].score);
|
|
return;
|
|
} else if (cmp < 0) {
|
|
low = mid + 1;
|
|
} else {
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
|
|
printf("未找到学号为 %s 的学生!\n", targetId);
|
|
}
|
|
|
|
// 显示菜单
|
|
void ShowMenu() {
|
|
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("0. 退出系统\n");
|
|
printf("请选择操作: ");
|
|
}
|
|
|
|
int main() {
|
|
StudentList list;
|
|
InitList(&list);
|
|
|
|
int choice;
|
|
do {
|
|
ShowMenu();
|
|
scanf("%d", &choice);
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
InputStudents(&list);
|
|
break;
|
|
case 2:
|
|
DisplayStudents(&list);
|
|
break;
|
|
case 3:
|
|
InsertStudent(&list);
|
|
break;
|
|
case 4:
|
|
DeleteStudent(&list);
|
|
break;
|
|
case 5:
|
|
CountStudents(&list);
|
|
break;
|
|
case 6:
|
|
SortStudents(&list);
|
|
break;
|
|
case 7:
|
|
BinarySearchById(&list);
|
|
break;
|
|
case 0:
|
|
printf("感谢使用,再见!\n");
|
|
break;
|
|
default:
|
|
printf("无效选择,请重新输入!\n");
|
|
}
|
|
} while (choice != 0);
|
|
|
|
return 0;
|
|
}
|