0
点赞
收藏
分享

微信扫一扫

2008秋-计算机软件基础-多关键字排序

/* 多关键字排序:

  先按总分由高到低排序,若总分相同则按数学成绩由高到低排序

,若总分和数学成绩都相同,则按英语成绩由高到低排序。

 */

#include <stdio.h>
struct student

{

  int xuehao;

  char xingming[10];

  int shuxue;

  int yingyu;

  int yuwen;

  int zongfen;

};

typedef struct student S;

void shuruchengji(S a[], int L)

{

   /*elements are stored in a[1] to a[L]*/

   a[1].xuehao=101;

   strcpy(a[1].xingming,"a1");

   a[1].shuxue=80;

   a[1].yingyu=80;

   a[1].yuwen=80;

   a[1].zongfen=240;


   a[2].xuehao=102;

   strcpy(a[2].xingming,"a2");

   a[2].shuxue=70;

   a[2].yingyu=70;

   a[2].yuwen=70;

   a[2].zongfen=210;

   

   a[3].xuehao=103;

   strcpy(a[3].xingming,"a3");

   a[3].shuxue=90;

   a[3].yingyu=80;

   a[3].yuwen=70;

   a[3].zongfen=240;


   a[4].xuehao=104;

   strcpy(a[4].xingming,"a4");

   a[4].shuxue=90;

   a[4].yingyu=70;

   a[4].yuwen=80;

   a[4].zongfen=240;

}

void bubblesortYingYu(S r[],int n)

 { /*elements are stored in r[1] to r[n]*/

  int i,j,flag;

  S temp;

  flag=1;

  i=1;

  while((i<n)) /*外循环控制排序的总趟数*/

   { 

      for(j=n;j>i;j--) /*内循环控制一趟排序的进行*/ 

          if(r[j].yingyu>r[j-1].yingyu)  /*相邻元素进行比较,若逆序就交换*/

           {         

              temp=r[j];

              r[j]=r[j-1];

              r[j-1]=temp;

           }

      i++;

    }

}

void bubblesortShuXue(S r[],int n)

 { /*elements are stored in r[1] to r[n]*/

  int i,j,flag;

  S temp;

  flag=1;

  i=1;

  while((i<n)) /*外循环控制排序的总趟数*/

   { 

      for(j=n;j>i;j--) /*内循环控制一趟排序的进行*/ 

          if(r[j].shuxue>r[j-1].shuxue)  /*相邻元素进行比较,若逆序就交换*/

           {         

              temp=r[j];

              r[j]=r[j-1];

              r[j-1]=temp;

           }

      i++;

    }

}

void bubblesortZongFen(S r[],int n)

 { /*elements are stored in r[1] to r[n]*/

  int i,j,flag;

  S temp;

  flag=1;

  i=1;

  while((i<n)) /*外循环控制排序的总趟数*/

   { 

      for(j=n;j>i;j--) /*内循环控制一趟排序的进行*/ 

          if(r[j].zongfen>r[j-1].zongfen)  /*相邻元素进行比较,若逆序就交换*/

           {         

              temp=r[j];

              r[j]=r[j-1];

              r[j-1]=temp;

           }

      i++;

    }

}

void xianshi(S a[], int L)

{

  int i;

  for(i=1;i<=L;i++)

      printf(" %d %s %d %d %d %d \n",

      a[i].xuehao,a[i].xingming,a[i].shuxue,

      a[i].yingyu, a[i].yuwen, a[i].zongfen);

}

void main()

{


  S a[5];

  shuruchengji(a,4);

  bubblesortYingYu(a,4);

  bubblesortShuXue(a,4);

  bubblesortZongFen(a,4);

  xianshi(a,4);

  getchar();

}

结构体数组排序,作者:EmanLee。



举报

相关推荐

0 条评论