请写一个用c语言实现学生信息管理系统其功能包括学生信息链表创建、学生信息输入、学生信息 查询、学生信息修改、学生信息删除、学生信息输出(全部或部分输出)或保存到文件,学生相关排序的代码
时间: 2023-08-14 16:04:40 浏览: 67
【C语言】学生信息管理_链表
好的,以下是一个简单的学生信息管理系统的代码,实现了创建链表、输入、查询、修改、删除、输出和保存到文件的功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生信息结构体
typedef struct student {
int id; // 学号
char name[20]; // 姓名
int age; // 年龄
float score; // 成绩
struct student *next; // 指向下一个节点的指针
} Student;
// 创建新的学生信息节点
Student* create_student(int id, char* name, int age, float score) {
Student* s = (Student*)malloc(sizeof(Student));
s->id = id;
strcpy(s->name, name);
s->age = age;
s->score = score;
s->next = NULL;
return s;
}
// 在链表末尾添加新的学生信息节点
void add_student(Student* head, Student* new_student) {
Student* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_student;
}
// 输入新的学生信息
void input_student(Student* head) {
int id, age;
char name[20];
float score;
printf("请输入学号:");
scanf("%d", &id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入成绩:");
scanf("%f", &score);
Student* new_student = create_student(id, name, age, score);
add_student(head, new_student);
printf("添加成功!\n");
}
// 根据学号查询学生信息
void search_student(Student* head) {
int id;
printf("请输入要查询的学号:");
scanf("%d", &id);
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
printf("学号:%d,姓名:%s,年龄:%d,成绩:%.2f\n", p->id, p->name, p->age, p->score);
return;
}
p = p->next;
}
printf("未找到该学生!\n");
}
// 根据学号修改学生信息
void modify_student(Student* head) {
int id, age;
char name[20];
float score;
printf("请输入要修改的学号:");
scanf("%d", &id);
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
printf("请输入新的姓名:");
scanf("%s", name);
printf("请输入新的年龄:");
scanf("%d", &age);
printf("请输入新的成绩:");
scanf("%f", &score);
strcpy(p->name, name);
p->age = age;
p->score = score;
printf("修改成功!\n");
return;
}
p = p->next;
}
printf("未找到该学生!\n");
}
// 根据学号删除学生信息
void delete_student(Student* head) {
int id;
printf("请输入要删除的学号:");
scanf("%d", &id);
Student* p = head;
while (p->next != NULL) {
if (p->next->id == id) {
Student* temp = p->next;
p->next = temp->next;
free(temp);
printf("删除成功!\n");
return;
}
p = p->next;
}
printf("未找到该学生!\n");
}
// 输出所有学生信息
void output_all_students(Student* head) {
Student* p = head->next;
while (p != NULL) {
printf("学号:%d,姓名:%s,年龄:%d,成绩:%.2f\n", p->id, p->name, p->age, p->score);
p = p->next;
}
}
// 保存学生信息到文件
void save_students_to_file(Student* head) {
FILE* fp;
fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
Student* p = head->next;
while (p != NULL) {
fprintf(fp, "%d %s %d %.2f\n", p->id, p->name, p->age, p->score);
p = p->next;
}
fclose(fp);
printf("保存成功!\n");
}
// 根据成绩排序学生信息
void sort_students_by_score(Student* head) {
Student* p = head->next;
int count = 0;
while (p != NULL) {
count++;
p = p->next;
}
Student* students[count];
p = head->next;
for (int i = 0; i < count; i++) {
students[i] = p;
p = p->next;
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j]->score < students[j+1]->score) {
Student* temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
printf("按成绩排序后的学生信息:\n");
for (int i = 0; i < count; i++) {
printf("学号:%d,姓名:%s,年龄:%d,成绩:%.2f\n", students[i]->id, students[i]->name, students[i]->age, students[i]->score);
}
}
int main() {
Student* head = create_student(0, "", 0, 0);
int choice = 0;
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生信息\n");
printf("2. 查询学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 输出所有学生信息\n");
printf("6. 保存学生信息到文件\n");
printf("7. 按成绩排序学生信息\n");
printf("8. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
input_student(head);
break;
case 2:
search_student(head);
break;
case 3:
modify_student(head);
break;
case 4:
delete_student(head);
break;
case 5:
output_all_students(head);
break;
case 6:
save_students_to_file(head);
break;
case 7:
sort_students_by_score(head);
break;
case 8:
printf("再见!\n");
return 0;
default:
printf("无效的选项!\n");
break;
}
}
}
```
注意:这个代码只是一个示例,可能还有很多需要改进和完善的地方。比如,可以加上输入信息时的数据验证,避免输入不合法的数据;在删除学生信息时,可以让用户再次确认是否要删除等。
阅读全文