学生成绩管理系统C语言(链表)多门课程
时间: 2025-05-11 08:28:15 浏览: 18
### 使用C语言实现支持多门课程的学生成绩管理系统
为了实现一个多门课程的学生管理系统,可以利用单链表作为数据存储结构。以下是基于已有引用中的知识点和代码片段构建的一个完整的示例。
#### 单链表节点定义
在该系统中,`struct student` 被用来表示学生的个人信息以及每门课的成绩。这里假设每位学生最多有五门课程成绩[^4]:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
int id;
char name[20];
float scores[5]; // 假设有5门课程
float average; // 平均分
struct student* next;
} STU;
```
#### 添加新学生记录函数
此函数用于向链表中添加新的学生信息并初始化其各科成绩为零。
```c
STU* add_student(STU* head, int id, const char* name) {
STU* new_stu = (STU*)malloc(sizeof(STU));
memset(new_stu->scores, 0, sizeof(float)*5);
new_stu->id = id;
strcpy(new_stu->name, name);
new_stu->average = 0.0f;
if (!head || strcmp(name, head->name)<0){
new_stu->next = head;
return new_stu;
}
STU* current=head,*prev=NULL;
while(current && strcmp(name,current->name)>0 ){
prev=current;
current=current->next ;
}
prev->next=new_stu;
new_stu->next=current;
return head;
}
```
上述代码实现了按姓名顺序插入的功能[^1]。
#### 显示所有学生的信息
遍历整个链表并将每个节点的内容打印出来。
```c
void display_students(STU* head) {
printf("%-10s%-20s", "ID", "Name");
for(int i=0;i<5;i++)printf("Score%d\t",i+1);
puts("Average");
STU* temp = head;
while(temp != NULL){
printf("%-10d%-20s",temp->id,temp->name );
for(int j=0;j<5;j++)printf("%.2f\t",temp->scores[j]);
printf("%.2f\n",temp->average);
temp=temp->next;
}
}
```
以上展示了如何格式化输出学生列表及其成绩详情。
#### 计算平均分数
每当更新某位学生的某一科目得分时都需要重新计算他的总评成绩。
```c
float calculate_average(STU* stu){
float sum=0;
for(int k=0;k<5;k++)sum+=stu->scores[k];
return sum/5;
}
// 更新某个特定学生的平均值
void update_average_score(STU* targetStu){
targetStu->average = calculate_average(targetStu);
}
```
这部分逻辑确保每次录入或者调整成绩后都能及时反映最新的平均分情况。
### 完整程序框架
综合前面各个部分可得如下整体架构:
```c
int main(){
STU *root=NULL;
root=add_student(root,1,"Alice");// 插入第一个学生
root=add_student(root,2,"Bob"); // 继续增加其他成员...
// 设置一些初始成绩给这些同学
set_scores(&root,"Mathematics",{78,92});
...
display_students(root); // 展示最终结果
free_list(root); // 清理内存资源释放工作完成后的指针销毁动作。
return EXIT_SUCCESS;
}
```
阅读全文
相关推荐


















