单链表:学生成绩管理系统
student.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct STUDENT
{
char ID[8];
char name[20];
char sex[4];
int chinese;
int math;
int english;
int zh;
int total;
};
struct STUD
{
struct STUDENT data;
struct STUD *next;
};
//菜单选项
void menu();
//创建链表
struct STUD *CreateLink();
//输出链表
void OutputLink(struct STUD *);
//录入学生成绩信息
struct STUD *InputStud(int);
//通过学号查询学生信息
void QueryLink(struct STUD *);
//添加学生成绩信息
void AddNode(struct STUD *);
//存储学生成绩信息
void SaveLink(struct STUD *);
//读取学生成绩信息
struct STUD *ReadLink();
//浏览学生成绩信息
void ShowLink(struct STUD *phead);
//删除学生成绩信息
void DelNode(struct STUD *phead);
//查看学生成绩统计信息
void DisplayLink();
//子菜单1
int menu1();
//按总分排序
void SortTotal(struct STUD *phead);
//按语文成绩排序
void SortChinese(struct STUD *phead);
//按数学成绩排序
void SortMath(struct STUD *phead);
//按英语成绩排序
void SortEnglish(struct STUD *phead);
//按综合成绩排序
void SortZh(struct STUD *phead);
//排序
void SortNode(struct STUD *phead, char *s);
student.c
#include "student.h"
extern struct STUD *head; //extern 使得可以在这儿使用HeadNode.c中定义的全局变量 head头指针
//创建链表
struct STUD *CreateLink()
{
int count = 0, i;
struct STUD *phead, *pend, *pnew;
phead = ( struct STUD * )malloc(sizeof(struct STUD));
if (NULL == phead)
{
printf("\t\t\t内存分配失败,程序终止!\n");
exit(-1);
}
head = phead;
pend = phead;
printf("\n\t\t\t 请输入学生人数: ");
rewind(stdin);
scanf("%d", &count);
for (i=0; i<count; i++)
{
pnew = InputStud(i + 1);
pend->next = pnew;
pnew->next = NULL;
pend = pnew;
}
printf("\n\t\t\t学生成绩信息录入完成!\n\n");
printf("\n\t\t\t正在保存。。。");
rewind(stdin);
getchar();
SaveLink(phead);
return phead;
}
//录入学生成绩信息
struct STUD *InputStud(int n)
{
struct STUD *p = ( struct STUD * )malloc(sizeof(struct STUD));
if ( NULL == p )
{
printf("\t\t\t内存分配失败,程序终止!\n");
exit(-1);
};
int count = 0;
printf("\n\t\t\t请输入第%d个学生的学号:", n);
rewind(stdin);
scanf("%s", p->data.ID);
printf("\t\t\t\n");
while ( strlen(p->data.ID) < 0 || strlen(p->data.ID) > 8 )
{
count++;
printf("\t\t\t你输入的学号错误,请重新输入8位以内学号!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%s", p->data.ID);
if ( 3 == count )
{
printf("\t\t\t你输入的学号错误次数过多,退出系统!");
exit(0);
}
}
printf("\t\t\t请输入第%d个学生的姓名:", n);
rewind(stdin);
scanf("%s", p->data.name);
printf("\t\t\t\n");
printf("\t\t\t请输入第%d个学生的性别:", n);
rewind(stdin);
scanf("%s", p->data.sex);
printf("\t\t\t\n");
count = 0;
while ( strcmp(p->data.sex, "男") != 0 && strcmp(p->data.sex, "女") != 0 )
{
count++;
printf("\t\t\t你输入的性别错误,请重新输入!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%s", p->data.sex);
if ( 3 == count )
{
printf("\t\t\t你输入的性别错误次数过多,退出系统!");
exit(0);
}
}
printf("\t\t\t请输入第%d个学生的语文成绩:", n);
rewind(stdin);
scanf("%d", &p->data.chinese);
printf("\t\t\t\n");
count = 0;
while ( p->data.chinese < 0 || p->data.chinese >150 )
{
count++;
printf("\t\t\t你输入的语文成绩错误,请重新输入!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%d", &p->data.chinese);
if ( 3 == count )
{
printf("\t\t\t你输入的语文成绩错误次数过多,退出系统!");
exit(0);
}
}
printf("\t\t\t请输入第%d个学生的数学成绩:", n);
rewind(stdin);
scanf("%d", &p->data.math);
printf("\t\t\t\n");
count = 0;
while ( p->data.math < 0 || p->data.math >150 )
{
count++;
printf("\t\t\t你输入的数学成绩错误,请重新输入!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%d", &p->data.math);
if ( 3 == count )
{
printf("\t\t\t你输入的数学成绩错误次数过多,退出系统!");
exit(0);
}
}
printf("\t\t\t请输入第%d个学生的英语成绩:", n);
rewind(stdin);
scanf("%d", &p->data.english);
printf("\t\t\t\n");
count = 0;
while ( p->data.english < 0 || p->data.english >150 )
{
count++;
printf("\t\t\t你输入的英语成绩错误,请重新输入!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%d", &p->data.english);
if ( 3 == count )
{
printf("\t\t\t你输入的英语成绩错误次数过多,退出系统!");
exit(0);
}
}
printf("\t\t\t请输入第%d个学生的理(文)综成绩:", n);
rewind(stdin);
scanf("%d", &p->data.zh);
printf("\t\t\t\n");
count = 0;
while ( p->data.zh < 0 || p->data.zh >300 )
{
count++;
printf("\t\t\t你输入的理(文)综成绩错误,请重新输入!\n");
printf("\t\t\t");
rewind(stdin);
scanf("%d", &p->data.zh);
if ( 3 == count )
{
printf("\t\t\t你输入的理(文)综成绩错误次数过多,退出系统!");
exit(0);
}
}
p->data.total = p->data.chinese + p->data.math + p->data.english + p->data.zh;
return p;
}
//输出链表
void OutputLink(struct STUD *phead)
{
if (NULL == phead)
{
printf("\n\t\t\t 读取学生成绩信息失败,程序终止!\n");
exit(-1);
}
struct STUD *p = phead->next;
printf("\n\t\t\t学号\t姓名\t性别\t语文\t数学\t英语\t综合\t总分\n");
printf("\t\t======================================================================\n");
while ( NULL != p )
{
printf("\t\t\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p->data.ID, p->data.name, p->data.sex,
p->data.chinese, p->data.math, p->data.english, p->data.zh, p->data.total);
p = p->next;
}
}
//通过学号查询学生信息
void QueryLink(struct STUD *phead)
{
char sID[8] = { '\0' };
if (NULL == phead)
{
phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
phead = ReadLink();
struct STUD *pnow = phead;
}
struct STUD *p = phead->next;
printf("\t\t\t请输入要查询的学号: ");
scanf("%s", sID);
printf("\t\t\t\n");
while ( NULL != p)
{
if ( strcmp(sID, p->data.ID) == 0 )
{
printf("\n\t\t\t学号\t姓名\t性别\t语文\t数学\t英语\t综合\t总分\n");
printf("\t\t======================================================================\n");
printf("\t\t\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n", p->data.ID, p->data.name, p->data.sex,
p->data.chinese, p->data.math, p->data.english, p->data.zh, p->data.total);
printf("\n\n\t\t\t按任意键继续....... ");
rewind(stdin);
getchar();
return;
}
p = p->next;
}
rewind(stdin);
printf("\n\t\t\t学号 %s 未找到,按任意键继续....... ", sID);
getchar();
}
//添加学生成绩信息
void AddNode(struct STUD *phead)
{
int num = 0, i = 0;
char sID[8] = { '\0' };
if ( NULL == phead )
{
phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
phead = ReadLink();
}
struct STUD *pnew = phead, *pmove = NULL;
printf("\t\t\t查询要添加的学号是否存在:\n\n");
QueryLink(phead);
printf("\t\t\t请输入要添加学生的人数: ");
scanf("%d", &num);
printf("\t\t\t\n\n");
while ( NULL != pnew->next )
{
pnew = pnew->next;
}
for ( i = 0; i < num; i++)
{
pmove = InputStud(i + 1);
pnew->next = pmove;
pmove->next = NULL;
pnew = pmove;
}
SaveLink(phead);
rewind(stdin);
printf("\n\n\t\t\t添加学生信息完成,按任意键继续....... ");
getchar();
}
//保存学生信息
void SaveLink(struct STUD *phead)
{
if ( NULL == phead )
{
printf("\t\t\t学生成绩信息为空,程序终止!\n");
exit(-1);
}
struct STUD *p = phead->next;
FILE *fp;
if (( fp = fopen("student.bin", "wb")) == NULL)
{
printf("\t\t\t不能存储文件,程序终止!\n");
exit(-1);
}
while ( NULL != p )
{
fwrite(p, sizeof(struct STUD), 1, fp);
p = p->next;
}
printf("\t\t\t存储文件完成!\n");
fclose(fp);
}
//读取学生成绩信息
struct STUD *ReadLink()
{
int i = 1;
system("cls");
printf("\n\t\t\t 正在从文件中读取 \n\n");
printf("\n\t\t ");
while ( i < 30 )
{
printf("#");
i++;
Sleep(5);
}
//Sleep(5000);
struct STUD *phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
struct STUD *pnow = phead;
FILE *fp;
if ( ( fp = fopen("student.bin", "rb") ) == NULL )
{
printf("\t\t\t读取文件失败,程序终止!\n");
exit(-1);
}
fseek(fp, 0, SEEK_SET);
struct STUD *q = NULL;
while ( 1 )
{
q = ( struct STUD * )malloc(sizeof(struct STUD));
if ( fread(q, sizeof(struct STUD), 1, fp) == 1 )
{
pnow->next = q;
q->next = NULL;
pnow = q;
}
else
break;
}
fclose(fp);
printf("\n\n\t\t\t 从文件中读取成功! \n\n");
return phead;
}
//浏览学生成绩信息
void ShowLink(struct STUD *phead)
{
if (NULL == phead)
{
phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
phead = ReadLink();
}
printf("\n\t\t\t\n");
OutputLink(phead);
rewind(stdin);
printf("\n\t\t\t按任意键继续....... ");
getchar();
}
//删除学生成绩信息
void DelNode(struct STUD *phead)
{
char sID[8] = { '\0' };
if ( NULL == phead )
{
phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
phead = ReadLink();
}
struct STUD *pnow = phead, *psave;
printf("\t\t\t请输入要删除的学生学号:\n");
printf("\t\t\t");
scanf("%s", sID);
rewind(stdin);
while ( pnow->next != NULL )
{
if (strcmp(pnow->next->data.ID, sID) == 0)
{
if (pnow->next->next == NULL) //如果指向最后一个结点为空
{
psave = pnow->next;
pnow->next = NULL;
free(psave);
psave = NULL;
SaveLink(phead);
return;
}
else
{
psave = pnow->next;
pnow->next = psave->next;
free(psave);
psave = NULL;
SaveLink(phead);
printf("\n\t\t\t删除学号为%s的学生完成!\n\n", sID);
printf("\n\t\t\t按任意键继续...\n\n");
printf("\n\t\t\t");
rewind(stdin);
getchar();
return;
}
}
pnow = pnow->next;
}
printf("\n\t\t\t学号为%s的学生不存在!\n\n", sID);
printf("\n\t\t\t按任意键继续...\n\n");
printf("\n\t\t\t");
rewind(stdin);
getchar();
}
//查看学生成绩统计信息
void DisplayLink()
{
int num = -1;
while ( 1 )
{
system("cls");
num = menu1();
if (num < 0 || num > 5)
{
printf("\n\n\t\t\t 选择错误,请重新选择!\n");
printf("\n\n\t\t\t");
rewind(stdin);
num = menu1();
}
else
{
switch ( num )
{
case 1:
SortTotal(head);
break;
case 2:
SortChinese(head);
break;
case 3:
SortMath(head);
break;
case 4:
SortEnglish(head);
break;
case 5:
SortZh(head);
break;
case 0:
return;
break;
}
}
}
}
//子菜单1
int menu1()
{
int choice = -1;
system("Color f0");
printf("\n\t\t\t========================================\n");
printf("\t\t\t= 学生成绩统计信息 =\n");
printf("\t\t\t========================================\n");
printf("\t\t\t= 1. 按总分浏览 =\n");
printf("\t\t\t= 2. 按语文成绩 =\n");
printf("\t\t\t= 3. 按数学成绩 =\n");
printf("\t\t\t= 4. 按英语成绩 =\n");
printf("\t\t\t= 5. 按综合成绩 =\n");
printf("\t\t\t= 0. 返回主菜单 =\n");
printf("\t\t\t========================================\n");
printf("\n\n\t\t\t 请选择菜单选项: ");
rewind(stdin);
scanf("%d", &choice);
return choice;
}
//按总分排序
void SortTotal(struct STUD *phead)
{
char s[20] = "总分";
SortNode(phead, s);
return;
}
//按语文成绩排序
void SortChinese(struct STUD *phead)
{
char s[20] = "语文";
SortNode(phead, s);
return;
}
//按数学成绩排序
void SortMath(struct STUD *phead)
{
char s[20] = "数学";
SortNode(phead, s);
return;
}
//按英语成绩排序
void SortEnglish(struct STUD *phead)
{
char s[20] = "英语";
SortNode(phead, s);
return;
}
//按综合成绩排序
void SortZh(struct STUD *phead)
{
char s[20] = "综合";
SortNode(phead, s);
return;
}
//排序
void SortNode(struct STUD *phead, char *s)
{
if ( NULL == phead )
{
phead = ( struct STUD * )malloc(sizeof(struct STUD));
phead->next = NULL;
phead = ReadLink();
}
struct STUD *move = phead->next;
struct STUD *turn = phead->next;
struct STUDENT buf;
if ( NULL == move ) //说明输入的学生个数是0
{
printf("\t\t\t学生成绩信息为空,程序终止!\n");
exit(-1);
}
while ( NULL != turn->next )
{
while ( NULL != move->next )
{
if ( strcmp(s, "总分") == 0 )
{
if ( move->data.total < move->next->data.total )
{
buf = move->data;
move->data = move->next->data;
move->next->data = buf;
}
}
else if ( strcmp(s, "语文") == 0 )
{
if ( move->data.chinese < move->next->data.chinese )
{
buf = move->data;
move->data = move->next->data;
move->next->data = buf;
}
}
else if ( strcmp(s, "数学") == 0 )
{
if ( move->data.math < move->next->data.math )
{
buf = move->data;
move->data = move->next->data;
move->next->data = buf;
}
}
else if ( strcmp(s, "英语") == 0 )
{
if ( move->data.english < move->next->data.english )
{
buf = move->data;
move->data = move->next->data;
move->next->data = buf;
}
}
else if ( strcmp(s, "综合") == 0 )
{
if ( move->data.zh < move->next->data.zh )
{
buf = move->data;
move->data = move->next->data;
move->next->data = buf;
}
}
move = move->next;
}
move = phead->next;
turn = turn->next;
}
OutputLink(phead);
printf("\n\t\t\t按任意键继续...\n\n");
printf("\n\t\t\t");
rewind(stdin);
getchar();
}
menu.c
#include "student.h"
//菜单选项
void menu()
{
system("Color f0");
printf("\n\t\t\t========================================\n");
printf("\t\t\t= 学生成绩管理系统 =\n");
printf("\t\t\t========================================\n");
printf("\t\t\t= 1. 录入学生信息 =\n");
printf("\t\t\t= 2. 浏览学生信息 =\n");
printf("\t\t\t= 3. 查询学生信息 =\n");
printf("\t\t\t= 4. 添加学生信息 =\n");
printf("\t\t\t= 5. 删除学生信息 =\n");
printf("\t\t\t= 6. 查看统计信息 =\n");
printf("\t\t\t= 0. 退出系统 =\n");
printf("\t\t\t========================================\n");
printf("\n\n\t\t\t 请选择菜单选项: ");
}
HeadNode.c
#include "student.h"
struct STUD *head = NULL;
int main(void)
{
struct STUD *phead = NULL;
phead = head;
int choice = -1;
while ( 1 )
{
system("cls");
menu();
scanf("%d", &choice);
switch ( choice )
{
case 1:
phead = CreateLink();
break;
case 2:
ShowLink(phead);
break;
case 3:
QueryLink(phead);
break;
case 4:
AddNode(phead);
break;
case 5:
DelNode(phead);
break;
case 6:
DisplayLink(phead);
break;
case 0:
exit(0);
default:
break;
}
}
return 0;
}