C语言-学生管理系统

可根据学生的信息进行学号排序、平均分排序等操作。(需要建立文件)

具体功能请看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);
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值