//mystu.h --- @CopyLeft by tsaiwn@cs.nctu.edu.tw // Header file for using msort( ), useMgSort( ) // also declare qqsort( ), qsort2( ) in this Header file #ifndef _MYSTU_H_ #define _MYSTU_H_ // 以上這兩列 #ifndef 與 #define 以及最後的#endif 使得這檔案萬一.. // .. 萬一不小心被 #include 多次也沒關係! /// void msort(void*x, int n,int sz, int(*comp)(const void*, const void*)); // 上面這 ( ) 的參數與用法跟 C Library qsort( ) 完全相同 /// 以下這兩個宣告對應到我們自己寫的 qqsort( ) 和 qsort2( ) void qqsort(void*x, int n, int size, int (*cmp)(const void*, const void*)); void qsort2(void*x, int n, int size, int (*cmp)(const void*, const void*) ); // 以下是之前 hwk12qsort 抄來, 但再定義一個沒有 _t 的 :-) typedef struct { long sid; int s1, s2, s3, s4; // struct 內各項目順序不重要 double total; // 學期總成績= (h*0.2 + m*0.3 + q*0.1+f*0.4); /// 這裡的 s1, s2, s3, s4 就是 h, m, q, f long rank, recno; // rank, original record number char name[9]; // student's name } Student_t; // 這種命名方式是不錯的習慣 ( _t) typedef Student_t Student; // 這樣, 使得寫 Student 也可以:-) /// void useMgSort(Student*, int, int(*comp)(Student*,int,int)); /// 注意此 sorting function 接受的 comp 比較函數與 qsort 的不同! /// qsort 的 compare function 是接受兩個 pointer 指標 /// 我們這個須接受三個參數: /// 一個 Student array (即Student*)和兩個整數 i, k; /// 該兩個整數指出要比的哪兩個元素之 index(subscript),不是pointer ! /// Usage of useMgSort: /// useMgSort( x, n, myComp); /// where x is a Student array, n is number of elements in x; /// and myComp(int i, int k); is a compare function that /// whill compare i-th element with k-th element /// the compare function myComp( ) should return 1, 0, -1 /// to indicat that x[i] >, ==, < x[k] /// And thus, x[ ] will be sorted in Ascending order.(遞增序) #endif