可根据学生的信息进行学号排序、平均分排序等操作。(需要建立文件)
具体功能请看menu函数
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define N 5 //文件
typedef struct student
{
char number[10];
char name[15];
int C;//Chinese
int M;//Math
int E;//English
float ave;//self_ave
struct student *next;
}stu;
void menu();//菜单
stu *creatNode();//创建带头结点链表
void output(stu *head);//查看全部学生信息
void fail(stu *head);//查看挂科学生
stu *NoSort(stu *head);//按学号排序
stu *AveSort(stu *head);//按平均分排序
void AveScore(stu *head,float a[]);//计算各科平均分
void freeNode(stu *head);//销链
void save(stu *head); //保存数据
int main()
{
stu *head=NULL,*h;
float a[3]={0};
int num,last=0;//last 用来区别保存的数据
head=creatNode();
printf("读取文件完毕,按任意键继续\n");
getch();
system("cls");
while(menu(),scanf("%d",&num)!=EOF)
{
switch(num)
{
case 0:printf("系统正在退出,请您耐心等待。\n");Sleep(1000);exit(0);
case 1:output(head);break;
case 2:fail(head);break;
case 3:h=NoSort(head);output(h);freeNode(h);break;
case 4:h=AveSort(head);output(h);freeNode(h);break;
case 5:AveScore(head,a);;break;
case 6: if(last==5||last==1)
save(head);
else
{
save(h);
free(h);
}
printf("\n保存完毕\n");break;
default :printf("请输入0和6之间的数字...");break;
}
printf("\n按任意键继续\n");
getch();
system("cls");
last=num;
}
free(head);//释放链表空间
return 0;
}
void menu()//菜单
{
printf("\n");
printf("--------------------【学生信息管理系统】--------------------\n");
printf("1.查看全部学生信息\n");
printf("2.查看挂科学生信息\n");
printf("3.按学号排序\n");
printf("4.按平均分排序\n");
printf("5.计算各科平均分\n");
printf("6.保存当前数据\n");
printf("0.退出\n");
}
stu *creatNode()//创建带头结点链表
{
char number[10];
char name[15];
int C;//Chinese
int M;//Math
int E;//English
float ave;//self_ave
FILE *fp;
if((fp=fopen("data.txt","r"))==NULL)
{
printf("can not open this file\n");
exit(0);
}
stu *h=NULL,*tail,*p;
int i;
h=(stu *)malloc(sizeof(stu));
h->next=NULL;
tail=h;
for(i=0;i<N;i++)//可以用feof
{
p=(stu *)malloc(sizeof(stu));
fscanf(fp,"%s%s%d%d%d%f",&number,&name,&C,&M,&E,&ave);
strcpy(p->number,number);strcpy(p->name,name);//可以用fread
p->C=C;p->M=M;p->E=E;p->ave=ave;
p->next=NULL;
tail->next=p;
tail=p;
}
fclose(fp);
return h;
}
void freeNode(stu *head)//销链
{
stu *p=head;
while(p)
{
head=head->next;
free(p);
p=head;
}
}
void output(stu *head)//输出全部学生的信息
{
system("cls");
stu *p=head->next;
printf("\n\n--------------------------------------------------------------\n");
printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
while(p)
{
printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
p=p->next;
}
}
void fail(stu *head)//输出挂科学生信息
{
system("cls");
stu *p=head->next;
printf("\n\n--------------------------------------------------------------\n");
printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
while(p)
{
if(p->C<60||p->E<60||p->M<60)
printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
p=p->next;
}
}
stu *NoSort(stu *head)//按学号排序
{
system("cls");
stu *p=head->next,*q,*h=NULL,*s;
h=(stu *)malloc(sizeof(stu));
h->next=NULL;
while(p)
{
q=h;
s=(stu *)malloc(sizeof(stu));
s->C=p->C;
s->E=p->E;
s->M=p->M;
s->ave=p->ave;
strcpy(s->name,p->name);
strcpy(s->number,p->number);
s->next=NULL;
//插入排序
if(h->next==NULL)
h->next=s;
else
{
while(q->next)
{
if(strcmp(q->next->number,s->number)>0) break;
q=q->next;
}
if(q->next==NULL)
q->next=s;
else
{
s->next=q->next;
q->next=s;
}
}
p=p->next;
}
return h;
}
stu *AveSort(stu *head)//按平均分排序
{
system("cls");
stu *p=head->next,*q,*h=NULL,*s;
h=(stu *)malloc(sizeof(stu));
h->next=NULL;
while(p)
{
q=h;
s=(stu *)malloc(sizeof(stu));
s->C=p->C;
s->E=p->E;
s->M=p->M;
s->ave=p->ave;
strcpy(s->name,p->name);
strcpy(s->number,p->number);
s->next=NULL;
if(h->next==NULL)
h->next=s;
else
{
while(q->next)
{
if(q->next->ave < s->ave) break;
q=q->next;
}
if(q->next==NULL)
q->next=s;
else
{
s->next=q->next;
q->next=s;
}
}
p=p->next;
}
return h;
}
void AveScore(stu *head,float a[])//计算各科平均分
{
system("cls");
stu *p=head->next;
int i=0;
printf("\n\n--------------------------------------------------------------\n");
printf("%10s%10s%10s\n","语文","数学","英语");
while(p)
{
i++;
a[0]+=p->C ;
a[1]+=p->M ;
a[2]+=p->E ;
p=p->next;
}
a[0]/=i;a[1]/=i;a[2]/=i;
printf("%10.2f%10.2f%10.2f\n",a[0],a[1],a[2]);
}
void save(stu *head)//保存数据
{
system("cls");
FILE *fp;
stu *p=head->next;
if((fp=fopen("data.txt","w+"))==NULL)
{
printf("can not save this file\n");
exit(0);
}
while(p!=NULL)
{
fprintf(fp,"%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
p=p->next;
}
fclose(fp);
}