#include <stdio.h>#include<string.h>typedefstruct{ Char*Mem; CharLen;} m_table_t;intTABLE_CMP (m_table_t *p1, m_table_t *p2) { intret; if(P1->len = = p2->len)returnMEMCMP (P1->mem, P2->mem, p1->Len); if(P1->len > p2->len) ret= memcmp (P1->mem, P2->mem, p2->Len); Elseret= memcmp (P1->mem, P2->mem, p1->Len); if(0==ret)returnP1->len-p2->Len; returnret;}voidSort (m_table_t *a,intnum) { inti,j; m_table_t temp; for(i=0; i<num;i++) { for(j=0; j<num;j++) { if(TABLE_CMP (&a[j], &a[j+1]) >0) {memcpy (&temp, &a[j],sizeof(m_table_t)); memcpy (&A[J], &a[j+1],sizeof(m_table_t)); memcpy (&a[j+1], &temp,sizeof(m_table_t)); } } }}voidQuickSort (m_table_t *s,intLintR) { intI, J; m_table_t x; if(L <r) {i= L; j =R; memcpy (&x, &s[i],sizeof(m_table_t)); while(I <j) { while(I < J && (TABLE_CMP (&s[j], &x) >0)) j--; if(I <j) {memcpy (&s[i++], &s[j],sizeof(m_table_t)); } while(I < J && (TABLE_CMP (&x, &s[i]) >0)) i++; if(I <j) {memcpy (&s[j--], &s[i],sizeof(m_table_t)); }} memcpy (&s[i], &x,sizeof(m_table_t)); QuickSort (S, l, I-1); QuickSort (S, I+1, R); }}intStr2table (m_table_t *table,Char*STR,int*index) { inti,w; *index =0; for(i=0, w=-1; I<strlen (str); i++) { if(' '! = * (str+i)) {if( (-1==W)) {W)= i;//Start } } Else { if(-1! = W)//Stop{table[*index].mem = str+W; table[*index].len = i-W; (*index) + +; W= -1; } } } if(-1!=W) {table[*index].mem = str+V; table[*index].len = i-W; (*index) + +; W= -1; } return 0;}voidDisplay (m_table_t *table,intindex) { inti; Chartmp[ the] = {0}; for(i=0; i<index; i++) {memset (TMP,0, the); memcpy (TMP, TABLE[I].MEM, Table[i].len); printf ("%s\n", TMP); } return;}intMainvoid ){ intindex; Charinput[ -] = {0}; m_table_t g_table[ the]= {0}; Gets (input); Str2table (&g_table[0], input, &index); QuickSort (&g_table[0],0, index-1);//sort (&g_table[0], index-1);Display (&g_table[0], index); return 0;}
HW Pen Question-02