c9-4.c 854 B

12345678910111213141516171819202122232425
  1. #include <stdio.h>
  2. struct student // 声明结构体类型struct student
  3. {int num;
  4. char name[20];
  5. float score;
  6. };
  7. int main()
  8. {struct student stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},
  9. {10108,"Ling",73.5},{10110,"Fun",100}}; // 定义结倒构体数组并初始化
  10. struct student temp; //定义结构体变量temp,用作交换时的临时变量
  11. const int n=5;
  12. int i,j,k;
  13. printf("The order is:\n");
  14. for(i=0;i<n-1;i++)
  15. {k=i;
  16. for(j=i+1;j<n;j++)
  17. if(stu[j].score>stu[k].score) // 进行成绩的比较
  18. k=j;
  19. temp=stu[k];stu[k]=stu[i];stu[i]=temp; // stu[k]和stu[i]元素互换
  20. }
  21. for(i=0;i<n;i++)
  22. printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
  23. printf("\n");
  24. return 0;
  25. }