软件:Visual Studio 2019
语言:C/C++
#define _CRT_SECURE_NO_WARNINGS //非VS环境下可注释此行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
int shoudsave = 0;
char path[30] = "E:\\Student\\stu.txt";//此处修改文件存放的位置
struct student
{
char num[20]; //学号
char name[10]; //姓名
char sex[4]; //性别
int cgrade;
int mgrade;
int egrade;
int totle;
int ave;
char neartime[10];//最近更新时间
};
typedef struct datain
{
char num[20]; //学号
char name[10]; //姓名
char sex[4]; //性别
char cgrade[10];
char mgrade[10];
char egrade[10];
} dataIn;
typedef struct node
{
struct student data;
struct node* next;
}Node, * Link;
//界面函数
void menu()
{
printf("*************************************************************\n");
printf("\t1.登记学生资料\t\t\t2.删除学生资料\n");
printf("\t3.查询学生资料\t\t\t4.修改学生资料\n");
printf("\t5.显示学生资料\t\t\t6.统计学生资料\n");
printf("\t7.排序语文成绩\t\t\t8.排序数学成绩\n");
printf("\t9.排序英语资料\t\t\t10.选出分段分数\n");
printf("\t11.保存学生资料\t\t\t12.帮助信息\n");
printf("\t13.读入信息\n");
printf("*************************************************************\n");
}
//打印开始
void printstart()
{
printf("-------------------------------------------------------------\n");
}
//打印错误信息
void printwrong()
{
printf("\n======>提示:输入错误!\n");
}
//打印未找到信息
void Nofind()
{
printf("\n======>提示:没有找到此学生。");
}
//输出中文
void printc()
{
printf("显示顺序为:学号 姓名 性别 语文成绩\n");
printf(" 数学成绩 英语成绩 总分 平均分\n");
}
//输出英文
void printe(Node *p)
{
printf("Stunum:%s\t", p->data.num);
printf("Name:%s\t", p->data.name);
printf("Gender:%s\t",p->data.sex);
printf("Chinese:%d\n", p->data.cgrade);
printf("Math:%d \t", p->data.mgrade);
printf("English:%d\t", p->data.egrade);
printf("Score:%d\tAverage:%d\n",p->data.totle,p->data.ave);
}
//定位链表中符合要求的结点,返回该指针
Node* Locate(Link l, char findmess[], char nameornum[])
{
Node* r;
if (strcmp(nameornum, "num") == 0)//按学号查询
{
r = l->next;
while (r != NULL)
{
if (strcmp(r->data.num, findmess) == 0)
return r;
r = r->next;
}
}
else if(strcmp(nameornum,"name") == 0)
{
r = l->next;
while (r != NULL)
{
if (strcmp(r->data.name, findmess) == 0)
{
return r;
}
r = r->next;
}
}
return NULL;
}
//增加学生
void Add(Link l)
{
Node* p, * r, * s;
char num[10];
r = l;
s = l->next;
//指针定位末尾
while (r->next != NULL)
{
r->next;
}
while (1)
{
printf("请你输入学号(输入'0'返回上一级菜单:)");
scanf("%s", num);
if (strcmp(num, "0") == 0)
{
break;
}
//遍历查找是否有重复
while (s)
{
if (strcmp(s->data.num, num) == 0)
{
printf("======>提示:学号为%s的学生已存在,若要修改请选择 4.修改\n", num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
}
s = s->next;
}
p = (Node*)malloc(sizeof(Node));
strcpy(p->data.num, num);
printf("请你输入姓名:");
scanf("%s", p->data.name);
getchar();
printf("请输入性别:");
scanf("%s", p->data.sex);
getchar();
printf("请你输入语文成绩:");
scanf("%d", &p->data.cgrade);
getchar();
printf("请你输入数学成绩:");
scanf("%d", &p->data.mgrade);
getchar();
printf("请你输入英语成绩:");
scanf("%d", &p->data.egrade);
getchar();
p->data.totle = p->data.cgrade + p->data.mgrade + p->data.egrade;
p->data.ave = p->data.totle / 3;
/* 信息输入完成 */
p->next = r->next;
r->next = p;
shoudsave = 1;
}
}
//查询学生
void Qur(Link l)
{
char NUM[5] = "num";
char NAME[5] = "name";
int sel;
char findmess[20];
Node* p;
//空链表
if (!l->next)
{
printf("\n======>提示:没有资料可以查询!\n");
return ;
}
printf("\n======>1.按学号查找\n====>2.按姓名查找\n");
scanf("%d", &sel);
if (sel == 1)
{
printf("请输入你要查找的学号:");
scanf("%s", findmess);
p = Locate(l, findmess, NUM);
if (p)
{
printf("*****查找结果*****\n");
printstart();
printc();
printe(p);
printstart();
}
else
{
Nofind();
}
}
else if (sel == 2)
{
printf("请你输入要查找的姓名:");
scanf("%s", findmess);
p = Locate(l, findmess, NAME);
if (p)
{
printf("*****查找结果*****\n");
printstart();
printc();
printe(p);
printstart();
}
else
{
Nofind();
}
}
else
{
printwrong();
}
}
//删除信息
void Del(Link l)
{
char NUM[5] = "num";
char NAME[5] = "name";
int sel;
Node* p, * r;
char findmess[20];
if (!l->next)
{
printf("\n======>提示:没有资料可以删除!\n");
return;
}
printf("\n======>1.按学号删除\n=======2.按姓名删除\n");
scanf("%d", &sel);
if (sel == 1)
{
printf("请你输入要删除的学号:");
scanf("%s", findmess);
p = Locate(l, findmess, NUM);
if (p)
{
r = l;
while (r->next != p)
{
r = r->next;
}
r->next = p->next;
free(p);
printf("\n=======>提示:该学生已经成功删除");
shoudsave = 1;
}
else
{
Nofind();
}
}
else if(sel == 2)
{
printf("请你输入要删除的姓名:");
scanf("%s", findmess);
p = Locate(l, findmess, NAME);
if (p)
{
r = l;
while (r->next != p)
{
r = r->next;
}
r->next = p->next;
free(p);
printf("\n=====>提示:该学生已经成功删除\n");
shoudsave = 1;
}
else
{
Nofind();
}
}
else
{
printwrong();
}
}
//修改
void Modify(Link l)
{
char NUM[5] = "num";
char NAME[5] = "name";
Node* p;
char findmess[20];
if (l->next == NULL)
{
printf("\n=====>提示:没有资料可以修改!\n");
return;
}
printf("请输入你要修改的学生学号:");
scanf("%s", findmess);
p = Locate(l, findmess, NUM);
if (p)
{
printf("请你输入新的学号(原来是:%s):", p->data.num);
scanf("%s", p->data.num);
getchar();
printf("请你输入新的姓名:(原来是:%s):", p->data.name);
scanf("%s", p->data.name);
getchar();
printf("请你输入新的性别(原来是:%s):", p->data.sex);
scanf("%s", p->data.sex);
getchar();
printf("请你输入新的语文成绩(原来是:%d):", p->data.cgrade);
scanf("%d", &p->data.cgrade);
getchar();
printf("请你输入新的数学成绩(原来是:%d):", p->data.mgrade);
scanf("%d", &p->data.mgrade);
getchar();
printf("请你输入新的英语成绩(原来是:%d):", p->data.egrade);
scanf("%d", &p->data.egrade);
getchar();
p->data.totle = p->data.cgrade + p->data.mgrade + p->data.egrade;
p->data.ave = p->data.totle / 3;
printf("\n提示========>资料修改成功!\n");
shoudsave = 1;
}
else
{
Nofind();
}
}
//显示函数
void Disp(Link l)
{
Node* p=l->next;
if (l == NULL)
{
printf("\n=========>提示:没有资料可以显示!\n");
return;
}
printf("\t\t\t显示结果\n");
printstart();
printc();
printf("\n");
while (p)
{
printe(p);
p = p->next;
}
printstart();
printf("\n");
}
void Tongji(Link l)
{
Node *pm,*pe,*pc,*pt,*pa;
Node* r = l->next;
if (!r)
{
printf("\n=====>提示:没有资料可以统计\n");
return;
}
pm = pe = pc = pt = pa = r;
while (r != NULL)
{
if (r->data.cgrade >= pc->data.cgrade)
pc = r;
if (r->data.mgrade >= pm->data.mgrade)
pm = r;
if (r->data.egrade >= pe->data.egrade)
pe = r;
if (r->data.totle >= pt->data.totle)
pt = r;
if (r->data.ave >= pa->data.ave)
pa = r;
r = r->next;
}
printf("----------------------统计结果-------------------\n");
printf("总分最高者:\t%s %d分\n", pt->data.name,pt->data.totle);
printf("平均分最高者:\t%s %d分\n", pa->data.name, pa->data.ave);
printf("英语最高者:\t%s %d分\n", pe->data.name, pe->data.egrade);
printf("数学最高者:\t%s %d分\n", pm->data.name, pm->data.mgrade);
printf("语文最高者:\t%s %d分\n", pc->data.name, pc->data.cgrade);
printstart();
}
void Chinese(Link l)
{
Link ll;
Node* p, * rr, * s;
ll = (Link)malloc(sizeof(Node));
ll->next = NULL;
if (l->next == NULL)
{
printf("\n======>提示:没有资料可以排序!\n");
return;
}
p = l->next;
while (p)
{
s = (Node*)malloc(sizeof(Node));
s->data = p->data;
s->next = NULL;
rr = ll;
while (rr->next != NULL && (rr->next->data.cgrade >= p->data.cgrade))
{
rr = rr->next;
}
if (rr->next == NULL)
{
rr->next = s;
}
else
{
s->next = rr->next;
rr->next = s;
}
p = p->next;
}
//free(l);
l->next = ll->next;
printf("\n==========>提示:排序已经完成!\n");
}
//排序数学
void Maths(Link l)
{
Link ll;
Node* p, * rr, * s;
ll = (Link)malloc(sizeof(Node));
ll->next = NULL;
if (l->next = NULL)
{
printf("\n=====>提示:没有资料可以排序!\n");
return;
}
p = l->next;
while (p)
{
s = (Node*)malloc(sizeof(Node));
s->data = p->data;
s->next = NULL;
rr = ll;
while (rr->next != NULL && (rr->next->data.mgrade >= p->data.mgrade))
rr = rr->next;
if (rr->next == NULL)
rr->next = s;
else
{
s->next = rr->next;
rr->next = s;
}
p = p->next;
}
//free(l);
l->next = ll->next;
printf("\n=======>提示:排序已经完成!\n");
}
void English(Link l)
{
Link ll;
Node* p, * rr, * s;
ll = (Link)malloc(sizeof(Node));
ll->next = NULL;
if (l->next == NULL)
{
printf("\n======>提示:没有资料可以排序!\n");
return;
}
p = l->next;
while (p)
{
s = (Node*)malloc(sizeof(Node));
s->data = p->data;
s->next = NULL;
rr = ll;
while (rr->next != NULL && (rr->next->data.egrade >= p->data.egrade))
{
rr = rr->next;
}
if (rr->next == NULL)
{
rr->next = s;
}
else
{
s->next = rr->next;
rr->next = s;
}
p = p->next;
}
//free(l);
l->next = ll->next;
printf("\n==========>提示:排序已经完成!\n");
}
//选择
void Select(Link l)
{
int i, j, k;
Node* pc = l;
Node* r = l->next;
if (!r)
{
printf("\n========>提示:没有资料可以筛选!\n");
return;
}
printf("输入课程名 1.语文 2.数学 3.英语\n");
printf("你选择:");
scanf("%d", &i);
if (i == 1)
{
printf("请输入筛选分数的上限:");
scanf("%d", &j);
printf("请输入筛选分数的下限:");
scanf("%d", &k);
printf("-------------------------------筛选结果----------------------\n");
printf("学号\t 姓名\t 性别 语文成绩 数学成绩 英语成绩 总分 平均分\n");
printstart();
while (r != NULL)
{
if ((k <= (r->data.cgrade)) && ((r->data.cgrade) < j))
printe(r);
r = r->next;
}
}
else if (i == 2)
{
printf("请输入筛选分数的上限:");
scanf("%d", &j);
printf("请输入筛选分数的下限:");
scanf("%d", &k);
printf("-------------------------------筛选结果----------------------");
printf("学号\t 姓名\t 性别 语文成绩 数学成绩 英语成绩 总分 平均分\n");
printstart();
while (r != NULL)
{
if (k <= (r->data.mgrade) && (r->data.mgrade) < j)
printe(r);
r = r->next;
}
}
else if (i == 3)
{
printf("请输入筛选分数的上限:");
scanf("%d", &j);
printf("请输入筛选分数的下限:");
scanf("%d", &k);
printf("-------------------------------筛选结果----------------------");
printf("学号\t 姓名\t 性别 语文成绩 数学成绩 英语成绩 总分 平均分\n");
printstart();
while (r != NULL)
{
if (k <= (r->data.egrade) && (r->data.egrade) < j)
printe(r);
r = r->next;
}
}
else
{
printwrong();
}
}
/*
void Save1(Link l)
{
FILE* fp;
Node* p;
int flag = 1, count = 0;
char pathname[30]="E:\\Student\\stu.txt";
fp = fopen(pathname, "wb");
if (fp == NULL)
{
printf("\n======>提示:打开文件出错!\n");
exit(1);
}
p = l->next;
while (p)
{
if (fwrite(p, sizeof(Node), 1, fp) == 1)
{
p = p->next;
count++;
}
else
{
flag = 0;
break;
}
}
if (flag)
{
printf("\n====>提示:文件保存成功.(有%d条记录已经保存.)\n", count);
shoudsave = 0;
}
fclose(fp);
}
*/
void MySave(Link l)
{
FILE* fp;
Node* p;
int flag = 1, count = 0;
fp = fopen(path, "wb");
if (fp == NULL)
{
printf("\n======>提示:打开文件出错!\n");
exit(1);
}
p = l->next;
fprintf(fp, "Stunum Name Sex Chinese Math English\n");
//fprintf(fp, "-----------------------------------------------------------------------\r\n");
while (p)
{
if (fprintf(fp, "%s %s %s %d %d %d\n",p->data.num,p->data.name,p->data.sex,p->data.cgrade,p->data.mgrade,p->data.egrade) > 0)
{
p = p->next;
count++;
}
else
{
flag = 0;
break;
}
}
if (flag)
{
printf("\n====>提示:文件保存成功.(有%d条记录已经保存.)\n", count);
shoudsave = 0;
}
fclose(fp);
}
void readIn(char* path)
{
FILE* fp;
dataIn* pHead;
char a, b;
fp = fopen(path, "r+");
//rewind(fp);
if (fp == nullptr)
{
printf("文件打开失败!\n");
return;
}
while (!feof(fp))
{
pHead = (dataIn*)malloc(sizeof(dataIn));
fscanf(fp, "%s %s %s %s %s %s\n", pHead->num, pHead->name, pHead->sex, pHead->cgrade, pHead->mgrade, pHead->egrade);
if (strcmp(pHead->num, "Stunum") == 0)
{
printf("%s \t%s \t%s \t%s \t%s \t%s\n", pHead->num, pHead->name, pHead->sex, pHead->cgrade, pHead->mgrade, pHead->egrade);
}
else
{
printf("%s \t\t%s \t%s \t%s \t%s \t%s\n", pHead->num, pHead->name, pHead->sex, pHead->cgrade, pHead->mgrade, pHead->egrade);
}
}
fclose(fp);
}
int main()
{
Link l; //链表
FILE* fp; //文件指针
char pathname[30] = "E:\\Student";
int sel;
char ch;
//char jian;
int count = 0;
//Node* p;
Node* r;
printf("\t\t学生成绩管理系统\n\t\t--by xx1801 xxx 5120185935\n");
l = (Node*)malloc(sizeof(Node));
l->next = NULL;
r = l;
fp = fopen(pathname, "wb");
loop:menu();
printf("请选择操作:");
scanf("%d", &sel);
if (sel == 0)
{
if (shoudsave == 1)
{
getchar();
printf("\n======>提示:资料已经改动,是否将改动保存到文件中(y|n)?\n");
scanf("%c", &ch);
if (ch == 'y' || ch == 'Y')
{
MySave(l);
}
printf("\n======>提示:你已经退出系统,再见!\n");
return 0;
}
}
switch (sel)
{
case 1:Add(l); goto loop; break; //增加
case 2:Del(l); goto loop; break; //删除
case 3:Qur(l); goto loop; break; //查询
case 4:Modify(l); goto loop; break; //修改
case 5:Disp(l); goto loop; break; //显示
case 6:Tongji(l); goto loop; break; //统计
case 7:Chinese(l); goto loop; break; //语文
case 8:Maths(l); goto loop; break; //数学
case 9:English(l); goto loop; break; //英语
case 10:Select(l); goto loop; break; //选择分段
case 11:MySave(l); goto loop; break; //保存文件
case 12:printf("*************帮助信息**********\n"); goto loop; break;
case 13:readIn(path); goto loop; break;
default:printwrong(); getchar(); break;
}
return 0;
}