可以帮我用c语言编写一个学生成绩管理系统吗?具体要求如下:用链表的形式保存学生信息和成绩信息。添加:增加一个或几个学生记录 删除:删除已录入的学生记录。 修改:根据输入的学号修改某一学生记录。 查询:根据输入的学号寻找某一学生记录。 排序:按平均成绩由高到低排序并输出名次表。 显示:输出所有需要补考的学生记录
时间: 2024-03-13 17:42:38 浏览: 70
学生成绩管理系统(c语言做的)
3星 · 编辑精心推荐
好的,我可以帮您编写这个学生成绩管理系统。以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20 // 最大名字长度
#define MAX_ID_LEN 10 // 最大学号长度
#define MAX_GRADE_LEN 5 // 最大成绩长度
// 定义学生结构体
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
float grade;
int is_makeup; // 是否需要补考,0表示不需要,1表示需要
struct student *next;
} student_t;
// 函数声明
student_t *create_student(char *name, char *id, float grade);
void add_student(student_t **head, student_t *new_student);
void delete_student(student_t **head, char *id);
void modify_student(student_t *head, char *id);
student_t *search_student(student_t *head, char *id);
void sort_students(student_t *head);
void show_makeup_students(student_t *head);
void show_all_students(student_t *head);
int main()
{
student_t *head = NULL;
// 添加学生
student_t *s1 = create_student("Tom", "10001", 80);
student_t *s2 = create_student("Jerry", "10002", 90);
student_t *s3 = create_student("Lucy", "10003", 85);
add_student(&head, s1);
add_student(&head, s2);
add_student(&head, s3);
// 删除学生
delete_student(&head, "10002");
// 修改学生
modify_student(head, "10001");
// 查询学生
student_t *s4 = search_student(head, "10003");
if (s4 != NULL) {
printf("Name: %s, ID: %s, Grade: %.2f\n", s4->name, s4->id, s4->grade);
}
// 排序
sort_students(head);
// 显示需要补考的学生
show_makeup_students(head);
// 显示所有学生
show_all_students(head);
return 0;
}
// 创建一个新学生
student_t *create_student(char *name, char *id, float grade)
{
student_t *new_student = (student_t *)malloc(sizeof(student_t));
strcpy(new_student->name, name);
strcpy(new_student->id, id);
new_student->grade = grade;
new_student->is_makeup = (grade < 60) ? 1 : 0;
new_student->next = NULL;
return new_student;
}
// 添加一个新学生
void add_student(student_t **head, student_t *new_student)
{
if (*head == NULL) {
*head = new_student;
} else {
student_t *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_student;
}
}
// 删除一个学生
void delete_student(student_t **head, char *id)
{
student_t *current = *head;
student_t *previous = NULL;
while (current != NULL && strcmp(current->id, id) != 0) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
// 修改一个学生
void modify_student(student_t *head, char *id)
{
student_t *current = head;
while (current != NULL && strcmp(current->id, id) != 0) {
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return;
}
printf("Please input the new name: ");
scanf("%s", current->name);
printf("Please input the new grade: ");
scanf("%f", ¤t->grade);
current->is_makeup = (current->grade < 60) ? 1 : 0;
}
// 查询一个学生
student_t *search_student(student_t *head, char *id)
{
student_t *current = head;
while (current != NULL && strcmp(current->id, id) != 0) {
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return NULL;
} else {
return current;
}
}
// 按平均成绩由高到低排序
void sort_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
student_t *inner_current = current->next;
while (inner_current != NULL) {
if (current->grade < inner_current->grade) {
char temp_name[MAX_NAME_LEN];
char temp_id[MAX_ID_LEN];
float temp_grade = current->grade;
int temp_is_makeup = current->is_makeup;
strcpy(temp_name, current->name);
strcpy(temp_id, current->id);
current->grade = inner_current->grade;
current->is_makeup = inner_current->is_makeup;
strcpy(current->name, inner_current->name);
strcpy(current->id, inner_current->id);
inner_current->grade = temp_grade;
inner_current->is_makeup = temp_is_makeup;
strcpy(inner_current->name, temp_name);
strcpy(inner_current->id, temp_id);
}
inner_current = inner_current->next;
}
current = current->next;
}
}
// 显示需要补考的学生
void show_makeup_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
if (current->is_makeup) {
printf("Name: %s, ID: %s, Grade: %.2f\n", current->name, current->id, current->grade);
}
current = current->next;
}
}
// 显示所有学生
void show_all_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
printf("Name: %s, ID: %s, Grade: %.2f\n", current->name, current->id, current->grade);
current = current->next;
}
}
```
以上是一个简单的学生成绩管理系统,实现了添加、删除、修改、查询、排序和显示需要补考的学生等功能。您可以根据自己的实际需求进行修改和扩展。
阅读全文