秩序化された構造体配列

4096 ワード

/*   
*             :   
* Copyright (c) 2013,             
* All rights reserved.   
*     :test.cpp   
*       :     
*     :2014  3  6     
*      :v1.0   
*              :  
*     :    
*     :         
*     :    
*     :   
*     :    
*/      

#include <iostream>
#include <iomanip>
using namespace std;

struct Student    //   1
{
char num[13];
   char name[10];
   int cpp;
   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);
    //          
    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].math+s[i].cpp+s[i].english;//    
        s[i].average=s[i].grade/3;//     
    }
}
void sort1(Student s[],int n)//       
{
    int i,j;
    Student k;
    for (i=0;i<n-1;i++)
    {
        for (j=0;j<n-i-1;j++)
    {
        if (s[j].grade<s[j+1].grade)
        {
        k=s[j];
        s[j]=s[j+1];
        s[j+1]=k;
        }
    }
    }
}
void sort2(Student s[],int n)//     
{
    int i,j;
    Student k;
    for (i=0;i<n;i++)
    {
   for (j=0;j<n-i;j++)
    {
       if(s[i].num>s[i+1].num)
        {
        k=s[j];
        s[j]=s[j+1];
        s[j+1]=k;
        }
    }
    }
}
void output(Student s[],int n)//     
{
    cout<<"|+"<<setw(9)<<"  "<<setw(8)<<"  "<<setw(16)<<"  "<<setw(18)<<"  "<<endl;
    for (int i=0;i<n;i++)
    {
        cout<<"|+"<<setw(9)<<s[i].num<<setw(8)<<s[i].name<<setw(16)<<s[i].grade<<setw(18)<<s[i].average<<endl;
    }
}
void outputExcellent(Student s[],int n)//     :   285,     90
{
    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;
       }
    }
}

演算結果:
心得体得:やはり熟練していません