学生管理系统,是由链表来实现,将每个学生看作一个结点,从而对学生进行管理。下面,我分别来说几个学生管理系统的基本操作。
1,学生管理系统的创建
struct student//学生信息
{
int num;
char name[20];
int score;
};
struct Node//学生结点
{
struct student student;
struct Node* next;
};
struct Node* createhead()//创建头节点
{
struct Node* headNode = (struct Node*)malloc(sizeof(Node));
headNode->next = NULL;
}
2,学生信息的插入
链表中结点的插入分别为头插法,尾插法和指定位置插入法,由于后面我们要对学生信息进行排序,所以没必要用指定位置插入法,这里我吗采用尾插法
void inputstudent(struct Node* headNode)
{
struct Node* newNode = (struct Node*)malloc(sizeof(Node));
newNode->next = NULL;
printf("请输入学号,姓名,成绩\n");
scanf("%d%s%d", &newNode->student.num, newNode->student.name, &newNode->student.score);
struct Node* pMove = headNode;
while (pMove != NULL)
{
pMove = pMove->next;
}
pMove->next = newNode;
savestudent(headNode);//将信息保存到文件中
system("pause");
}
3,打印学生信息
void printstudent(struct Node*headNode)
{
struct Node* pMove = headNode->next;
while (pMove)
{
printf("学号:%d 姓名:%s 成绩:%d", pMove->student.num, pMove->student.name, pMove->student.score);
pMove = pMove->next;
}
system("pause");
}
4,对学生人数进行统计
void countstudent(struct Node* headNode)
{
int count = 0;//首先设总人数为0
struct Node* pMove = headNode->next;//创建一个结点指向第一个结点,也就是头节点的下一个结点
while (pMove)
{
count++;
pMove = pMove->next;
}
printf("学生总数为:%d\n", count);
system("pause");
}
5,对学生进行查找
这里,我采用的是对学生的学号进行查找,当然,也可以通过其他信息进行查找
void findstudent(struct Node* headNode)
{
int stunum;
printf("请输入要寻找的学生学号\n");
scanf("%d", &stunum);
struct Node* pMove = headNode->next;//从第一个学生开始找起
while (pMove)
{
if (pMove->student.num == stunum)//如果该节点指向的学号与要查找的学号相等,则输出该学生的信息
{
printf("学号:%d 姓名:%s 成绩:%d", pMove->student.num, pMove->student.name, pMove->student.score);
system("pause");
return;
}
pMove = pMove->next;
}
printf("无法找到该学生\n");
system("pause");
}
6,对学生信息进行保存
我们对学生信息进行的很多操作都需要经过这一步,包括修改,插入,删除,而我们保存的信息都存放在一个文件中,从而使得学生信息的永久化
void sevestudent(struct Node* headNode)
{
FILE* file = fopen(".stu/infoa", 'w');
struct Node* pMove = headNode->next;
while (pMove)
{
if (fwrite(&pMove->student, sizeof(student), 1, file) != 1)
{
printf("写入失败\n");
return;
}
pMove = pMove->next;
}
system("pause");
fclose(file);
}
7,学生信息的读取
既然已经对学生信息保存了,那就少不了对信息的读取。我们每一次启动该系统,只有将我们保存的信息读取出来后,才能对其进行操作
void loadstudent(struct Node* headNode)
{
FILE* file = fopen("./stu.info", "r");
if (!file)
{
printf("没有学生文件,跳过读取\n");
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(Node));
newNode->next = NULL;
struct Node* pMove = headNode;
while (fread(&newNode->student, sizeof(student), 1, file) == 1)
{
pMove->next = newNode;
pMove = newNode;
newNode = (struct Node*)malloc(sizeof(student));
newNode->next = NULL;
}
free(newNode);
fclose(file);
printf("读取成功\n");
}
8,对学生信息进行排序
对学生信息进行排序的方法有很多种,我这里使用最长见的冒泡排序,排序的参数也有多种,有学号,姓名,成绩等等,我这里采用对成绩进行从低到高进行排序
void sortstudent(struct Node* headNode)
{
for (struct Node* turn = headNode->next; turn->next != NULL; turn = turn->next)
{
for (struct Node* pMove = headNode->next; pMove->next != NULL; pMove = pMove->next)
{
if (pMove->student.score > pMove->next->student.score)//这里基本上就是冒泡排序的步骤了
{
int num1, score1;
char name1[20];
num1 = pMove->student.num;
pMove->student.num = pMove->next->student.num;
pMove->next->student.num = num1;
score1 = pMove->student.score;
pMove->student.score = pMove->next->student.score;
pMove->next->student.score = score1;
strcpy(name1, pMove->student.name);
strcpy(pMove->student.name, pMove->next->student.name);
strcpy(pMove->next->student.name, name1);
}
}
}
printf("排序完成\n");
}
9,对学生信息进行修改
void modifystudent(struct Node* headNode)
{
printf("输入要修改的学生的学号:\n");
int stunum;
scanf("%d", &stunum);
struct Node* pMove = headNode->next;
while (pMove != NULL)
{
if (pMove->student.num == stunum)
{
printf("请输入学生姓名,成绩:\n");
scanf("%s%d", pMove->student.name, &pMove->student.score);
savestudent(headNode);
printf("修改学生信息成功\n");
system("pause");
system("cls");//清空控制台
return;
}
pMove = pMove->next;
}
printf("未找到学生信息\n");
system("pause");
system("cls");
}
10,对学生进行删除
其实修改,查找,删除的原理都是一样的,都是从开头开始寻找,直到找到需要进行操作的结点,就进行相应的操作,代码如下
void deletestudent(struct Node* headNode)
{
printf("请输入要删除学生学号:\n");
int stunum = 0;
scanf("%d", &stunum);
struct Node* pMove = headNode;
while (pMove->next)
{
if (pMove->next->student.num == stunum)
{
struct Node* tem = pMove->next;
pMove->next = pMove->next->next;
tem->next = NULL;
free(tem);
savestudent(headNode);
printf("删除成功\n");
system("pause");
system("cls");
return;
}
pMove = pMove->next;
}
printf("未找到学生\n");
system("pause");
system("cls");
}
11,开始页面
开始页面就没什么好说的,这个可以根据个人的喜好来设计,无非就是一些简单的打印,就怎么方便怎么来
void welcome()
{
printf("*********************************\n");
printf("*\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("*********************************\n");
}
12,主函数
//在最上面要进行函数声明
void inputstudent(struct Node* headNode);
void printstudent(struct Node* headNode);
void countstudent(struct Node* headNode);
void findstudent(struct Node* headNode);
void savestudent(struct Node* headNode);
void loadstudent(struct Node* headNode);
int main()
{
struct Node* headNode = createhead();
headNode->next = NULL;
loadstudent(headNode);
welcome();
while (1)
{
char c = _getch();
switch (c)
{
case '1':
inputstudent(headNode);
break;
case '2':
printstudent(headNode);
break;
case '3':
countstudent(headNode);
break;
case '4':
findstudent(headNode);
break;
case '5':
modifystudent(headNode);
break;
case '6':
deletestudent(headNode);
break;
case '7':
sortstudent(headNode);
break;
case '8':
system("cls");
printf("拜拜了您嘞\n");
exit(0);
break;
default:
break;
}
}
return 0;
}