c数据结构的学生成绩管理系统代码
时间: 2023-12-15 20:03:56 浏览: 141
数据结构------学生成绩管理系统 C代码
4星 · 用户满意度95%
以下是一个基于C语言的学生成绩管理系统代码,其中使用了链表数据结构来存储学生信息和成绩:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char name[20]; // 学生姓名
int id; // 学生学号
float score; // 学生成绩
struct student *next; // 指向下一个学生的指针
} Student;
// 定义链表头指针
Student *head = NULL;
// 添加学生信息
void add_student() {
Student *new_student = (Student*)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生学号:");
scanf("%d", &new_student->id);
printf("请输入学生成绩:");
scanf("%f", &new_student->score);
new_student->next = head;
head = new_student;
printf("添加成功!\n");
}
// 删除学生信息
void delete_student() {
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
Student *current = head;
Student *previous = NULL;
while (current != NULL) {
if (current->id == id) {
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("删除成功!\n");
return;
}
previous = current;
current = current->next;
}
printf("未找到该学生!\n");
}
// 修改学生成绩
void modify_score() {
int id;
printf("请输入要修改成绩的学生学号:");
scanf("%d", &id);
Student *current = head;
while (current != NULL) {
if (current->id == id) {
printf("请输入新的成绩:");
scanf("%f", ¤t->score);
printf("修改成功!\n");
return;
}
current = current->next;
}
printf("未找到该学生!\n");
}
// 查询学生成绩
void query_score() {
int id;
printf("请输入要查询成绩的学生学号:");
scanf("%d", &id);
Student *current = head;
while (current != NULL) {
if (current->id == id) {
printf("学生姓名:%s\n", current->name);
printf("学生学号:%d\n", current->id);
printf("学生成绩:%.2f\n", current->score);
return;
}
current = current->next;
}
printf("未找到该学生!\n");
}
// 显示所有学生信息
void display_all() {
Student *current = head;
while (current != NULL) {
printf("学生姓名:%s\n", current->name);
printf("学生学号:%d\n", current->id);
printf("学生成绩:%.2f\n", current->score);
current = current->next;
}
}
// 主函数
int main() {
int choice;
while (1) {
printf("\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生成绩\n");
printf("4. 查询学生成绩\n");
printf("5. 显示所有学生信息\n");
printf("6. 退出\n"); printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
modify_score();
break;
case 4:
query_score();
break;
case 5:
display_all();
break;
case 6:
exit(0);
default:
printf("无效的操作编号!\n");
}
}
return 0;
}
```
阅读全文