回 帖 发 新 帖 刷新版面

主题:[讨论]高手帮忙添加个功能~学生成绩管理程序

在网上找了些例子,自己改进了一点,但还不够好,想把程序再多个功能:显示每科的全部同学的最高分和最低分。我还是个初学者,对我来说有点难,请高手帮个忙~(注释有错的帮忙纠正下)谢谢~~
[code=c]
请填写代码
[/code]#include<stdio.h>
#define N 50            /*宏定义*/
struct stu              /*定义学生类型结构体*/
{    
    int sum;            /*定义sum为整形变量*/
    int num;            /*定义num为整形变量*/    
    char name[20];        /*定义一维字符name含20个元素(学生姓名)*/    
    float score[6];        /*定义单精度一维字符score含6个元素(科目)*/
    float average;        /*定义字符串average为单精度(成绩平均分)*/
};
int count=0;            /*定义字符串count为整形变量,并赋初值为0*/        
void display(struct stu []);        /*声明未定义的显示输出函数*/
struct stu input();            /*输入学生的成绩*/
void sort(struct stu []);        /*声明未定义的排序函数*/
void insert(struct stu infm[]);        /*声明未定义的插入函数*/
void search(struct stu infm[]);        /*声明未定义的搜索函数*/
void del(struct stu infm[]);        /*声明未定义的删除函数*/
void main()            /*主函数*/
{    
    char ch;            /*定义ch为字符变量*/    
    struct stu infm[N];        /*infm[N]是struct stu类型*/
    int k;            /*定义k为整形变量*/
    do     
    {    
    printf("****************************欢迎使用学生成绩管理程序****************************\n请输入要实现的功能:\n1、录入;2排序;3、显示;4、查找;5、插入;6、删除\n");        /*显示欢迎语句和输入提示*/
    scanf("%d",&k);            /*从键盘输入内容赋值给k*/
    switch(k)    
        {    
            case 1:        /*选1录入信息*/    
                {            
                    do         
                        {            
                            infm[count]=input();            /**/
                            count++;                /**/
                            printf("是否继续录入?(y/n)");        /*显示输入提示*/
                            fflush(stdin);            /*清空输入缓冲区*/        
                            scanf("%c",&ch);            /*从键盘上输入内容赋值给ch*/            
                        }
                    while(ch=='y'||ch=='Y');    /**/        
                    display(infm);        /*调用函数*/        
                    break;                    
                }    
            case 2:                    /*选2排序*/
                {            
                    sort(infm);        /*调用函数*/                
                    break;         
                }
            case 3:                    /*选3显示信息*/
                {            
                    display(infm);        /*调用函数*/        
                    break;            
                }    
            case 4:                /*选4查找信息*/
                {            
                    search(infm);/*调用函数*/    
                    break;    
                }    
            case 5:        /*选5插入信息*/
                {            
                    insert(infm);/*调用函数*/                
                    break;    
                }    
            case 6:        /*选6删除信息*/
                {            
                    del(infm);    /*调用函数*/            
                    break;        
                }    
            default:
                {        
                printf("输入错误!");/*显示输入错误*/        
                }    
        }
        printf("是否继续?(y/n)");    /*显示输入提示*/    
        fflush(stdin);        /*清空输入缓冲区*/    
        scanf("%c",&ch);        /*从键盘上输入内容赋值到ch的地址中*/    
    }
    while(ch=='y'||ch=='Y');        
}
struct stu input()            /*输入学生的成绩*/
    {    
        struct stu in;        /*in是struct stu类型*/    
        int i;            /*定义i为整形变量*/
        printf("请输入学生学号:");    /*显示输入提示*/
        fflush(stdin);        /*清空输入缓冲区*/
        scanf("%d",&in.num);    /*输入学号*/
        printf("请输入学生姓名:");    /*显示输入提示*/
        fflush(stdin);        /*清空输入缓冲区*/
        scanf("%s",in.name);    /*输入名字*/        
        for(i=0;i<6;i++)            
            {            
                printf("请输入学生%c",i);    /*显示输入提示*/
                switch(i)
                {
                    case 0:        /*输入英语成绩*/
                        {
                        printf("英语成绩:");
                        break;
                        }
                    case 1:        /*输入数学成绩*/
                        {
                        printf("数学成绩:");
                        break;
                        }
                    case 2:        /*输入历史成绩*/
                        {
                        printf("历史成绩:");
                        break;
                        }
                    case 3:        /*输入C语言成绩*/
                        {
                        printf("C语言成绩:");
                        break;
                        }
                    case 4:        /*输入工程制图成绩*/
                        {
                        printf("工程制图成绩:");
                        break;
                        }
                    case 5:        /*输入物理成绩*/
                        {
                        printf("物理成绩:");
                        break;
                        }
                
                }
                fflush(stdin);                /*清空输入缓冲区*/            
                scanf("%f",&in.score[i]);            /*输入各科的成绩*/            
            }        
        in.average=(in.score[0]+in.score[1]+in.score[2]+in.score[3]+in.score[4]+in.score[5])/6;/*求平均分*/
        in.sum=(in.score[0]+in.score[1]+in.score[2]+in.score[3]+in.score[4]+in.score[5]);    /*求总分*/
        return in;                            /*返回in*/
    }
void sort(struct stu infm[])                    /*学生成绩排序函数*/
    {    
        int i,j;                    /*定义i,j为整形变量*/    
        struct stu temp;            /*temp是struct stu类型*/
        printf("排序前的学生顺序为:");        /*输出语句*/
        display(infm);                /*输出排序前的成绩*/
        for(i=0;i<count-1;i++)                
            {        
                for(j=i+1;j<count;j++)                        
                    {            
                        if(infm[i].average<infm[j].average)    /*比较平均值*/            
                            {                
                                temp=infm[i];                
                                infm[i]=infm[j];            
                                infm[j]=temp;    /*infm[i]的值和infm[j]的值交换*/            
                            }        
                    }    
            }    
        printf("排序后的学生顺序为:");        
        display(infm);            /*显示排序后的学生成绩*/
    }
void display(struct stu out[])                /*显示学生成绩*/
    {        
        int i;                /*定义i为整形变量*/    
        printf("学号\t姓名\t总分\t平均成绩\n");        /*显示学号,姓名,总分,平均成绩*/
        for(i=0;i<count;i++)            
            {        
                printf("%d\t%s\t%d\t%3.1f\n",out[i].num,out[i].name,out[i].sum,out[i].average);    /*显示学号,姓名,总分,平均成绩*/
            }
        
    }
void search(struct stu infm[])                /*查找函数*/
    {    
        int i, num;            /*定义i和num为整形变量*/
        printf("请输入要查找学生的学号:");    /*显示输入提示*/
        scanf("%d",&num);            /*输入要查找的学生学号*/
        for(i=0;i<count;i++)            /**/
            {        
            if(infm[i].num==num)    /**/    
                {            
                    break;        
                }    
            }    
        printf("学号\t姓名\t\t平均成绩\n");    
        printf("%d\t%s\t%4.1f\t",infm[i].num,infm[i].name,infm[i].average);        /*输出查找到的学生信息*/
    }
void insert(struct stu infm[])            /*插入新的学生成绩*/
    {    
        int i,j;            /*定义i,j为整形变量*/
        struct stu temp;        /*temp是struct stu类型*/
        temp=input();        /*输入新插入的学生成绩*/
        for(i=0;i<count;i++)    /**/
            {        
                if(temp.average>infm[i].average)/*比较新插入的学生成绩的平均值与原先的比较*/    
                    {            
                        break;        
                    }    
            }    
        for(j=count;j>i;j--)            /**/
            {        
                infm[j]=infm[j-1];    /**/
            }    
        infm[i]=temp;            
        count++;                /**/
        printf("插入后的学生顺序为:\n");    /*显示输入提示*/
        display(infm);            /*输出插入后的成绩排序*/
    }
void del(struct stu infm[])                /*删除学生成绩信息*/
    {        
        int i,j,number;            /*定义i,j和number为整形变量*/
        printf("请输入要删除学生的学号:");    /*显示输入提示*/
        scanf("%d",&number);        /*从键盘输入内容赋值给number*/
        for(i=0;i<count;i++)            /*输入将删除的学生成绩学号*/
            {        
                if(infm[i].num==number)    /*找到学生学号*/
                    {        
                        break;
                    }    
            }    
        for(j=i;j<count-1;j++)        /*删除成绩*/
            {        
                infm[j]=infm[j+1];    /**/
            }    
        count--;                /**/    
        printf("删除后的学生顺序为:\n");    /*显示输入提示*/
        display(infm);            /*显示删除后的学生成绩*/
    }

回复列表 (共2个回复)

沙发

天下没有免费的午餐,如果你能看懂自己找来的代码,那么你想要的功能对你来说一点也不难

板凳

嗯,说的是

我来回复

您尚未登录,请登录后再回复。点此登录或注册