首页 > 代码库 > 成绩处理结构体

成绩处理结构体

  输入代码:

/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:sum123.cpp
*作    者:林海云
*完成日期:2014年12月23日
*版 本 号:v2.0
*
*问题描述:按要求输出学生成绩
*程序输入:
*程序输出:
*/
#include<iostream>
#include<cstdio>
using namespace std;
struct Student
{
    char num[13];
    char name[10];
    int cpp;
    int math;
    int English;
    int grade;
    double average;
};
void calculate(Student s[],int n);
void sort1(Student [],int n );
void sort2(Student [],int n );
void output(Student [],int n );
void outputExcellent(Student [],int n );
int main()
{
    Student stu[]= {{"201152501104","Tom",65,69 ,68 },
        {"201152501114","Jeery",94 ,89 ,63 },
        {"201152501138","Speike",67 ,62 ,84 },
        {"201152501204","Tyke",100 ,95 ,91 },
        {"201152501202","Tuffy",59 ,80 ,55 },
        {"201152501115","Butch",100 ,99 ,89 },
        {"201152501201","Light",95 ,92 ,99 },
        {"201152501145","Topsy",88 ,56 ,67 },
        {"201152501203","Droopy",62 ,62 ,95 },
        {"201152501140","Drupa",80 ,60 ,86 },
        {"201152501205","Buzz",73 ,90 ,94}
    };
    int stuNum=sizeof(stu)/sizeof(stu[0]);
    calculate(stu,stuNum);
    cout<<"下面是成绩单:"<<endl;
    output(stu,stuNum);
    cout<<"优秀者名单:"<<endl;
    outputExcellent(stu,stuNum);
    sort1(stu,stuNum);
    cout<<"按总分降序排序后:"<<endl;
    output(stu,stuNum);
    sort2(stu,stuNum);
    cout<<"按学号升序排序后:"<<endl;
    output(stu,stuNum);
    return 0;
}
void calculate(Student s[],int n)
{
    int i;
    for(i=0; i<n; ++i)
    {
        s[i].grade=s[i].cpp+s[i].math+s[i].English;
        s[i].average=s[i].grade/3;
    }
}
void sort1(Student s[],int n )
{
    int i,j;
    Student t;
    for(i=0; i<n-1; ++i)
        for(j=0; j<n-i-1; ++j)
        {
            if(s[j].grade<s[j+1].grade)
            {
                t=s[j];
                s[j]=s[j+1];
                s[j+1]=t;
            }
        }
}
void sort2(Student s[],int n )
{
    int i,j;
    Student t;
    for(i=0; i<n-1; ++i)
        for(j=0; j<n-i-1; ++j)
        {
            if(s[j].num>s[j+1].num)
            {
                t=s[j];
                s[j]=s[j+1];
                s[j+1]=t;
            }
        }
}
void output(Student s[],int n )
{
    int i;
    cout<<"学号    姓名   总分    均分 :"<<endl;
    for(i=0; i<n; ++i)
    {
        cout<<s[i].num<<"  "<<s[i].name<<"  "<<s[i].grade<<"  "<<s[i].average<<endl;
    }
}
void outputExcellent(Student s[],int n )
{
    int i,j;
    for(i=0; i<n; ++i)
    {
        if(s[i].grade>285&&s[i].cpp>=90&&s[i].math>=90&&s[i].English>=90)
        {
            cout<<"学号    姓名   总分    均分 :"<<endl;
            for(j=0; j<n; j++)
            {
                cout<<s[j].num<<"  "<<s[j].name<<"  "<<s[j].grade<<"  "<<s[j].average<<endl;
            }
        }
    }
}








运行结果:

技术分享



成绩处理结构体