本文实现学生管理系统的主要思想是通过结构体链表来实现,及其简单的增删减改的功能,仅供学习和参考。
源文件部分:
#include "学生管理系统.h"
#include <conio.h>
int main()
{
node* head = malloc(sizeof(node));
head->next = NULL;
loadstu(head);
while (1)
{
welcome();
char c = _getch();
switch (c)
{
case '1':
intputstu(head);
break;
case'2':
printfstu(head);
break;
case '3':
countstu(head);
break;
case '4':
findstu(head);
break;
case '5':
modiyfstu(head);
break;
case '6':
deletestu(head);
break;
case '7':
sortstu(head);
break;
case'8':
sortstu1(head);
break;
case '9':
return ;
break;
default:
printf("请输入正确的指令\n");
system("pause");
system("cls");
break;
}
}
return 0;
}
void welcome()
{
printf("*********************************\n");
printf("*\t学生管理系统\t\t*\n");
printf("*********************************\n");
printf("*\t请选择功能列表\t\t*\n");
printf("*********************************\n");
printf("*\t1.录入学生信息\t\t*\n");
printf("*\t2.打印学生信息\t\t*\n");
printf("*\t3.统计学生人数\t\t*\n");
printf("*\t4.查找学生信息\t\t*\n");
printf("*\t5.修改学生信息\t\t*\n");
printf("*\t6.删除学生信息\t\t*\n");
printf("*\t7.按成绩排序\t\t*\n");
printf("*\t8.按学号排序\t\t*\n");
printf("*\t9.退出系统\t\t*\n");
printf("*********************************\n");
}
void intputstu(node* head)
{
node* fresh = malloc(sizeof(node));
fresh->next = NULL;
printf("请输入学生的学号,姓名,成绩");
scanf("%d%s%d", &fresh->stu.stunum, fresh->stu.name, &fresh->stu.score);
node* move = head;
while (move->next != NULL)
{
move = move->next;
}
move->next = fresh;
savestu(head);
system("pause");
system("cls");
}
void printfstu(node* head)
{
node* move = head->next;
while (move != NULL)
{
printf("学号:%d 姓名:%s 成绩:%d\n", move->stu.stunum, move->stu.name, move->stu.score);
move = move->next;
}
system("pause");
system("cls");
}
void countstu(node* head)
{
int sum = 0;
node* move = head->next;
while (move != NULL)
{
sum++;
move = move->next;
}
printf("学生的总人数为:%d\n", sum);
system("pause");
system("cls");
}
void findstu(node* head)
{
printf("请输入要查找的学生学号:");
int num;
scanf("%d", &num);
node* move = head->next;
while (move != NULL)
{
if (num == move->stu.stunum)
{
printf("学号:%d 姓名:%s 成绩:%d\n", move->stu.stunum, move->stu.name, move->stu.score);
system("pause");
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息");
system("pause");
system("cls");
}
void savestu(node* head)
{
FILE* file=fopen("./stu.info", "w");
node* move = head->next;
while (move != NULL)
{
if (fwrite(&move->stu,sizeof(student), 1, file) != 1)
{
printf("写入失败\n");
return;
}
move = move->next;
}
fclose(file);
}
void loadstu(node* head)
{
FILE* file = fopen("./stu.info", "r");
if (!file)
{
printf("没有学生文件,跳过读取\n");
return;
}
node* fresh = malloc(sizeof(node));
fresh->next = NULL;
node* move = head;
while (fread(&fresh->stu, sizeof(student), 1, file) == 1)
{
move->next = fresh;
move = fresh;
fresh = malloc(sizeof(student));
fresh->next = NULL;
}
free(fresh);
fclose(file);
printf("读取成功\n");
}
void modiyfstu(node* head)
{
printf("请输入要修改的学生学号:");
int stunum;
scanf("%d", &stunum);
node* move = head->next;
while (move != NULL)
{
if (move->stu.stunum == stunum)
{
printf("%请输入学生姓名,成绩\n");
scanf("%s%d", move->stu.name, &move->stu.score);
savestu(head);
printf("修改成功\n");
system("pause");
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
system("pause");
system("cls");
}
void deletestu(node* head)
{
printf("请输入要删除的学生学号");
int stunum = 0;
scanf("%d", &stunum);
node* move = head;
while (move->next != NULL)
{
if (move->next->stu.stunum == stunum)
{
node* tmp = move->next;
move->next = move->next->next;
free(tmp);
tmp = NULL;
savestu(head);
printf("删除成功\n");
system("pause");
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
system("pause");
system("cls");
}
void sortstu(node* head)
{
for (node* turn = head->next; turn->next != NULL; turn = turn->next)
{
for (node* move = head->next; move->next != NULL; move = move->next)
{
if (move->stu.score > move->next->stu.score)
{
student temp = move->stu;
move->stu = move->next->stu;
move->next->stu = temp;
}
}
}
printf("排序成功!\n");
printfstu(head);
system("cls");
}
void sortstu1(node* head)
{
for (node* turn = head->next; turn->next != NULL; turn = turn->next)
{
for (node* move = head->next; move->next != NULL; move = move->next)
{
if (move->stu.stunum > move->next->stu.stunum)
{
student temp = move->stu;
move->stu = move->next->stu;
move->next->stu = temp;
}
}
}
printf("排序成功!\n");
printfstu(head);
system("cls");
}
头文件部分:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct _student
{
int stunum;
char name[20];
int score;
}student;
typedef struct _node
{
student stu;
struct _node* next;
}node;
void welcome();
void intputstu(node* head);
void printfstu(node* head);
void countstu(node* head);
void findstu(node* head);
void savestu(node* head);
void loadstu(node* head);
void modiyfstu(node* head);
void deletestu(node* head);
void sortstu(node* head);
void sortstu1(node* head);