c语言实训学生成绩管理系统代码举例
时间: 2023-08-12 14:42:59 浏览: 57
以下是一个简单的C语言实现学生成绩管理系统的代码示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct student {
char name[20]; // 学生姓名
int id; // 学生学号
char gender; // 学生性别
int age; // 学生年龄
int score; // 学生成绩
};
// 定义链表结构体
struct node {
struct student stu; // 学生结构体
struct node *next; // 指向下一个节点的指针
};
// 添加学生信息,返回头节点
struct node *add_student(struct node *head) {
struct student stu;
printf("请输入学生姓名:");
scanf("%s", stu.name);
printf("请输入学生学号:");
scanf("%d", &stu.id);
printf("请输入学生性别:");
scanf(" %c", &stu.gender);
printf("请输入学生年龄:");
scanf("%d", &stu.age);
printf("请输入学生成绩:");
scanf("%d", &stu.score);
// 将学生信息插入到链表中
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("内存分配失败!\n");
exit(1);
}
new_node->stu = stu;
new_node->next = head;
head = new_node;
return head;
}
// 删除学生信息,返回头节点
struct node *delete_student(struct node *head) {
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
if (head == NULL) {
printf("链表为空!\n");
return head;
}
struct node *p, *prev;
p = head;
prev = NULL;
while (p != NULL && p->stu.id != id) {
prev = p;
p = p->next;
}
if (p == NULL) {
printf("未找到该学生!\n");
return head;
}
if (prev == NULL) {
head = head->next;
} else {
prev->next = p->next;
}
free(p);
printf("删除成功!\n");
return head;
}
// 修改学生信息,返回头节点
struct node *modify_student(struct node *head) {
int id;
printf("请输入要修改的学生学号:");
scanf("%d", &id);
if (head == NULL) {
printf("链表为空!\n");
return head;
}
struct node *p;
p = head;
while (p != NULL && p->stu.id != id) {
p = p->next;
}
if (p == NULL) {
printf("未找到该学生!\n");
return head;
}
printf("请输入学生姓名:");
scanf("%s", p->stu.name);
printf("请输入学生学号:");
scanf("%d", &p->stu.id);
printf("请输入学生性别:");
scanf(" %c", &p->stu.gender);
printf("请输入学生年龄:");
scanf("%d", &p->stu.age);
printf("请输入学生成绩:");
scanf("%d", &p->stu.score);
printf("修改成功!\n");
return head;
}
// 查询学生信息
void query_student(struct node *head) {
int id;
printf("请输入要查询的学生学号:");
scanf("%d", &id);
if (head == NULL) {
printf("链表为空!\n");
return;
}
struct node *p;
p = head;
while (p != NULL && p->stu.id != id) {
p = p->next;
}
if (p == NULL) {
printf("未找到该学生!\n");
return;
}
printf("姓名:%s\n", p->stu.name);
printf("学号:%d\n", p->stu.id);
printf("性别:%c\n", p->stu.gender);
printf("年龄:%d\n", p->stu.age);
printf("成绩:%d\n", p->stu.score);
}
// 保存学生信息到文件
void save_student(struct node *head) {
FILE *fp;
fp = fopen("student.dat", "wb");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
struct node *p;
p = head;
while (p != NULL) {
fwrite(&(p->stu), sizeof(struct student), 1, fp);
p = p->next;
}
fclose(fp);
printf("保存成功!\n");
}
// 从文件中读取学生信息
struct node *load_student() {
FILE *fp;
fp = fopen("student.dat", "rb");
if (fp == NULL) {
printf("文件打开失败!\n");
return NULL;
}
struct node *head, *p;
head = NULL;
p = NULL;
while (1) {
struct student stu;
size_t size = fread(&stu, sizeof(struct student), 1, fp);
if (size == 0) {
break;
}
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("内存分配失败!\n");
exit(1);
}
new_node->stu = stu;
new_node->next = NULL;
if (head == NULL) {
head = new_node;
} else {
p->next = new_node;
}
p = new_node;
}
fclose(fp);
return head;
}
// 主函数
int main() {
struct node *head;
head = load_student();
int choice;
while (1) {
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:
head = add_student(head);
break;
case 2:
head = delete_student(head);
break;
case 3:
head = modify_student(head);
break;
case 4:
query_student(head);
break;
case 5:
save_student(head);
break;
case 6:
printf("程序已退出!\n");
return 0;
default:
printf("请输入正确的选项!\n");
break;
}
}
return 0;
}
```
注意:此代码仅供参考,实际应用中需要根据具体需求进行修改和完善。