C语言学生管理系统的实现,废话少说,代码如下,后续写个说明文档。
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//学生节点
typedef struct _STU
{
char arrStuID[15];//学号
char arrName[10];//姓名
int iStuScore;//
struct _STU* pNext;
}StuNode;
//头尾指针
StuNode* g_pHead = NULL; //global 全球;在声明全局变量时,加g_ 编程规范
StuNode* g_pTail = NULL;
//学生管理系统页面
void ShowOrder();
//Add a student information_tail
void AddStuMsgTail(char* arrStuID, char* arrName, int iStuScore);
//Add a student information_head
void AddStuMsgHead(char* arrStuID, char* arrName, int iStuScore);
//Add a student information_rand
void AddStuMsgRand(char* arrStuID, char* arrName, int iStuScore, char* index);
//查找指定元素
StuNode* SelectNode(char* index);
//clear list
void FreeList();
//打印数据
void ShowStuDate();
//头删除
void HeadDeleteNode();
//尾删除
void TailDeleteNode();
//删除指定节点
void DeleteRandNode(char* arrIndex);
//Save the owner’s information in a file
void SaveStuToFile();
// Read the owner information in the file
void ReadStuForFile();
//打印指定学生信息
void ShowStuDateRand(char* arrIndex);
//根据学号排序 从小到大
void PaiXvStuID();
//根据成绩进行排序 从大到小
void PaiXvStuScore();
//游戏模式
void ModeGame();
//删除重复学生信息
int DelDupStuData();
void DelDupStuData2();
void sortID();
int main(void)
{
char arrStuID[15] = {
'\0' };
char arrName[10] = {
'\0' };
char arrIndex[15] = {
'\0' };
StuNode* pTemp = NULL;
unsigned int iStuScore = 0; //编程规范 命名与初始化 变量名前加 i 代表int类型
ShowOrder();
int nFlag = 1;
int iOrder = -1;
ReadStuForFile();
while (nFlag)
{
printf("请输入指令(11、查看指令):\n");
scanf("%d", &iOrder);
switch (iOrder)
{
case 1:
// Add a student information_tail
printf("输入学号\n");
scanf_s("%s", arrStuID, 15);
printf("输入姓名\n");
scanf_s("%s", arrName, 10);
printf("输入成绩\n");
scanf_s("%u", &iStuScore);
AddStuMsgTail(arrStuID, arrName, iStuScore);
break;
case 2:
//Add a student information_head
printf("输入学号\n");
scanf_s("%s", arrStuID, 15);
printf("输入姓名\n");
scanf_s("%s", arrName, 10);
printf("输入成绩\n");
scanf_s("%u", &iStuScore);
AddStuMsgHead(arrStuID, arrName, iStuScore);
break;
case 3:
//任意位置添加学生信息
printf("输入学生学号\n");
scanf_s("%s", arrIndex, 15);
pTemp = SelectNode(arrIndex);
if (NULL != pTemp)
{
printf("输入学号\n");
scanf_s("%s", arrStuID, 15);
printf("输入姓名\n");
scanf_s("%s", arrName, 10);
printf("输入成绩\n");
scanf_s("%u", &iStuScore);
AddStuMsgRand(arrStuID, arrName, iStuScore, arrIndex);
}
break;
case 4:
// Find information about a specified student
printf("输入学生学号\n");
scanf_s("%s", arrIndex, 15);
pTemp = SelectNode(arrIndex);
if (NULL != pTemp)
{
printf("学号:%s\t姓名:%s\t成绩:%d\n", pTemp->arrStuID, pTemp->arrName, pTemp->iStuScore);
}
break;
case 5:
// Modify the information of the specified student
printf("输入学生学号\n");
scanf_s("%s", arrIndex, 15);
pTemp = SelectNode(arrIndex);
if (NULL != pTemp)
{
printf("修改学号为:\n");
scanf_s("%s", pTemp->arrStuID, 15);
printf("修改姓名为:\n");
scanf_s("%s", pTemp->arrName, 10);
printf("修改成绩为:\n");
scanf_s("%u", &pTemp->iStuScore);
}
break;
case 6:
//Save the owner’s information in a file
SaveStuToFile();
break;
case 7:
// Read the owner information in the file
ReadStuForFile();
break;
case 8:
// Delete the information of the specified student
printf("输入删除的学生学号\n");
scanf_s("%s", arrIndex, 15);
//查找并返回学生节点
pTemp = SelectNode(arrIndex);
//删除节点
if (NULL != pTemp)
{
//调用删除函数
DeleteRandNode(arrIndex);
}
break;
case 10:
//Recover deleted student information
FreeList();
ReadStuForFile();
break;
case 9:
//Show information about all students
ShowStuDate();
break;
case 11:
// 展示所有指令
ShowOrder();
break;
case 12:
// TODO 游戏模式
break;
case 13:
//清空控制台
system("cls");
break;
case 14:
//TODO 根据总分进行排序 从大到小
break;
case 15:
//TODO 根据学号进行排序,从小到大
sortID();
break;
case 16:
//TODO 删除重复学生信息
DelDupStuData();
break;
case 17:
DelDupStuData2();
break;
case 0:
//退出
system("cls");
printf("欢迎使用系统\n");
nFlag = 0;
break;
default:
printf("输入指令不对\n");
}
}
//保存
SaveStuToFile();
//释放链表
FreeList();
system("pause");
return 0;
}
void sortID()
{
StuNode* pTemp1 = g_pHead;
StuNode* pTemp2 = g_pHead;
while (pTemp1 != NULL)
{
int sort1 = atoi(pTemp1->arrStuID);
while (NULL != pTemp1)
{
int sort2 = atoi(pTemp2->pNext->arrStuID);
//printf("%d %d\n", sort1, sort2);
if (sort1 > sort2 && pTemp1 == g_pHead)
{
pTemp1->pNext = pTemp2->pNext;
pTemp2->pNext = pTemp1;
}
else
{
StuNode* pT = g_pHead;
pTemp1->pNext = pTemp2->pNext;
pTemp2->pNext = pTemp1;
while (pT->pNext != pTemp1)
{
pT = pT->pNext;
}
pT->pNext = pTemp2;
}
pTemp2 = pTemp2->pNext;
}
pTemp2 = pTemp1->pNext;
pTemp1 = pTemp2;
}
}
//删除重复学生信息
void DelDupStuData2()
{
if (NULL == g_pHead)
{
printf("信息为空\n");
return NULL;
}
StuNode* pTemp1 = g_pHead;
StuNode* pTemp2 = g_pHead;
while (pTemp1 != NULL)
{
if (pTemp2->pNext == g_pTail)
{
if (0 == strcmp(pTemp2->arrStuID, g_pTail->arrStuID)) //相等返回 0
{
free(g_pTail);
g_pTail = pTemp2;
g_pTail->pNext = NULL;
pTemp1 = pTemp2;
break;
}
}
else
{
while (pTemp1 != NULL)
{
pTemp1 = pTemp1->pNext;
if (NULL == pTemp1)
{
break;
}
if (pTemp2->arrStuID == g_pTail->arrStuID)
{
StuNode* px = g_pHead->pNext;
while (px != g_pTail)
{
px = px->pNext;
}
pTemp2->pNext = px->pNext;
free(px);
pTemp1 = px->pNext;
continue;
if(g_pHead->arrStuID == g_pTail->arrStuID)
g_pTail = px;
g_pTail->pNext = NULL;
continue;
}
else if (0 == strcmp(pTemp2->arrStuID, pTemp1->arrStuID)) //相等返回 0
{
StuNode* pT = pTemp1;
//pT = pT->pNext;
pTemp2->pNext = pT->pNext;
pTemp1 = pTemp2;
free(pT);
//ShowStuDate();
}
}
}
pTemp1 = pTemp2->pNext;
pTemp2 = pTemp1;
}
}
int DelDupStuData()
{
//TODO 问题: 首尾不同,中间相同删不掉重复的
//TODO 问题: 首尾相同,中间不同,中间的全没了
//TODO 问题: 全部相同的情况下,删除重复的始终还剩两个
//循环有问题
//删除重复学生信息
if (NULL == g_pHead)
{
printf("信息为空\n");
return NULL;
}
char* StuID = {
NULL };
//char* p = (char*)malloc(sizeof(char)*15);
StuNode* pTemp1 = g_pHead;
StuNode* pTemp2 = g_pHead;
while (pTemp2 != NULL)
{
StuID = pTemp2->arrStuID;
while (pTemp1 != NULL)
{
if (NULL == pTemp1)
{
break;
}
pTemp1 = pTemp1->pNext;
//字符串比较
if (NULL == pTemp1)
{
break;
}
if (0 == strcmp(StuID, pTemp1->arrStuID)) //相等返回 0
{
//查找并返回学生节点
//pTemp1 = SelectNode(pTemp1->arrStuID);
if (g_pHead->pNext == g_pTail)
{
free(g_pTail);
g_pTail = pTemp2;
g_pTail->pNext = NULL;
pTemp1 = pTemp2;
}
else if (g_pTail == pTemp1)
{
StuNode* pTemp = g_pHead;
while (pTemp->pNext != g_pTail)
{
pTemp = pTemp->pNext;
}
free(g_pTail);
g_pTail = pTemp;
g_pTail->pNext = NULL;
pTemp1 = g_pTail->pNext;
}
else
{
StuNode* pT = SelectNode(pTemp1);
pT = pT->pNext;
pTemp2->pNext = pT->pNext;
pTemp1 = pT->pNext;
free(pT);
//ShowStuDate();
}
}
}
if (pTemp2->pNext == g_pTail)
{
if (0 == strcmp(pTemp2->arrStuID , g_pTail->arrStuID))
{
free(g_pTail);
g_pTail = pTemp2;
g_pTail->pNext = NULL;
pTemp1 = pTemp2;
}
}
pTemp1 = pTemp2->pNext;
pTemp2 = pTemp1;
//StuID = pTemp2->arrStuID;
if (NULL == pTemp1 || NULL == pTemp2)
{
break;
}
}
//free(p);
//没找到
return 0;
}
//根据成绩排序 从大到小
void PaiXvStuScore()
{
}
//根据学号进行排序 从小到大
void PaiXvStuID()
{
}
void gameorder()
{
printf(" 1、 贪食蛇游戏 \n");
printf(" 2、 俄罗斯方块 \n");
printf(" 3、 飞机大战 \n");
printf(" 4、 扫雷 \n");
}
//游戏模式
void ModeGame()
{
int ipat = 0;
scanf("%d", &ipat);
gameorder();
switch (ipat)
{
case 1:
//TODO 贪食蛇游戏
break;
case 2:
//TODO 俄罗斯方块
break;
case 3:
//TODO 飞机大战
break;
case 4:
//TODO 扫雷
break;
default:
printf("退出游戏模式\n");
break;
}
}
// Read the owner information in the file
void ReadStuForFile()
{
FILE* pFile = fopen("E:\\编程\\Student_Management_System\\StudentManagementSystem\\111.txt", "rb");
if (NULL == pFile)
{
printf("文件打开失败\n");
return;
}
//操作文件 读取函数
char strBuf[30] = {
0 };
char strStuID[15] = {
0 };
char strStuName[10] = {
0 };
char strStuScore[5] = {
0 };
while (NULL != fgets(strBuf, 30, pFile))
{
int i = 0;
int iCount = 0;
int j = 0;
for (i = 0; strBuf[i] != '\n'; i++)
{
if (0 == iCount)// 没到空格 学号
{
strStuID[i] = strBuf[i];
if (' ' == strBuf[i])
{
strStuID[i] = '\0';
iCount++;
}
}
else if (1 == iCount) //第一个空格 名字
{
strStuName[j] = strBuf[i];
j++;
if (' ' == strBuf[i])
{
strStuName[j] = '\0';
iCount++;
j = 0;
//continue;
}
}
else //第二个空格 成绩
{
strStuScore[j] = strBuf[i];
j++;
}
}
//将数据写入链表
AddStuMsgTail(strStuID, strStuName, atoi(strStuScore));
}
//关闭文件
fclose(pFile);
}
//Save the owner’s information in a file
void SaveStuToFile()
{
//检测链表是否为空
if (NULL == g_pHead)
{
printf("链表为空\n");
return;
}
//打开一个文件
FILE* pFile = NULL;
StuNode* pTemp = g_pHead;
char strBuf[30] = {
'\0' };
char strScore[10] = {
'\0' };
pFile = fopen("E:\\编程\\Student_Management_System\\StudentManagementSystem\\111.txt", "wb");
if (NULL == pFile)
{
printf("文件打开失败\n");
return;
}
//操作文件指针
while (pTemp)
{
//stuent ID
strcpy(strBuf, pTemp->arrStuID);
strcat(strBuf, " ");
//name
strcat(strBuf, pTemp->arrName);
strcat(strBuf, " ");
//score
//转换为字符
_itoa(pTemp->iStuScore, strScore, 10);
//接在字符数组中
strcat(strBuf, strScore);
strcat(strBuf, "\n");
fwrite(strBuf, 1, strlen(strBuf), pFile);
pTemp = pTemp->pNext;
}
//关闭文件
fclose(pFile);
}
//删除指定节点
void DeleteRandNode(char* arrIndex)
{
//链表检测
if (NULL == g_pHead)
{
printf("链表为空\n");
return;
}
//逻辑
//判断
StuNode* pT = SelectNode(arrIndex);
if (g_pHead == pT)
{
HeadDeleteNode();
}
else if (g_pTail == pT)
{
TailDeleteNode();
}
else
{
StuNode* pTemp = g_pHead;
while (pTemp->pNext != pT)
{
pTemp = pTemp->pNext;
}
pTemp->pNext = pT->pNext;
free(pT);
}
}
//头删除
void HeadDeleteNode()
{
//链表检测
if (NULL == g_pHead)
{
printf("链表为空\n");
return;
}
//判断节点
if (g_pHead == g_pTail)
{
free(g_pHead);
g_pHead = NULL;
g_pTail = NULL;
}
else
{
StuNode* pTemp = g_pHead;
g_pHead = pTemp->pNext;
free(pTemp);
}
}
//尾删除
void TailDeleteNode()
{
//链表检测
if (NULL == g_pHead)
{
printf("链表为空\n");
return;
}
//判断节点
if (g_pHead == g_pTail)
{
free(g_pHead);
g_pHead = NULL;
g_pTail = NULL;
}
else
{
StuNode* pTemp = g_pHead;
while (pTemp->pNext != g_pTail)
{
pTemp = pTemp->pNext;
}
free(g_pTail);
g_pTail = pTemp;
g_pTail->pNext = NULL;
}
}
//Add a student information_rand
void AddStuMsgRand(char* arrStuID, char* arrName, int iStuScore, char* arrIndex)
{
//检测合法性
if (NULL == arrStuID || NULL == arrName || NULL == arrIndex || iStuScore < 0)
{
printf("输入信息有误\n");
return;
}
//逻辑
//找位置
StuNode* pt = SelectNode(arrIndex);
if (NULL == pt)
{
printf("no have Node\n");
return;
}
//创建节点
StuNode* pTemp = (StuNode*)malloc(sizeof(StuNode));
//节点赋值
strcpy(pTemp->arrStuID, arrStuID);
strcpy(pTemp->arrName, arrName);
pTemp->iStuScore = iStuScore;
pTemp->pNext = NULL;
//链接
if (NULL == g_pHead)
{
g_pHead = pTemp;
g_pTail = pTemp;
}
else if (g_pHead == pTemp)
{
AddStuMsgHead(arrStuID, arrName, iStuScore);
}
else if (g_pTail == pTemp)
{
AddStuMsgTail(arrStuID, arrName, iStuScore);
}
else
{
pTemp->pNext = pt->pNext;
pt->pNext = pTemp;
}
}
//查找指定学生
StuNode* SelectNode(char* arrIndex)
{
if (NULL == arrIndex)
{
printf("学号输入错误\n");
return NULL;
}
if (NULL == g_pHead)
{
printf("信息为空\n");
return NULL;
}
StuNode* pTemp = g_pHead;
while (pTemp != NULL)
{
//字符串比较
if (0 == strcmp(arrIndex,pTemp->arrStuID)) //相等返回 0
{
return pTemp;
}
pTemp = pTemp->pNext;
}
//没找到
printf("查无此节点\n");
return NULL;
}
//Add a student information_head
void AddStuMsgHead(char* arrStuID, char* arrName, int iStuScore)
{
//检验的参数的合法性
if (NULL == arrStuID || NULL == arrName || iStuScore < 0)
{
printf("输入的信息有误\n");
return;
}
//逻辑
//创建一个节点
StuNode* pTemp = (StuNode*)malloc(sizeof(StuNode));
//节点成员赋值
strcpy(pTemp->arrStuID, arrStuID);
strcpy(pTemp->arrName, arrName);
pTemp->iStuScore = iStuScore;
pTemp->pNext = NULL;
//接在链表上
if (NULL == g_pHead)
{
//g_pHead = pTemp;
g_pTail = pTemp;
}
else
{
pTemp->pNext = g_pHead;
//g_pHead = pTemp;
}
g_pHead = pTemp;
}
//打印数据
void ShowStuDate()
{
if (g_pHead == NULL)
{
printf("无学生信息\n");
return;
}
else
{
StuNode* pTemp = g_pHead;
while (pTemp != NULL)
{
printf("学号:%s \t 姓名:%s \t 成绩:%d\n", pTemp->arrStuID, pTemp->arrName, pTemp->iStuScore);
pTemp = pTemp->pNext;
}
}
}
//clear list
void FreeList()
{
//创建一个指针指向头,防止头指针改变
StuNode* pTemp = g_pHead;
while (pTemp != NULL)
{
//引入中间节点,依次释放节点
StuNode* pt = pTemp;
pTemp = pTemp->pNext;
free(pt);
}
g_pHead = NULL;
g_pTail = NULL;
}
//Add a student information_tail
void AddStuMsgTail(char* arrStuID,char* arrName,int iStuScore)
{
//检验的参数的合法性
if (NULL == arrStuID || NULL == arrName || iStuScore < 0)
{
printf("学生消息输入错误\n");
return;
}
//逻辑
//创建一个节点
StuNode* pTemp = (StuNode*)malloc(sizeof(StuNode));
//节点成员赋值
strcpy(pTemp->arrStuID, arrStuID);
strcpy(pTemp->arrName, arrName);
pTemp->iStuScore = iStuScore;
pTemp->pNext = NULL;
//接在链表上
if (NULL == g_pHead)
{
g_pHead = pTemp;
}
else
{
g_pTail->pNext = pTemp;
//g_pTail->pNext = NULL;
}
g_pTail = pTemp;
}
void ShowOrder()
{
printf(" 欢迎进入学生管理系统 \n");
printf(" 系统指令如下 \n");
printf(" 1、 添加学生信息 尾添加 \n");
printf(" 2、 添加学生信息 头添加 \n");
printf(" 3、 添加学生信息 任意位置添加 \n");
printf(" 4、 查找指定的学生信息 \n");
printf(" 5、 修改指定的学生信息 \n");
printf(" 6、 保存学生信息到文件中 \n");
printf(" 7、 读取文件中的学生信息 \n");
printf(" 8、 删除指定的学生信息 \n");
printf(" 9、 展示所有学生信息 \n");
printf(" 10、 恢复删除的学生信息 \n");
printf(" 11、 查看指令 \n");
printf(" 13、 清空控制台 \n");
printf(" 14、 成绩排序(从大到小) \n");
printf(" 17、 删除重复学生信息 \n");
printf(" 0、 退出系统 \n");
printf(" \n");
}