当前位置:编程学习 > C/C++ >>

有序的结构体数组

[cpp]  
*/             
* 程序的版权和版本声明部分             
*Copyright (c)2013, 烟台大学计算机学院学生             
* All rightsreserved.             
*文件名称: array.cpp                                        
* 作    者:杨绍宁                                        
* 完成日期: 2012 年3 月9 日             
* 版本号: v1.0                   
*             
* 输入描述:已经在程序初始化中  
*问题描述:将学生成绩信息存储在结构体数组中,对结构体并输出排序   
*输出:按C++降序和按学号升序排列后的学生成绩单       
*/               
#include  
using namespace std;  
struct Score  
{  
    char num[14];  
    int cpp;  
    int math;  
    int english;  
};  
void sort1(Score[],int); //要自定义的函数  
void sort2(Score[],int);  
void output(Score[],int);  
int main()  
{  
    Score score[]={{"201152501104",65,69 ,68 },  
    {"201152501114",94 ,89 ,63 },  
    {"201152501138",67 ,62 ,84 },  
    {"201152501204",100 ,65 ,91 },  
    {"201152501202",59 ,80 ,55 },  
    {"201152501115",92 ,84 ,60 },  
    {"201152501205",80 ,92 ,71 },  
    {"201152501141",88 ,56 ,67 },  
    {"201152501203",62 ,62 ,95 },  
    {"201152501140",80 ,60 ,86 },  
    {"201152501201",73 ,90 ,94}};  
    int stuNum=sizeof(score)/sizeof(score[0]);  
    //将所有同学按C++降序排序后输出  
    sort1(score,stuNum);  
    cout<<"按C++降序排序后:"<
    output(score,stuNum);  
    //将所有同学按学号升序排序后输出  
    sort2(score,stuNum);  
    cout<<"按学号升序排序后:"<
    output(score,stuNum);  
    return 0;  
}  
void sort1(Score score[],int num)   
{  
    int i,j;  
    Score t;             //因为score是结构体的变量,所以t也得是啊  
    for(j=0;j
        for(i=0;i
            if(score[i].cpp
                t=score[i];   
                score[i]=score[i+1];           
                score[i+1]=t;}  
                  
}  
void sort2(Score score[],int num)   
{  
    int i,j;  
    Score t;  
  
    for(j=0;j
        for(i=0;i
            if(strcmp(score[i].num,score[i+1].num)>0){      //学号是字符型  
                t=score[i];  
                score[i]=score[i+1];  
                score[i+1]=t;}  
                  
}  
void output(Score score[],int num )  
{  www.zzzyk.com
    int i;  
    for(i=0;i<11;i++)  
        cout<
}  
结果:
感受:虽然明白了些,但还有不懂得,好好看看吧!!!
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,