include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
typedef struct Node{
int id;//学号
char name[50];//姓名
int ma,C,da;//科目
int sum;//总分
int ave;//平均分
struct Node *next;//指针域
}node;
node List;//链表
//读取文件
int readFile(node *L);
//保存文件
int saveFile(node *L);
// 主菜单界面
void welcome();
//增加学生信息
void printAddStuInfo();//界面
void insertStuInfo(node *L,node e);//功能
//修改学生信息
void printFixStuInfo(node *L);
//查询学生信息
void printSearchStuInfo(node *L);
//按学号进行查找
node * searchStuInfoById(int id,node *L);
//按姓名进行查找
node * searchStuInfoByName(char name[],node *L);
//输出学生信息
void printStuInfo(node *L);
//排序比较规则
bool cmp(node a,node b){
return a.sum>b.sum;
}
void stuScoreSort(node *L){
for(node *p=L->next;p!=NULL;p=p->next){
for(node *q=p;q!=NULL;q=q->next){
if(!cmp(*p,*q)){
//交换数据域
node t=*p;
*p=*q;
*q=t;
//处理指针域
t.next=p->next;
p->next=q->next;
q->next=t.next;
}
}
}
}
int findMaxScore(node *L,int mode){
//mode 0:高数 1:C语言 2:数据结构
node *p=L->next;
if(p!=NULL){
int maxs[5]={-1,-1,-1};
while(p!=NULL){
if(p->ma>maxs[0]) maxs[0]=p->ma;
if(p->C>maxs[1]) maxs[1]=p->C;
if(p->da>maxs[2]) maxs[2]=p->da;
p=p->next;
}
return maxs[mode];
}
return -1;
}
int findMinScore(node *L,int mode){
//mode 0:高数 1:C语言 2:数据结构
node *p=L->next;
if(p!=NULL){
int mina[5]={1000,1000,1000};
while(p!=NULL){
if(p->ma<mina[0]) mina[0]=p->ma;
if(p->C<mina[1]) mina[1]=p->C;
if(p->da<mina[2]) mina[2]=p->da;
p=p->next;
}
return mina[mode];
}
return 1000;
}
//退出程序
void goodBye();
int main(){
int choice=0;
readFile(&List);
while(true){
welcome();
scanf("%d",&choice);
switch(choice){
case 1://增加学生信息
printAddStuInfo();
break;
case 2://修改学生信息
printFixStuInfo(&List);
break;
case 3://查询学生信息
printSearchStuInfo(&List);
break;
case 4://输出学生信息
printStuInfo(&List);
break;
case 0://退出程序
goodBye();
break;
}
printf("是否需要继续操作?(yes:1 / no:0 ):");
scanf("%d",&choice);
if(choice==0){
break;
}
}
return 0;
}
//主菜单界面
void welcome(){
system("cls");
printf("************************\n");
printf("** 学生成绩管理系统 **\n");
printf("** 作者:*** **\n");
printf("** **\n");
printf("** 增加学生信息 ---1 **\n");
printf("** 修改学生信息 ---2 **\n");
printf("** 查询学生信息 ---3 **\n");
printf("** 输出学生信息 ---4 **\n");
printf("** 退出管理系统 ---0 **\n");
printf("请输入对应的功能键(数字): ");
}
//增加学生信息
void printAddStuInfo(){
//
system("cls");
node st;
printf("请输入新增学生相关信息\n");
printf("学号:");
scanf("%d",&st.id);
printf("姓名:");
scanf("%s",st.name);
printf("高数:");
scanf("%d",&st.ma);
printf("C语言:");
scanf("%d",&st.C);
printf("数据结构:");
scanf("%d",&st.da);
st.sum=st.ma+st.C+st.da;
st.ave=st.sum/3;
insertStuInfo(&List,st);
}
//插入学生信息
void insertStuInfo(node *L,node e){
//头插法
node *h=L;
node *s=(node *)malloc(sizeof(node));
*s=e;
s->next=h->next;
h->next=s;
//保存文件
saveFile(L);
}
//修改学生信息
void printFixStuInfo(node *L){
system("cls");
int id;
int choice=-1;
printf("请输入要查找的学生学号");
scanf("%d",&id);
node *st=searchStuInfoById(id,L);
if(st==NULL){
printf("查无此人!");
return;
}
st=st->next;
while(1){
system("cls");
printf("_________________________________________________________\n");
printf("|学号\t|姓名\t|高数\t|C语言\t|数据结构|总分\t|平均分\t|\n");
printf("____________________________________________________________\n");
printf("%d\t|%s\t|%d\t|%d\t|%d| %d\t|%d\t|\n",st->id,st->name,st->ma,st->C,st->da,st->sum,st->ave);
printf("_________________________________________________________\n");
printf("修改姓名---- 1\n");
printf("修改高数---- 2\n");
printf("修改c语言---- 3\n");
printf("修改数据结构----4\n");
printf("请输入要修改的信息: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("请输入姓名:");
scanf("%s",st->name);
break;
case 2:
printf("请输入高数:");
scanf("%d",&st->ma);
break;
case 3:
printf("请输入C语言:");
scanf("%d",&st->C);
break;
case 4:
printf("请输入数据结构:");
scanf("%d",&st->da);
break;
}
st->sum=st->ma+st->C+st->da;
printf("是否继续修改学生信息?(y-1 / n-0)\n");
scanf("%d",&choice);
if(choice == 0){
break;
}
}
printf("_________________________________________________________\n");
printf("|学号\t|姓名\t|高数\t|C语言\t|数据结构|总分\t|平均分\t|\n");
printf("_________________________________________________________\n");
printf("%d\t|%s\t|%d\t|%d\t|%d |%d\t|%d\t|\n",st->id,st->name,st->ma,st->C,st->da,st->sum,st->ave);
printf("_________________________________________________________\n");
saveFile(L);
}
//查询学生信息
void printSearchStuInfo(node *L){
system("cls");
int choice=0;
int id;
char name[50];
node *st;
printf("按学号查询----- 1\n");
printf("按姓名查询----- 2\n");
printf("请输入查询方式:");
scanf("%d",&choice);
if(choice == 1){
printf("请输入要查询的学号:");
scanf("%d",&id);
st=searchStuInfoById(id,L);
if(st==NULL){
printf("查无此人!\n");
}else{
st=st->next;
printf("_________________________________________________________\n");
printf("|学号\t|姓名\t|高数\t|C语言\t|数据结构|总分\t|平均分\t|\n");
printf("_________________________________________________________\n");
printf("%d\t|%s\t|%d\t|%d\t|%d |%d\t|%d\t|\n",st->id,st->name,st->ma,st->C,st->da,st->sum,st->ave);
printf("_________________________________________________________\n");
}
}else if(choice ==2){
printf("请输入要查询的姓名:");
scanf("%s",name);
st=searchStuInfoByName(name,L);
if(st==NULL){
printf("查无此人!\n");
}else{
st=st->next;
printf("_________________________________________________________\n");
printf("|学号\t|姓名\t|高数\t|C语言\t|数据结构|总分\t|平均分\t|\n");
printf("_________________________________________________________\n");
printf("%d\t|%s\t|%d\t|%d\t|%d\t |%d\t|%d\t|\n",st->id,st->name,st->ma,st->C,st->da,st->sum,st->ave);
printf("_________________________________________________________\n");
}
}
}
//按学号进行查找
node * searchStuInfoById(int id,node *L){
node *p=L;
while(p->next!=NULL){
if(p->next->id==id){
return p;
}
p=p->next;
}
return NULL;
}
//按姓名进行查找
node * searchStuInfoByName(char name[],node *L){
node *p=L;
while(p->next!=NULL){
if(strcmp(name,p->next->name)==0){
return p;
}
p=p->next;
}
return NULL;
}
//输出学生信息
void printStuInfo(node *L){
system("cls");
//学生成绩从高到低排序
stuScoreSort(L);
node *p=L->next;
printf("_________________________________________________________\n");
printf("|学号\t|姓名\t|高数\t|C语言\t|数据结构|总分\t|平均分\t|\n");
printf("_________________________________________________________\n");
if(p!=NULL){
while(p!=NULL){
printf("%d\t|%s\t|%d\t|%d\t|%d |%d\t|%d\t|\n",p->id,p->name,p->ma,p->C,p->da,p->sum,p->ave);
printf("______________________________________________________\n");
p=p->next;
}
}
printf("最高分:\n");
printf("高数:%d\n",findMaxScore(L,0));
printf("C语言:%d\n",findMaxScore(L,1));
printf("数据结构: %d\n",findMaxScore(L,2));
printf("\n");
printf("最低分:\n");
printf("高数:%d\n",findMinScore(L,0));
printf("C语言:%d\n",findMinScore(L,1));
printf("数据结构: %d\n",findMinScore(L,2));
printf("\n");
printf("数学平均分为:%d\n",aveScore(L,0);
printf("C语言平均分为:%d\n",aveScore(L,1);
printf("数据结构平均分为:%d\n",aveScore(L,2));
}
//退出程序
void goodBye(){
system("cls");
printf("欢迎下次使用~\n");
exit(0);//结束程序
}
// 文件输入
int readFile(Node *L){
FILE *fpr=fopen("D:\\studentInfo.txt","r");
node st;
node *s;
node *t=L;
if(fpr==NULL){
return 0;
}else{
//fcanf()
while(fscanf(fpr,"%d %s %d %d %d %d %d",&st.id,st.name,&st.ma,&st.C,&st.da,&st.sum,&st.ave)!=EOF){
s=(node *)malloc(sizeof(node));
*s=st;
t->next=s;
t=s;
t->next=NULL;
}
}
fclose(fpr);//关闭文件指针
return 1;
}
//保存文件
int saveFile(node *L){
FILE *fpw=fopen("D:\\studentInfo.txt","w");
if(fpw==NULL) return 0;
//fprintf(fpw,"xxx",);
node *p=L->next;
while(p!=NULL){
fprintf(fpw,"%d %s %d %d %d %d %d\n",p->id,p->name,p->ma,p->C,p->da,p->sum,p->ave);
p=p->next;
}
fclose(fpw);//关闭文件指针
return 1;
}
计算每门课的平均分,计算每个学生多门课的平均成绩并按照平均成绩从高到低进行排序,依据每个学生的平均成绩对其进行优(90分以上)、良(80-90)、中(60-80)、差(60分以下)等级划分。