#define__stdc_want_lib_ext1__ 1#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>#defineBuffer_len 5//Initialize input buffer length#defineBUFFER_LEN_INCR 3//buffer length Increase amount#defineCAPACITY_INCR 2//for the growth capacity of the proverb to be enteredintMainvoid){ Char**pproverbs=null;//pointer to proverb string Char**temp=null;//Temporary pointerssize_t *plengths =null;//the pointer of the proverb lengthsize_t *ptemplengths =null;//Temporary length pointer intCapacity =0;//the number of proverbs that can be stored intCount =0;//the number of Proverbs read Char*pstart =null;//Pointer to the beginning of the buffered input Char*pstr = NULL;//pointer to a string intBuffer_length =buffer_len;//Input buffer length Char*pbuffer = (Char*)malloc(Buffer_len);//Initialize memory allocations for buffers, note that this is a one-dimensional array if(!pbuffer) {printf ("bullshit\n"); Exit (1); } Pstart= Pbuffer;//the beginning of the storage buffer for(;;) { //1. Determine how many proverbs to save. //is the capacity fully stored? if(Count = =capacity) {Capacity+ = CAPACITY_INCR;//add two proverb volumes at a timetemp =realloc(pproverbs,capacity*sizeof(Char*));//re-open new memory to Pproverbs, specify char* type pointer, number is capacity if(!temp) {printf ("bullshit!\n"); Exit (1); } pproverbs=temp; Temp=NULL; Ptemplengths=realloc(plengths,capacity*sizeof(size_t));// if(!ptemplengths) {printf ("bullshit!\n"); Exit (1); } plengths=ptemplengths; Ptemplengths=NULL; } printf ("enter a proverb and press ENTER, or just press ENTER to end:\n"); //2. Determine the input of each proverb into the dynamically allocated memory//read the proverb of the device input while((*pbuffer++ = GetChar ())! ='\ n')//one-dimensional array element input { if(Pbuffer-pstart = =buffer_length) {Buffer_length+ = BUFFER_LEN_INCR;//Increase buffer lengthPSTR =realloc(Pstart,buffer_length);//re-allocating new buffered memory if(!pstr) {printf ("bullshit!\n"); Exit (1); } //The new memory contains the current proverb text, but can be a different address than the Pstart//We must reset the pbuffer pointer to the new memory of the same relevant address. //as it is in the old memory, Pstart should point to the new bufferPbuffer =pstr+ (Pbuffer-pstart);//the address of the next location in the new memoryPstart =pstr; Pstr=NULL; } } //3. If it is empty, exit. if((Pbuffer-pstart) <2) Break; //4. Store the current input good proverb string in a two-dimensional element. Why +1* (pbuffer-1) =' /'; Pbuffer=Pstart; * (plengths + count) = strnlen_s (pbuffer,buffer_length);//For example, you enter a, the length is 1.//printf ("%d\n", * (Plengths + count)); if(! (* (Pproverbs+count) = (Char*)malloc(* (Plengths+count) +1)))//but there's space to set aside the characters.{printf ("bullsht!\n"); Exit (1); } strcpy_s (* (Pproverbs+count), * (Plengths+count) +1, pbuffer); ++count; } size_t Length=0; for(size_t i =0; i<count-2;++i) { for(size_t j = i+1; j<count-1; j + +) { if(* (Plengths+i) >* (plengths+j)) {Pstr= * (pproverbs+i); * (pproverbs+i) = * (pproverbs+j); * (PPROVERBS+J) =pstr; Length= * (plengths+i); * (plengths+i) = * (plengths+j); * (plengths +j) =length; }}} printf ("\nall:\n"); for(size_t i =0; i<count;++i) {printf ("%s\n", * (pproverbs+i)); Free(* (pproverbs+i)); * (pproverbs+i) =NULL; } Free(Pproverbs); Free(Pstart); Free(plengths);}
7.82 hands to practice, enter any proverb, put them into memory allocated in the execution period, and then sort from short to long