有序的结构体数

#include <iostream>   
using namespace std;  
struct Score  
{  
    char num[14];  
    int cpp;  
    int math;  
    int english;  
};  
;  //要自定义的函数   
void sort1(Score p[],int n);  
void sort2(Score p[],int n);  
void output(Score p[],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 },  
    {"201152501201",80 ,92 ,71 },  
    {"201152501145",88 ,56 ,67 },  
    {"201152501203",62 ,62 ,95 },  
    {"201152501140",80 ,60 ,86 },  
    {"201152501205",73 ,90 ,94}};  
   int stuNum=sizeof(score)/sizeof(score[0]);  
    //将所有同学按C++降序排序后输出   
    sort1(score,stuNum);  
    cout<<"按C++降序排序后:"<<endl;  
    output(score,stuNum);  
    //将所有同学按学号升序排序后输出   
    sort2(score,stuNum);  
    cout<<"按学号升序排序后:"<<endl;  
    output(score,stuNum);  
    return 0;  
}  
void sort1(Score p[],int n)  
{  
    int i,j;  
    Score t[50];  
    for(j=0;j<n;j++)  
       for(i=0;i<n-j;i++)  
            if(p[i].cpp>p[i+1].cpp)  
            {  
                strcpy(t[i].num,p[i].num);  
                strcpy(p[i].num,p[i+1].num);  
                strcpy(p[i+1].num,t[i].num);  
               t[i].cpp=p[i].cpp;  
                p[i].cpp=p[i+1].cpp;  
                p[i+1].cpp=t[i].cpp;  
                t[i].math=p[i].math;  
                p[i].math=p[i+1].math;  
                p[i+1].math=t[i].math;  
                t[i].english=p[i].english;  
                p[i].english=p[i+1].english;  
                p[i+1].english=t[i].english;  
            }  
}  
void sort2(Score p[],int n)  
{  
    int i,j;  
    Score t[50];  
   for(j=0;j<n;j++)  
        for(i=0;i<n-j;i++)  
            if(strcmp(p[i].num,p[i+1].num)>0)  
            {     
                strcpy(t[i].num,p[i+1].num);  
               strcpy(p[i+1].num,p[i].num);  
                strcpy(p[i].num,t[i].num);  
                t[i].cpp=p[i+1].cpp;  
                p[i+1].cpp=p[i].cpp;  
                p[i].cpp=t[i].cpp;  
                t[i].math=p[i+1].math;  
                p[i+1].math=p[i].math;  
                p[i].math=t[i].math;  
                t[i].english=p[i+1].english;  
                p[i+1].english=p[i].english;  
                p[i].english=t[i].english;  
            }  
}  
void output(Score p[],int n)  
{  
    int i;  
    for(i=0;i<n;i++)  
        cout<<p[i].num<<" "<<p[i].cpp<<" "<<p[i].english<<" "<<p[i].math<<endl;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值