1. /*-------------------------------------------------------
    2. 【程序设计】
    3. ---------------------------------------------------------
    4. 题目:学生的记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组s中,
    5. 请编写函数fun,其功能是:按分数降序排列学生的记录,高分在前,低分在后。
    6. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入
    7. 你编写的若干语句。
    8. -------------------------------------------------------*/
    9. #include <stdio.h>
    10. #define N 16
    11. void wwjt ( );
    12. typedef struct
    13. {
    14. char num[10];
    15. int s;
    16. } STREC;
    17. void fun( STREC a[] )
    18. {
    19. /**********Program**********/
    20. int i,j;
    21. STREC t;
    22. for(i=1;i<N;i++)
    23. for(j=0;j<N-1;j++)
    24. if(a[j].s<a[j+1].s){
    25. t = a[j];
    26. a[j]=a[j+1];
    27. a[j+1]=t;
    28. }
    29. /********** End **********/
    30. }
    31. void main()
    32. {
    33. STREC s[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},
    34. {"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},
    35. {"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},
    36. {"GA011",66},{"GA017",64},{"GA018",64},{"GA016",72}};
    37. int i;
    38. fun( s );
    39. printf("The data after sorted :\n");
    40. for(i=0;i<N; i++)
    41. {
    42. if( (i)%4==0 )printf("\n");
    43. printf("%s %4d ",s[i].num,s[i].s);
    44. }
    45. printf("\n");
    46. wwjt( );
    47. }
    48. void wwjt( )
    49. {
    50. FILE *in, *out ;
    51. STREC s[N];
    52. int i,j;
    53. in=fopen("in.dat","r");
    54. if(in==NULL)
    55. {
    56. printf("Please Verify The Currernt Dir..It May Be Changed");
    57. }
    58. out=fopen("out.dat","w");
    59. if(out==NULL)
    60. {
    61. printf("Please Verify The Current Dir.. It May Be Changed");
    62. }
    63. for(j=0;j<N; j++)
    64. {
    65. fscanf(in,"%s %d",&s[j].num,&s[j].s);
    66. }
    67. fun(s);
    68. for(i=0;i<N; i++)
    69. {
    70. if( (i)%4==0 && i)
    71. fprintf(out, "\n");
    72. fprintf(out, "%4d ",s[i].s);
    73. }
    74. fprintf(out,"\n");
    75. fclose(out) ;
    76. }