C语言程序设计 第七章 指针与结构体 指针数组 例题

/* ---------------------------------------
   例7.19
   输入N个学生学号,姓名,成绩,并按成绩降序排列,并输出
   p指向结构体变量s1 , 则  s1.成员名,(*p).成员名,p->成员名 等价。
   本题采用自定义函数较为合适
  Author: emanlee  https://www.cnblogs.com/emanlee/
  Date: 2008-05-12
  --------------------------------------- */
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include <string.h>
#define N 2

struct student
{
   int num;
   char name[12];
   int score;
};


main( )
{
   struct student s[N];
   struct student *p,*q[N];
   int i,j,k;
   printf (" Input %d student's num  name  score\n",N);
   p=s;
   for (i=0;i<N;i++) /* 输入 */
   {
       scanf("%d%s%d",&p->num,p->name,&p->score);
       q[i]=p++;
   }
   for (i=0;i<N-1;i++)   /*选择排序*/
   {
       k=i;
       for (j=i+1;j<N;j++)
          if (q[k]->score<q[j]->score)
             k=j;
       if (k!=i)
       {
          p=q[i];
          q[i]=q[k];
          q[k]=p;
       }
   }
   printf("Number  Name       Score\n");    /*输出 */
   for (i=0; i<N; i++)
      printf("%-8d%-12s%-d\n",q[i]->num,q[i]->name,q[i]->score);
   getchar();     getchar();
}
 /* --------------------------------------*/


posted @ 2008-05-12 08:53  emanlee  阅读(2412)  评论(0编辑  收藏  举报