fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct{
  4. int id; //学籍番号
  5. int mth; //数学
  6. int eng; //英語
  7. int phy; //物理
  8. int avg; //平均点
  9. } Score;
  10.  
  11. void calcAverage(Score s[], int n);
  12. void showScore(Score s[], int n );
  13. void swap(Score *x, Score *y);
  14.  
  15. int main(void) {
  16. Score s[] = {
  17. { 1001, 65, 80, 95, 0},
  18. { 1002, 70, 68, 75, 0},
  19. { 1003, 60, 100, 83, 0},
  20. { 1004, 100, 55, 74, 0},
  21. { 1005, 90, 85, 100, 0},
  22. };
  23. int i,j, n=5;
  24.  
  25. calcAverage(s, n);
  26. showScore(s, n);
  27.  
  28. for(i=0;i<n; i++)
  29. for(j=n-1; j>i; j--)
  30. if( s[j-1].avg>s[j].avg )
  31. swap( &s[j-1], &s[j] );
  32.  
  33. printf( "\nAfter sorting\n" );
  34. showScore(s, n);
  35.  
  36. return 0;
  37. }
  38.  
  39. void calcAverage(Score s[], int n){
  40. int i;
  41.  
  42. for( i=0; i<n; i++ )
  43. s[i].avg = (s[i].mth + s[i].eng + s[i].phy)/3;
  44. }
  45.  
  46. void showScore(Score s[], int n ){
  47. int i;
  48. for(i=0; i<n; i++ )
  49. printf( "[%5d] %4d %4d %4d %4d\n", s[i].id, s[i].mth, s[i].eng, s[i].phy, s[i].avg );
  50. }
  51.  
  52. void swap( Score *x, Score *y ){
  53. Score tmp=*x;
  54. *x = *y;
  55. *y = tmp;
  56. }
  57.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
[ 1001]   65   80   95   80
[ 1002]   70   68   75   71
[ 1003]   60  100   83   81
[ 1004]  100   55   74   76
[ 1005]   90   85  100   91

After sorting
[ 1002]   70   68   75   71
[ 1004]  100   55   74   76
[ 1001]   65   80   95   80
[ 1003]   60  100   83   81
[ 1005]   90   85  100   91