C語言程式設計 第七章 指標與結構體 指標數組 例題

來源:互聯網
上載者:User
/* ---------------------------------------
   例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();
}
 /* --------------------------------------*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.