1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <stdio.h> #include <stdlib.h> #include <math.h> // 数学库 void bye(); void print_array(const int array[] , int size); int my_compar(const void * p1, const void * p2); int main(int argc, const char * argv[]) { atexit(bye); double angle = 30.0 * 3.14/180; double x = sin(angle); printf("sin(%f) = %0.2f \n",angle,x); int array[3] = {4,2,3}; print_array(array,3); int (* fp)(const void *, const void *) = my_compar; qsort(array, 3, sizeof(int),fp); print_array(array,3); return 0; } void bye() { printf("bye \n"); } int my_compar(const void * p1, const void * p2) { const int * p3 = (const int *)p1; const int * p4 = (const int *)p2; if(*p3 > *p4)return 1; else if(*p3 < *p4)return -1; else return 0; } void print_array(const int array[] , int size) { for(int i = 0 ; i < size ; i++) { printf("%d",array[i]); } printf("\n"); }
|