c:链表实现学生成绩管理系统

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

typedef struct tagStudent

{

char szName[10]; //姓名

int nAge; //性别

int nStuNum; //学号

int nScore; //成绩

}Student;

typedef struct tagNode

{

Student stu; //学生信息

struct tagNode* pNext; //指向下一个学生

}Node;

Node *g_pHead = NULL; //定义第一个学生

//菜单栏

void title()

{

printf("==============欢迎使用高校学生成绩管理系统V1.0==============\n");

printf("\t=======请选择功能列表=======\n");

printf("\t\t1.录入学生信息\n");

printf("\t\t2.打印学生信息\n");

printf("\t\t3.保存学生信息\n");

printf("\t\t4.读取学生信息\n");

printf("\t\t5.统计所有学生人数\n");

printf("\t\t6.查找学生信息\n");

printf("\t\t7.修改学生信息\n");

printf("\t\t8.删除学生信息\n");

printf("\t\t0.退出系统\n");

}

//录入学生信息

void InputStudent();

//打印学生信息

void PrintStudent();

//保存学生信息

void SaveStudent();

//读取学员信息

void ReadStudent();

//统计所有学生人数

int CountStudent();

//查找学生信息

Node* FindStudent();

//修改某个学生的信息

void ModifyStudent();

//删除学生信息

void DeleteStudent();

int main()

{

title();

while (1)

{

char ch;

printf("请输入菜单选项号:");

ch = _getch();

//scanf("%c", &ch);

switch (ch)

{

case '1'://录入学生信息

system("cls");

title();

InputStudent();

break;

case '2'://打印学生信息

system("cls");

title();

PrintStudent();

break;

case '3'://保存学生信息

system("cls");

title();

SaveStudent();

break;

case '4'://读取学员信息

system("cls");

title();

ReadStudent();

break;

case '5'://统计所有学生人数

printf("当前学生总人数为:%d\n", CountStudent());

break;

case '6'://查找学生信息

{

system("cls");

title();

Node *pNode = FindStudent();

if (pNode != NULL)

{

printf("学号:%d\t姓名:%s\t年龄:%d\t成绩:%d\n", pNode->stu.nStuNum, pNode->stu.szName, pNode->stu.nAge, pNode->stu.nScore);

}

break;

}

case '7'://修改某个学生的信息

system("cls");

title();

ModifyStudent();

break;

case '8'://删除学生信息

system("cls");

title();

DeleteStudent();

break;

case '0':

printf("欢迎再次使用!\n");

return 0;

break;

default:

printf("您的输入有误,请重新输入!\n");

break;

}

}

return 0;

}

//录入学生信息

void InputStudent()

{

printf("\n请输入学生信息:姓名 学号 年龄 成绩\n");

Node* p;

p = g_pHead;

while (g_pHead != NULL && p->pNext != NULL)

{

p = p->pNext;

}

//为新的学生分配一个空间

Node* pNewNode = (Node*)malloc(sizeof(Node));

pNewNode->pNext = NULL;

if (g_pHead == NULL)

{

g_pHead = pNewNode;

p = g_pHead;

}

else

{

p->pNext = pNewNode;//p的下一个节点为pNewNode

}

//输入新的学生数据

scanf("%s %d %d %d", pNewNode->stu.szName, &pNewNode->stu.nStuNum, &pNewNode->stu.nAge, &pNewNode->stu.nScore);

printf("\n数据添加成功....\n");

}

//打印学生信息

void PrintStudent()

{

printf("打印所有学生信息\n");

Node* p;

p = g_pHead;

printf("学号:\t\t姓名:\t\t年龄:\t\t成绩\n");

while (p != NULL)

{

printf("%-16d%-16s%-16d%-16d\n",

p->stu.nStuNum,

p->stu.szName,

p->stu.nAge,

p->stu.nScore);

p = p->pNext;

}

}

//保存学生信息

void SaveStudent()

{

FILE *pFile = fopen("C:\\stuinfo.data", "w");

if (pFile == NULL)

{

pFile = fopen("D:\\stuinfo.data", "w");

if (pFile == NULL)

{

printf("打开文件失败\n");

return;

}

}

//写入数据

Node *p;

p = g_pHead;

while (p != NULL)

{

fprintf(pFile, "%d %s %d %d\n", p->stu.nStuNum, p->stu.szName, p->stu.nAge, p->stu.nScore);

p = p->pNext;

}

printf("数据保存成功\n");

//关闭文件

fclose(pFile);

}

//读取学员信息

void ReadStudent()

{

//首先删除链表数据,然后从文件中读取数据

Node *p, *p2;

p = p2 = g_pHead;

while (p2 != NULL)

{

//卒个删除链表中的数据

p = p->pNext;

free(p2);

p2 = p;

}

g_pHead = NULL;

//打开文件

FILE *pFile = fopen("C:\\stuinfo.data", "w");

if (pFile == NULL)

{

pFile = fopen("D:\\stuinfo.data", "w");

if (pFile == NULL)

{

printf("打开文件失败\n");

return;

}

}

//读取数据

while (!feof(pFile))

{

//分配空间以存储数据

Node *pTemp = (Node*)malloc(sizeof(Node));

//从文件中读取

fscanf(pFile, "%d %s %d %d\n", &pTemp->stu.nStuNum, &pTemp->stu.szName, &pTemp->stu.nAge, &pTemp->stu.nScore);

//创建链表

if (g_pHead == NULL)

{

g_pHead = pTemp;

p = g_pHead;

}

else

{

p->pNext = pTemp;//p的下一个节点为temp

p = p->pNext;

p->pNext = NULL;

}

}

//关闭文件

fclose(pFile);

}

//统计所有学生人数

int CountStudent()

{

int nCount = 0;

//先把文件中的数据读取到链表中

ReadStudent();

Node *p;

p = g_pHead;

while (p != NULL)

{

nCount++;

p = p->pNext;

}

return nCount;

}

//查找学生信息

Node* FindStudent()

{

int nStuNum;

printf("请输入要查找的学生的学号:\n");

scanf("%d", &nStuNum);

//先从文件中读取到链表中

ReadStudent();

Node* p;

p = g_pHead;

while (p != NULL)

{

if (p->stu.nStuNum == nStuNum)

{

return p;

}

p = p->pNext;

}

//遍历完链表也没有找到信息

if (p == NULL)

{

printf("没有该学生的信息\n");

return NULL;

}

return NULL;

}

//修改某个学生的信息

void ModifyStudent()

{

int nStuNum;

printf("请输入要修改学生信息的学号:\n");

scanf("%d", &nStuNum);

//先把文件中的数据读取到链表中

ReadStudent();

Node* p;

p = g_pHead;

while (p != NULL)

{

if (p->stu.nStuNum == nStuNum)

{

printf("请输入要修改的信息:姓名 学号 年龄 成绩\n");

scanf("%s %d %d %d", p->stu.szName, &p->stu.nStuNum, &p->stu.nAge, &p->stu.nScore);

printf("修改成功!\n");

break;

}

p = p->pNext;

}

if (p == NULL)

{

printf("没有该学生信息!\n");

}

}

//删除学生信息

void DeleteStudent()

{

int nStuNum;

printf("请输入要删除学生信息的学号:\n");

scanf("%d", &nStuNum);

//先把文件中的数据读取到链表

ReadStudent();

Node *p, *p2;

p = g_pHead;

//判断是否是头结点

if (g_pHead->stu.nStuNum == nStuNum)

{

p2 = g_pHead;

g_pHead = g_pHead->pNext;//头指针指向删除元素后的首元素

free(p2);//释放节点

return;

}

//不是头结点

while (p->pNext != NULL)

{

if (p->pNext->stu.nStuNum == nStuNum)

{

p2 = p->pNext;

p->pNext = p->pNext->pNext;//删除该节点指针跳过此节点

free(p2);

return;

}

p = p->pNext;//指向下一个节点

if (p->pNext == NULL)

{

//判断是否到了链表的结尾

break;

}

}

if (p->pNext == NULL)

{

printf("没有该学生信息\n");

}

}

相关推荐
宇卿.几秒前
Java键盘输入语句
java·开发语言
Amo Xiang10 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
friklogff23 分钟前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
重生之我在20年代敲代码1 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286115 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py5 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy5 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
jiao000016 小时前
数据结构——队列
c语言·数据结构·算法