学生成绩管理系统C代码
时间: 2023-12-22 21:28:38 浏览: 77
C语言学生成绩管理系统源代码
以下是一个简单的学生成绩管理系统的C代码,包括插入、删除、排序和输出等操作。代码中使用了带头结点的单链表来管理学生成绩表。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char id[20]; // 学号
char name[20]; // 姓名
int score[4]; // 各科成绩
int sum; // 总分
struct student *next; // 指向下一个学生的指针
} Student;
// 创建学生结点
Student *create_node(char *id, char *name, int *score) {
Student *node = (Student *)malloc(sizeof(Student));
strcpy(node->id, id);
strcpy(node->name, name);
memcpy(node->score, score, sizeof(int) * 4);
node->sum = score[0] + score[1] + score[2] + score[3];
node->next = NULL;
return node;
}
// 插入学生结点
void insert_node(Student *head, Student *node) {
Student *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
// 删除学生结点
void delete_node(Student *head, char *id) {
Student *p = head->next;
Student *pre = head;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
pre->next = p->next;
free(p);
return;
}
pre = p;
p = p->next;
}
}
// 按总分排序
void sort_by_sum(Student *head) {
Student *p, *q, *pre, *tmp;
for (p = head->next; p != NULL; p = p->next) {
tmp = p;
for (q = p->next; q != NULL; q = q->next) {
if (q->sum > tmp->sum) {
tmp = q;
}
}
if (tmp != p) {
pre->next = tmp;
p->next = tmp->next;
tmp->next = p;
tmp = p;
}
pre = tmp;
}
}
// 输出学生信息
void print_student(Student *node) {
printf("%s\t%s\t%d\t%d\t%d\t%d\n", node->id, node->name, node->score[0], node->score[1], node->score[2], node->score[3]);
}
// 输出学生表
void print_list(Student *head) {
Student *p = head->next;
printf("ID\tName\tChinese\tMath\tEnglish\tPolitics\n");
while (p != NULL) {
print_student(p);
p = p->next;
}
}
int main() {
Student *head = (Student *)malloc(sizeof(Student));
head->next = NULL;
int n, i, j, score[4];
char id[20], name[20];
printf("Please input the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Please input the information of student %d:\n", i + 1);
printf("ID: ");
scanf("%s", id);
printf("Name: ");
scanf("%s", name);
printf("Score of Chinese, Math, English, Politics: ");
for (j = 0; j < 4; j++) {
scanf("%d", &score[j]);
}
Student *node = create_node(id, name, score);
insert_node(head, node);
}
printf("The student list is:\n");
print_list(head);
printf("Please input the ID of the student you want to delete: ");
scanf("%s", id);
delete_node(head, id);
printf("After deleting, the student list is:\n");
print_list(head);
printf("After sorting by sum, the student list is:\n");
sort_by_sum(head);
print_list(head);
return 0;
}
```
阅读全文