// File: qtemplate.h // // Header file for template to get qsort for different types // // The DATA class must support <, > and << #ifndef _qtempate_h #define _qtemplate_h //==================================================================== // Template prototypes template void PrintArray(DATA [] , int) ; template int CheckArray(DATA [], int) ; template void qsort(DATA [], int) ; template int compare (const DATA *, const DATA *) ; //==================================================================== // Template prototypes template int compare(const DATA *ptr1, const DATA *ptr2) { if (*ptr1 < *ptr2) return -1 ; if (*ptr1 > *ptr2) return 1 ; return 0 ; } template void PrintArray(DATA A[], int n) { int i ; for (i = 0 ; i < n ; i++) { cout.width(14) ; // Output at least 14 chars wide cout << A[i] << " " ; // does not depend on DATA if (i % 5 == 4) cout << "\n" ; } if (n % 5 != 0) cout << "\n" ; // Print one more CR ? } template int CheckArray(DATA A[], int n) { int i ; for (i = 0 ; i < n-1 ; i++) { if (A[i] > A[i+1]) return 0 ; } return 1 ; } template void qsort(DATA A[], int n) { // variable compare_func stores address to the correct compare function // This is needed to distinguish between the many compare functions int (*compare_func) (const DATA *, const DATA *) ; compare_func = &compare ; qsort(A, n, sizeof(DATA), compare_func) ; } #endif