Time:06-02
CodePudding user response:
there is no demand? Just want to code? If so, it is suggested that the baidu, find for prime Numbers, and so on, for example,
?//////////////////////////////////////////////////////////////////////////file name: void_pointer c//author: cfjtaishan //date: 2013-03-21 //description: the use of generic function pointer. //description: //1. Implements integer array to maximum element and an array of strings summary for the biggest string; //2. The implementation of the integer array and bubble sort an array of strings; ////////////////////////////////////////////////////////////////////////#include #include #include #include //this two macros used to compile switch # define INTEGE # define STRING # define LEN 10 # define DAYS 7 //declare a generic function pointer Typedef int (* pfunc) (const void *, const void *). Fswap typedef void (*) (void *, void *). //function declaration Int compare_intege (const void * a, const void * b); Int compare_string (const void * a, const void * b); Void swap_for_intege (void * p, void * q); Void swap_for_string (void * p, void * q); Void * my_max (void * pbufs, const int len, const int size, pfunc CMP); Void bubble_sort (void * pbufs, const int len, const int size, pfunc CMP, fswap swap); Void initize_arr (int * arr, const int len); Void show_arr (const int * arr, const int len); Int main (int arg c, char * argv []) {# ifdef INTEGE Int arr (LEN), * Max; Initize_arr (arr, LEN); Printf (" Show the arr the arguments: \ n "); Show_arr (arr, LEN); Max=(int *) my_max (arr, LEN, sizeof (int), compare_intege); Printf (" Max integer is % d \ n ", * Max); Bubble_sort (arr, LEN, sizeof (int), compare_intege, swap_for_intege); Printf (" After sort: \ n "); Show_arr (arr, LEN); # endif # ifdef STRING int i; Char * week [only]={" Monday ", "Tuesday, Wednesday, Thursday," Friday ", "Saturday", "Sunday"}; Char * * max_str=(char * *) my_max (week DAYS, 4, compare_string);//pointer length is 4 Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n "); Printf (" Max string is "); Puts (* max_str); Bubble_sort (week DAYS, 4, compare_string, swap_for_string);//pointer length is 4 Printf (" After mercifully sort: \ n "); For (I=0; i {Printf (" % s ", week [I]); } Putchar (" \ n "); # endif return 0; } ////////////////////////////////////////////////////////////////////////////the function name: initize_arr //description: initialize array, //parameters: ///in/out arr - integer array first address; //[in] len - the largest integer array length; //the return value: no, //description: //////////////////////////////////////////////////////////////////////////Void initize_arr (int * arr, const int len) {int i; Srand (time (NULL)); For (I=0; i {Arr [I]=rand () % 1000;//generate 1000 random Numbers within the } } ////////////////////////////////////////////////////////////////////////////the function name: show_arr //description: print the array element value, //parameters: //[in] arr - integer array first address; //[in] len - the largest integer array length; //the return value: no, //description: //////////////////////////////////////////////////////////////////////////Void show_arr (const int * arr, const int len) {int i; For (I=0; i {Printf (" % d ", arr [I]); } Putchar (" \ n "); } ////////////////////////////////////////////////////////////////////////////the function name: compare_intege //description: compare two integer size, //parameters: //[in] a - an integer data address; //[in] b - another integer data address; //the return value: If//* a & gt; * b, return the positive integers; //* a * b==if it returns 0; If//* a & lt; * b, it returns a negative positive integers; //description: //////////////////////////////////////////////////////////////////////////Int compare_intege (const void * a, const void * b) {Return * (int *) a - b * (int *); } ////////////////////////////////////////////////////////////////////////////the function name: compare_string //description: compare two strings, the size of the //parameters: //[in] a - the address of a string; //[in] - another string b address; //the return value: //and return the same STRCMP. //description: //////////////////////////////////////////////////////////////////////////Int compare_string (const void * a, const void * b) {Return STRCMP (*) (char * * a, * (char * *), b); } ////////////////////////////////////////////////////////////////////////////the function name: my_max //description: for a maximum of the data set, //parameters: ///in/out pbufs - generic data first address; //[in] len - the maximum length of the generic data; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Page link:https//www.codepudding.com/Backend/112524.html