项目二有序的结构体数组

/*
*Copyright (c) 2013 ,烟台大学计算机学院
*All rights reserved.
*作者:王至超
*完成日期:2014年03月05
*版本号:v1.0
*问题描述:利用结构体来处理成绩
*样例输入:
*样例输出:
*问题分析:用简单的方法,学会活学活用
*/
#include <iostream>
#include <string.h>
using namespace std;
struct Student
{
    char num[13];//学号
    char name[10];//名字
    int cpp;//c++成绩
    int math;//数学成绩
    int english;//英语成绩
    int grade;//总成绩
    double average;//平均成绩
};
void calculate(Student s[],int n);//计算总分和均分,数据在s数组中,共有n个元素
void sort1(Student[],int);//按总分降序排序
void sort2(Student[],int);//按学号升序排序
void output(Student[],int);//输出成绩单
void outputExcellent(Student[],int);//输出优秀者:总分超过285,单科不低于90
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","Lightning",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]);  //用sizeof运算确定数组中元素个数
    //计算总分和均分后输出
     calculate(stu,stuNum);
    cout<<"下面是成绩单:"<<endl;
    output(stu,stuNum);
    cout<<"优秀者名单:"<<endl;
    outputExcellent(stu,stuNum);
    cout<<"按总分降序排序后输出"<<endl;
    sort1(stu,stuNum);
    //cout<<"按总分降序排序后:"<<endl;
    //output(stu,stuNum);
    cout<<"按学号升序排序后:"<<endl;
     sort2(stu,stuNum);
    //cout<<"按学号升序排序后:"<<endl;
    // output(stu,stuNum);

    return 0;
}
void calculate(Student s[],int n)
{
    int i,j;
    for(i=0; i<n; i++)
    {
        s[i].grade=s[i].cpp+s[i].math+s[i].english;

        s[i].average=(s[i].cpp+s[i].math+s[i].english)/3;
    }
    for(j=0; j<n; j++)

    {
        cout<<s[j].num<<" "<<s[j].name<<" "<<s[j].cpp<<" "<<s[j].math<<" "<<s[j].english<<" "<<s[j].grade<<" "<<s[j].average;
        cout<<endl;
    }
}
void output(Student s [],int n)//输出成绩单

{
    int i,j;
    for(i=0; i<n; i++)
    {
        s[i].grade=s[i].cpp+s[i].math+s[i].english;

        s[i].average=(s[i].cpp+s[i].math+s[i].english)/3;
    }
    for(j=0; j<n; j++)

    {
        cout<<s[j].num<<" "<<s[j].name<<" "<<s[j].cpp<<" "<<s[j].math<<" "<<s[j].english<<" "<<s[j].grade<<" "<<s[j].average;
        cout<<endl;

    }
}

void outputExcellent(Student s[],int n)

{  int i;
    for(i=0;i<n;i++)
    {
        if(s[i].grade>285&&s[i].cpp>=90&&s[i].math>=90&&s[i].english>=90)
        cout<<s[i].num<<" "<<s[i].name<<endl;
    }
}
void sort1(Student s[],int n)//按总分降序排序
{
   int i,j;
   Student t;
   for(j=0;j<n;j++)
   {
       for(i=0;i<n-1-j;i++)
       {
           if(s[i].grade<s[i+1].grade)
           {
               t=s[i];
               s[i]=s[i+1];
               s[i+1]=t;
           }
       }
    }

    for(j=0;j<n;j++)
        {
            cout<<s[j].num<<" "<<s[j].name<<" "<<s[j].cpp<<" "<<s[j].math<<" "<<s[j].english<<" "<<s[j].grade<<" "<<s[j].average;
            cout<<endl;
        }
}

void sort2(Student s[],int n)
{
    int i,j;
   Student t;
   for(j=0;j<n;j++)
   {
       for(i=0;i<n-1-j;i++)
       {
           if(strcmp(s[i].num,s[i+1].num)>0)
           {
               t=s[i];
               s[i]=s[i+1];
               s[i+1]=t;
           }
       }
    }

    for(j=0;j<n;j++)
        {
            cout<<s[j].num<<" "<<s[j].name<<" "<<s[j].cpp<<" "<<s[j].math<<" "<<s[j].english<<" "<<s[j].grade<<" "<<s[j].average;
            cout<<endl;
        }
}

项目二有序的结构体数组_第1张图片

你可能感兴趣的:(项目二有序的结构体数组)