/* ---------------------------------------
例7.19
輸入N個學生學號,姓名,成績,並按成績降序排列,並輸出
p指向結構體變數s1 , 則 s1.成員名,(*p).成員名,p->成員名 等價。
本題採用自訂函數較為合適
Author: emanlee, eman_lee@hotmail.com
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();
}
/* --------------------------------------*/