(创作不易,感谢有你,你的支持,就是我前行的最大动力,如果看完对你有帮助,请留下您的足迹)
今天我们来试着用链表创建一个学生成绩管理系统
其中主要包含的知识点为单链表(
http://t.csdn.cn/gsAdH
)和C语言文件操作(
http://t.csdn.cn/j3kwe
) 相关知识点的链接我已经给出
总体代码就是这样,如果大家发现bug或者有更好的方法 ,欢迎大家一起来讨论呀,一起加油
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
//学生信息
typedef struct student
{
char stuNum[20];
char name[20];
int score;
}Student;
//节点信息
typedef struct node
{
Student student;
struct node* next;
}node;
void welcome()
{
printf("**** 链式学生管理系统 *******\n");
printf("*******************************\n");
printf("****0.退出功能 1.录入功能******\n");
printf("****2.打印功能 3.统计功能******\n");
printf("****4.查找功能 5.修改功能******\n");
printf("****6.删除功能 7.排序功能******\n");
printf("*******************************\n");
}
//保存学生信息
void savestudent(node* head)
{
//打开文件
FILE* file = fopen("C:\\Users\\周星辰\\Desktop\\学生成绩管理系统.txt", "w");
if (file == NULL)
{
printf("打开文件失败\n");
return;
}
node* move = head->next;
while (move != NULL)
{
//写入文件
if (fwrite(&move->student, sizeof(student), 1, file) != 1)
{
printf("保存%s出现错误\n", move->student.name);
return;
}
move = move->next;
}
//关闭文件
fclose(file);
}
//录入学生信息
void inputstudent(node*head)
{
node* fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
printf("请输入学生学号\n");
scanf("%s", fresh->student.stuNum);
printf("请输入学生姓名\n");
scanf("%s", fresh->student.name);
printf("请输入学生成绩\n");
scanf("%d", &fresh->student.score);
node* move = head;
while (move->next != NULL)
{
move = move->next;
}
//将学生插入到尾部
move->next = fresh;
savestudent(head);
system("cls");//清屏
printf("学生信息录入成功\n");
system("pause");//暂停程序
system("cls");//清屏
}
//打印学生信息
void printStudent(node* head)
{
node* move = head->next;
printf("%10s\t%10s\t%10s\n","学号","姓名","成绩");
while (move != NULL)
{
/*printf("学号:%s 姓名:%s 成绩:%d\n",
move->student.stuNum,
move->student.name,
move->student.score);*/
printf("%10s\t%10s\t%10d\n",
move->student.stuNum,
move->student.name,
move->student.score);
move = move->next;
}
system("pause");//暂停程序
system("cls");//清屏
}
//统计总人数
void countstudent(node* head)
{
node* move = head->next;
int count = 0;
while (move != NULL)
{
count++;
move = move->next;
}
printf("学生总人数为:%d\n", count);
system("pause");//暂停程序
system("cls");//清屏
}
//查找学生信息
void findstudent(node* head)
{
printf("请输入学生的学号:\n");
char a[20];
scanf("%s", a);
node* move = head->next;
while (move != NULL)
{
if (strcmp(move->student.stuNum, a) == 0)
{
printf("%10s\t%10s\t%10s\n", "学号", "姓名", "成绩");
printf("%10s\t%10s\t%10d\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 loadstudent(node* head)
{
//打开文件
FILE* file = fopen("C:\\Users\\周星辰\\Desktop\\学生成绩管理系统.txt", "r");
if (!file)
{
printf("未找到学生文件\n");
return;
}
//创建一个节点
node* fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
node* move = head;
while (fread(&fresh->student, sizeof(student), 1, file) == 1)
{
move->next = fresh;
move = fresh;
fresh = (node*)malloc(sizeof(node));
fresh->next = NULL;
}
//最后多定义一个fresh,要将它释放掉
free(fresh);
//关闭文件
fclose(file);
}
//修改学生信息
void modifystudent(node* head)
{
printf("请输入要修改的学生的学号:");
char a[20];
scanf("%s", &a);
node* move = head->next;
while (move != NULL)
{
if (strcmp(move->student.stuNum, a) == 0)
{
printf("请输入学生学号\n");
scanf("%s", move->student.stuNum);
printf("请输入学生姓名\n");
scanf("%s", move->student.name);
printf("请输入学生成绩\n");
scanf("%d", &move->student.score);
savestudent(head);
printf("修改成功\n");
system("pause");//暂停程序
system("cls");//清屏
return;
}
move = move->next;
}
printf("未找到学生信息\n");
system("pause");//暂停程序
system("cls");//清屏
}
//删除学生信息
void deletestudent(node* head)
{
printf("输入要删除学生学号:\n");
char a[20];
scanf("%s", &a);
node* move = head;
while (move->next != NULL)
{
if (strcmp(move->next->student.stuNum, a) == 0)
{
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)
{
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;
}
}
}
printStudent(head);
}
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':
deletestudent(head);
break;
case '7':
sortstudent(head);
break;
case '0':
printf("退出程序\n");
exit(0);
break;
default:
printf("输入错误\n");
break;
}
}
return 0;
}
版权声明:本文为weixin_73295475原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。