內容

定義 資料結構... 1

定義 資料... 1

定義  資料結構 能成員函數... 1

void computing(score* mem_data) 1

void display( score* mem_data) 2

void get_key( int k[], int index[],int N, int sel ) 2

void display_all( int index[]) 3

定義  資料結構 之測用訊號... 3

int main(int argc, char *argv[]) 4

 

#include <stdio.h>

#include <stdlib.h>

// 定義 資料結構元素

struct _score

{

       char name[20];

       unsigned int id;

       unsigned int chn, eng, math, comp, total, avg;

};

typedef struct _score score;

返回首頁

// 定義 資料結構

// 引用以前講義之資料結構範例

#define max_no 100

score data[max_no];

// 定義 資料結構 之鍵值與索引值空間

int key[max_no];

int index1[max_no],index2[max_no];

返回首頁

// 定義  資料結構 功能成員函數

void computing(score* mem_data)

{

     mem_data->total=mem_data->chn+mem_data->eng+mem_data->math+mem_data->comp;

     mem_data->avg=mem_data->total/4;

}

返回

 

// 定義  資料結構 功能成員函數

void display( score* mem_data)

{

      printf("name = %s\n", mem_data->name);

      printf("id = %d \n", mem_data->id);

      printf("國文成績 = %d \n", mem_data->chn);

      printf("英文成績 = %d \n", mem_data->eng);

      printf("數學成績 = %d \n", mem_data->math);

      printf("計算機成績 = %d \n", mem_data->comp);

      printf("總分 = %d.   平均= %d\n", mem_data->total, mem_data->avg);    

}

返回

// 定義  資料結構 功能成員函數, 取得所需之排序鍵值

void get_key( int k[], int index[],int N, int sel )

{

     int i;

     k[0]=N;

     index[0]=N;

     for( i=1;i<=N; i++)

     {

          index[i]=i-1;

          switch (sel)

          {

          case 0:            // 設定以id為鍵值

               k[i]=data[i-1].id;

               break;

          case 1:           // 設定以國文為鍵值

               k[i]=data[i-1].chn;              

               break;

          case 2:           // 設定以英文為鍵值

               k[i]=data[i-1].eng;

               break;

          case 3:           // 設定以數學為鍵值

               k[i]=data[i-1].math;

               break;

          case 4:           // 設定以計算機為鍵值

               k[i]=data[i-1].comp;              

               break;

          case 5:           // 設定以總分為鍵值

               k[i]=data[i-1].total;

               break;

          case 6:           // 設定以平均成績為鍵值

               k[i]=data[i-1].avg;              

               break;

          default:           // 設定以順序為鍵值

               k[i]=i-1;

                  break;

          }

     }

 }

返回

 

// 定義  資料結構 功能成員函數

void display_all( int index[])

{

int i,j;

      printf("%10s %4s %4s %4s %4s %4s %4s %s\n",

        "name","id","國文","英文","數學","電腦","總分","平均" );

      for(j=1;j<=index[0];j++)

      {

      i=index[j];

      printf("%10s %4d %4d %4d %4d %4d %4d %4d\n",

      data[i].name, data[i].id, data[i].chn,

      data[i].eng, data[i].math, data[i].comp,

      data[i].total, data[i].avg);    

      }

}

返回

 

// 定義  資料結構 之測試用訊號

// 為了要測試排序, 寫出之隨機資料產生程式,  可產生N比元素資料

void init_data(int N)

{

     int A[5];

     char str[10];

     int i;

     StartRAND();   

     for( i=0;i<N;i++)

     {

         RandomNum(A,5,0,100);

         RandomChar(str,9,'A','Z');

         str[9]=0;

          strcpy( data[i].name,str);

          data[i].id=i+1;

          data[i].chn=A[1];

          data[i].eng=A[2];

          data[i].math=A[3];

          data[i].comp=A[4];

          computing( &data[i]);

      }

}

返回

 

int main(int argc, char *argv[])

{

  int Num=10;

  init_data(Num);

     int i;

     for( i=0;i<Num;i++)

     {

          display( &data[i] );

       }

// 設定 以國文成績為鍵值      

     get_key( key, index1, Num, 1);

// 排序    

    BubSort( key, index1);

    display_all( index1);

 

// 設定 以總分成績為鍵值

   get_key( key, index2, Num, 5);    // 可使用其他資料為鍵值

   BubSort( key, index2); 

   display_all( index2);

/*            可選擇不同之排序資料測試

   SelSort( key, index2);

   display_all( index2);

   InSort( key, index2);

   display_all( index2);

   QuickSort( key, index2); 

   display_all( index2);

   HeapSort( key, index2); 

   display_all( index2);

   ShellSort( key, index2);

   display_all( index2);

*/

  system("PAUSE");   

  return 0;

}

返回首頁