学生信息包含:学号、姓名、3门考试成绩(英语、数学、计算机)。并将学生信息用文件保存,并可在电脑中直接查找并打开该文件。 请设计一个学生成绩管理系统,该系统包括如下功能: 1. 添加学生信息 2. 按学号排序 3. 插入 (输入一个学生的信息,假定表按学号有序); 4. 查找 (输入一个学生学号,输出其各科成绩); 5. 删除 (按输入的学号删除该学生); 6. 退出C语言编写
时间: 2024-02-12 13:07:19 浏览: 80
好的,这是一个比较常见的学生成绩管理系统,我们可以用C语言来实现它。下面是我的代码实现,希望能够帮到你:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
// 定义学生结构体
struct Student {
int id; // 学号
char name[MAX_NAME_LENGTH]; // 姓名
int score[3]; // 3门考试成绩(英语、数学、计算机)
struct Student* next; // 链表指针
};
// 定义全局变量,指向链表头
struct Student* head = NULL;
// 函数声明
void addStudent();
void sortById();
void insert();
void search();
void delete();
void printMenu();
int main() {
int choice;
while (1) {
printMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
sortById();
break;
case 3:
insert();
break;
case 4:
search();
break;
case 5:
delete();
break;
case 6:
printf("退出成功!\n");
return 0;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
}
// 添加学生信息
void addStudent() {
struct Student* newStudent = (struct Student*) malloc(sizeof(struct Student));
printf("请输入学生的学号:");
scanf("%d", &(newStudent->id));
printf("请输入学生的姓名:");
scanf("%s", newStudent->name);
printf("请输入学生的英语成绩:");
scanf("%d", &(newStudent->score[0]));
printf("请输入学生的数学成绩:");
scanf("%d", &(newStudent->score[1]));
printf("请输入学生的计算机成绩:");
scanf("%d", &(newStudent->score[2]));
newStudent->next = NULL;
if (head == NULL) {
// 如果链表为空,新建链表头
head = newStudent;
} else {
// 否则将新节点加在链表尾部
struct Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
printf("添加成功!\n");
}
// 按学号排序
void sortById() {
int count = 0;
struct Student* current = head;
while (current != NULL) {
count++;
current = current->next;
}
struct Student* students[count];
current = head;
// 将链表节点存入数组中
for (int i = 0; i < count; i++) {
students[i] = current;
current = current->next;
}
// 冒泡排序
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (students[j]->id > students[j + 1]->id) {
struct Student* temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 重新生成链表
for (int i = 0; i < count - 1; i++) {
students[i]->next = students[i + 1];
}
students[count - 1]->next = NULL;
head = students[0];
printf("排序成功!\n");
}
// 插入
void insert() {
struct Student* newStudent = (struct Student*) malloc(sizeof(struct Student));
printf("请输入学生的学号:");
scanf("%d", &(newStudent->id));
printf("请输入学生的姓名:");
scanf("%s", newStudent->name);
printf("请输入学生的英语成绩:");
scanf("%d", &(newStudent->score[0]));
printf("请输入学生的数学成绩:");
scanf("%d", &(newStudent->score[1]));
printf("请输入学生的计算机成绩:");
scanf("%d", &(newStudent->score[2]));
newStudent->next = NULL;
if (head == NULL) {
// 如果链表为空,新建链表头
head = newStudent;
} else {
struct Student* current = head;
struct Student* previous = NULL;
while (current != NULL && current->id < newStudent->id) {
previous = current;
current = current->next;
}
if (previous == NULL) {
// 如果新节点比链表头的id还小,将新节点作为链表头
newStudent->next = head;
head = newStudent;
} else {
// 否则将新节点插入在previous和current之间
previous->next = newStudent;
newStudent->next = current;
}
}
printf("插入成功!\n");
}
// 查找
void search() {
int id;
struct Student* current = head;
printf("请输入要查找的学生的学号:");
scanf("%d", &id);
while (current != NULL && current->id != id) {
current = current->next;
}
if (current == NULL) {
printf("未找到该学生!\n");
} else {
printf("学号:%d,姓名:%s,英语成绩:%d,数学成绩:%d,计算机成绩:%d\n", current->id, current->name, current->score[0], current->score[1], current->score[2]);
}
}
// 删除
void delete() {
int id;
struct Student* current = head;
struct Student* previous = NULL;
printf("请输入要删除的学生的学号:");
scanf("%d", &id);
while (current != NULL && current->id != id) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("未找到该学生!\n");
} else {
if (previous == NULL) {
// 如果要删除的节点是链表头
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("删除成功!\n");
}
}
// 打印菜单
void printMenu() {
printf("学生成绩管理系统\n");
printf("1. 添加学生信息\n");
printf("2. 按学号排序\n");
printf("3. 插入\n");
printf("4. 查找\n");
printf("5. 删除\n");
printf("6. 退出\n");
printf("请选择操作:");
}
```
这个程序使用链表来存储学生信息,可以实现添加、排序、插入、查找和删除等功能。其中,`addStudent`函数用于添加学生信息,`sortById`函数用于按学号排序,`insert`函数用于插入一个新节点,`search`函数用于查找一个学生的信息,`delete`函数用于删除一个学生的信息。同时,还提供了一个`printMenu`函数,用于打印菜单,方便用户选择操作。
阅读全文