1,头文件
//标准函数输出
#include<stdio.h>
#include<stdlib.h>
//动态申请内存
#include<conio.h>
2,封装学生和结点数据
定义学生结构体,用于保存学生的学号,姓名,成绩信息,并使用typedef给struct _student类型起别名为student,方便使用。
//学生信息
typedef struct _Student
{
int stuNum;//学号
char name[20];//姓名
int score;//成绩
}Student;
定义结点结构体,用于保存链表中的结点数据,结点需要保存学生信息及下一个结点的地址,并使用typedef给struct _Node类型起名,方便使用
//结点信息
typedef struct _Node
{
Student student;
struct _Node* next;
}Node;
3,创建头结点
创建StudentManager.c,加入main()函数,并创建链表的头结点,定义head头指针指向头结点
//创建头结点
Node* head =(Node *)malloc(sizeof(Node));//存放空间
head->next = NULL;
4,系统功能提示
创建一个新的函数,用于建立系统功能提示
void welcome()
{
printf("*************************************\n");
printf("*\t学生成绩管理系统\t*\n");
printf("*************************************\n");
printf("*\t请选择功能列表\t\t*\n");
printf("*************************************\n");
printf("*\t录入学生信息\t\t*\n");
printf("*\t打印学生信息\t\t*\n");
printf("*\t统计学生信息\t\t*\n");
printf("*\t查找学生信息\t\t*\n");
printf("*\t修改学生信息\t\t*\n");
printf("*\t删除学生信息\t\t*\n");
printf("*\t按成绩排序\t\t*\n");
printf("*\t退出系统\t\t*\n");
printf("*************************************\n");
5,根据用户输入选择功能
_getch()函数主要用于读取用户的输入,而不会在控制台上显示输入的内容。它是在<conio.h>头文件中定义的。(_getch()函数的基本用法放在《关于学生信息系统的几个补充知识》中详细说明)
while (1)
{
welcome();
char c = _getch();
switch (c)
{
case '1'://录入学生信息
inputStudent(head);
break;
case '2'://打印学生信息
printStudent(head);
break;
case '3'://统计学生信息
countStudent(head);
break;
case '4'://查找学生信息
findStudent(head);
break;
case '5'://修改学生信息
modifyStudent(head);
break;
case '6'://删除学生信息
deleteSudent(head);
break;
case '7'://按成绩排序
sortStudent(head);
break;
case '8'://退出系统
system("cls");
printf("欢迎再次使用\n");
exit(0);
//return;
break;
default:
printf("请重新输入\n");
break;
}
}
return 0;
6,录入学生信息
void inputStudent(Node *head )//录入学生信息
{
Node* fresh = malloc(sizeof(Node));//调用一次输入一个学生信息
fresh -> next = NULL;//fresh指向头结点,首结点为空指针
printf("请输入学生的学号,姓名,成绩\n");
scanf("%d%s%d", &fresh->student.stuNum, fresh->student.name, &fresh->student.score);
Node* move = head;
while (move->next != NULL)
{
move = move->next;
}
//将学生插入到尾部
move->next = fresh;//尾部表示输入完毕,意思是结点移动到最后一个结点后结束循环
7,实现循环录入
while (move->next != NULL)
{
move = move->next;
}
以上代码会多次使用到,在该系统的程序中的逻辑起重要意义
8,程序暂停和清空控制台
saveStudent(head);
//暂停程序
system("pause");
//清空控制台
system("cls");
在以上代码中,system()函数的详情在<关于学生信息系统的几个补充知识>有提到,大家只需要记住这个形式可以暂停程序和清空控制台即可
9,打印学生信息
void printStudent(Node* head)
{
Node* move = head->next;
while (move != NULL)
{
printf("学号:%d 姓名:%s 成绩:%d\n", move->student.stuNum, move->student.name, move->student.score);
move = move->next;
}
//暂停程序
system("pause");
//清空控制台
system("cls");
打印程序中,要注意同样用到循环录入和程序暂停和清空控制台
10,统计学生人数
void countStudent(Node* head)
{
int count = 0;
Node* move = head->next;
while (move != NULL)
{
count++;
move = move->next;
}
printf("学生的总人数为:%d\n", count);
//暂停程序
system("pause");
//清空控制台
system("cls");
}
11,查找学生信息
void findStudent(Node* head)
{
printf("请输入要查找的学生的学号:");
int stuNum;
scanf("%d", &stuNum);
Node* move = head->next;//遍历学生信息
while (move != NULL)
{
if (stuNum == move->student.stuNum)
{
printf("学号:%d 姓名:%s 成绩:%d\n",move->student .stuNum ,move->student .name ,move->student .score );
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
}
12,修改学生信息
void modifyStudent(Node* head)//修改
{
printf("请输入要修改的学生的学号:");
int stuNum;
scanf("%d", &stuNum);
Node* move = head->next;
while (move != NULL)
{
if (move->student.stuNum == stuNum)
{
printf("请输入学生姓名,成绩\n");
scanf("%s%d", &move->student.name, &move->student.score);
saveStudent(head);
printf("修改成功\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
}
13,学生信息持久化
想要将信息持久化,就需要将信息存入文件,并且载入信息
void saveStudent(Node* head)
{//打开文件
FILE* file = fopen("./stu.info", "w");
Node* move = head->next;
if (file == NULL)
{
printf("打开文件失败\n");
return;
}
while (move != NULL)
{//将结构体写入文件
if (fwrite(&move->student, sizeof(Student), 1, file) != 1)
{
printf("写入失败\n");
return;
}
Node* move = head->next;
}
//关闭文件
fclose(file);
}
void loadStudent(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->student, sizeof(Student), 1, file )==1)
{
move -> next = fresh;
move = fresh;
fresh = malloc(sizeof(Student));
fresh->next = NULL;
}
free(fresh);
fclose(file);
printf("读取成功\n");
}
14,删除学生信息
void deleteSudent(Node* head)
{
printf("请输入要删除的学生信息\n");
int stuNum = 0;
scanf("%d", &stuNum);
Node* move = head;
while (move->next != NULL)
{
if (move->next->student.stuNum == stuNum)
{
Node* tmp = move->next;
move->next = move->next->next;
free(tmp);
tmp = NULL;
saveStudent(head);
printf("删除成功\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
printf("未找到学生信息\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
}
}
15,按照成绩排序
void sortStudent(Node* head)//冒泡排序的原理
{
Node* save = NULL;
Node* move = NULL;
for (Node* turn = head->next; turn->next != NULL; turn = turn->next)
{
for (Node* move = head->next; move->next != NULL; move = move->next)
{
if (move->student.score > move->next->student.score)
{
Student temp = move->student;
move->student = move->next->student;
move->next->student = temp;
}
}
save = move;
}
printStudent(head);
}
16,退出系统
case '8'://退出系统
system("cls");
printf("欢迎再次使用\n");
exit(0);
//return;
break;
17,源码分享
文件名:studentsystemManager.h
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//学生信息
typedef struct _Student
{
int stuNum;//学号
char name[20];//姓名
int score;//成绩
}Student;
//结点信息
typedef struct _Node
{
Student student;
struct _Node* next;
}Node;
void welcome();
void inputStudent(Node* head);
void printStudent(Node* head);
void countStudent(Node* head);
void findStudent(Node* head);
void saveStudent(Node* head);
void loadStudent(Node* head);
void modifyStudent(Node* head);
void deleteSudent(Node* head);
void sortStudent(Node* head);
studentsystemManager.c
#define _CRT_SECURE_NO_WARNINGS
#include"studentsystemManager.h"
int main()
{
//创建头结点
Node* head =(Node *)malloc(sizeof(Node));//存放空间
head->next = NULL;
loadStudent(head);
while (1)
{
welcome();
char c = _getch();
switch (c)
{
case '1'://录入学生信息
inputStudent(head);
break;
case '2'://打印学生信息
printStudent(head);
break;
case '3'://统计学生信息
countStudent(head);
break;
case '4'://查找学生信息
findStudent(head);
break;
case '5'://修改学生信息
modifyStudent(head);
break;
case '6'://删除学生信息
deleteSudent(head);
break;
case '7'://按成绩排序
sortStudent(head);
break;
case '8'://退出系统
system("cls");
printf("欢迎再次使用\n");
exit(0);
//return;
break;
default:
printf("请重新输入\n");
break;
}
}
return 0;
}
void welcome()
{
printf("*************************************\n");
printf("*\t学生成绩管理系统\t*\n");
printf("*************************************\n");
printf("*\t请选择功能列表\t\t*\n");
printf("*************************************\n");
printf("*\t录入学生信息\t\t*\n");
printf("*\t打印学生信息\t\t*\n");
printf("*\t统计学生信息\t\t*\n");
printf("*\t查找学生信息\t\t*\n");
printf("*\t修改学生信息\t\t*\n");
printf("*\t删除学生信息\t\t*\n");
printf("*\t按成绩排序\t\t*\n");
printf("*\t退出系统\t\t*\n");
printf("*************************************\n");
}
void inputStudent(Node *head )//录入学生信息
{
Node* fresh = malloc(sizeof(Node));//调用一次输入一个学生信息
fresh -> next = NULL;
printf("请输入学生的学号,姓名,成绩\n");
scanf("%d%s%d", &fresh->student.stuNum, fresh->student.name, &fresh->student.score);
Node* move = head;
while (move->next != NULL)
{
move = move->next;
}
//将学生插入到尾部
move->next = fresh;
saveStudent(head);
//暂停程序
system("pause");
//清空控制台
system("cls");
}
void printStudent(Node* head)
{
Node* move = head->next;
while (move != NULL)
{
printf("学号:%d 姓名:%s 成绩:%d\n", move->student.stuNum, move->student.name, move->student.score);
move = move->next;
}
//暂停程序
system("pause");
//清空控制台
system("cls");
}
void countStudent(Node* head)
{
int count = 0;
Node* move = head->next;
while (move != NULL)
{
count++;
move = move->next;
}
printf("学生的总人数为:%d\n", count);
//暂停程序
system("pause");
//清空控制台
system("cls");
}
void findStudent(Node* head)
{
printf("请输入要查找的学生的学号:");
int stuNum;
scanf("%d", &stuNum);
Node* move = head->next;
while (move != NULL)
{
if (stuNum == move->student.stuNum)
{
printf("学号:%d 姓名:%s 成绩:%d\n",move->student .stuNum ,move->student .name ,move->student .score );
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
}
void saveStudent(Node* head)
{//打开文件
FILE* file = fopen("./stu.info", "w");
Node* move = head->next;
if (file == NULL)
{
printf("打开文件失败\n");
return;
}
while (move != NULL)
{//将结构体写入文件
if (fwrite(&move->student, sizeof(Student), 1, file) != 1)
{
printf("写入失败\n");
return;
}
Node* move = head->next;
}
//关闭文件
fclose(file);
}
void loadStudent(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->student, sizeof(Student), 1, file )==1)
{
move -> next = fresh;
move = fresh;
fresh = malloc(sizeof(Student));
fresh->next = NULL;
}
free(fresh);
fclose(file);
printf("读取成功\n");
}
void modifyStudent(Node* head)//修改
{
printf("请输入要修改的学生的学号:");
int stuNum;
scanf("%d", &stuNum);
Node* move = head->next;
while (move != NULL)
{
if (move->student.stuNum == stuNum)
{
printf("请输入学生姓名,成绩\n");
scanf("%s%d", &move->student.name, &move->student.score);
saveStudent(head);
printf("修改成功\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
}
printf("未找到学生信息\n");
}
void deleteSudent(Node* head)
{
printf("请输入要删除的学生信息\n");
int stuNum = 0;
scanf("%d", &stuNum);
Node* move = head;
while (move->next != NULL)
{
if (move->next->student.stuNum == stuNum)
{
Node* tmp = move->next;
move->next = move->next->next;
free(tmp);
tmp = NULL;
saveStudent(head);
printf("删除成功\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
return;
}
move = move->next;
printf("未找到学生信息\n");
//暂停程序
system("pause");
//清空控制台
system("cls");
}
}
void sortStudent(Node* head)//冒泡排序的原理
{
Node* save = NULL;
Node* move = NULL;
for (Node* turn = head->next; turn->next != NULL; turn = turn->next)
{
for (Node* move = head->next; move->next != NULL; move = move->next)
{
if (move->student.score > move->next->student.score)
{
Student temp = move->student;
move->student = move->next->student;
move->next->student = temp;
}
}
save = move;
}
printStudent(head);
}