home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / cp1 / struc2.c < prev    next >
C/C++ Source or Header  |  1993-06-10  |  3KB  |  87 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-06-93 (21:18)             Number: 199
  4. From: DAVID JOHNSON                Refer#: 177
  5.   To: GEOFFREY LIU                  Recvd: NO  
  6. Subj: Problems with qsort            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. GL>        I hope someone here can point out what I'm doing wrong. I'm
  9. GL>trying to sort a structure with one of the elements as the "item" being
  10. GL>sorted. Here's the code:
  11.  
  12. You can't do it the way you want.. as you have to swap the elements of the struc
  13. ture.  If you sort on the NAME only then the ZIP will be out of place.
  14.  
  15. here is another version
  16.  
  17. /*Author: David Johnson */
  18. /*Copyright: Released to the Public Domain */
  19.  
  20. /* Structure Sorting routine example */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. struct address {
  26.        int zip;
  27.        char name[4];
  28.        int nothing;
  29. };
  30. /*   Prototypes */
  31. struct address list[5]= { 0,"cat",0,1, "car",1, 2,"cab",2, 3,"cap",3
  32.                         , 4,"can",4 };
  33. void qs_struct(struct address item, int left, int right); void quick_struct(stru
  34. ct address item[],int count); void swap_all_fields(struct address item[],int i,i
  35. nt j);
  36.  
  37.  
  38. main()
  39. {
  40. int counter=0;
  41. quick_struct(&list[0],5);
  42. for (counter=0;counter<4;counter++) printf("%s\n",list[counter].name); return 0;
  43.  }
  44.  
  45.  
  46. void quick_struct( struct address item[],int count)  /* setup */ { qs_struct(ite
  47. m[0],0,count-1); }
  48.  
  49. void qs_struct( struct address address, int left, int right) { register int i,j,
  50. l; char *x, *y; i=left; j=right; x=list[(left+right)/2].name; do
  51.   {
  52.   while(strcmp(list[i].name,x)<0 && i<right) i++;
  53.   while(strcmp(list[j].name,x)>0 && j>left) j--;
  54.   if (i<=j) {
  55.      swap_all_fields( list,i,j);
  56.      i++;j--;
  57.      }
  58.   } while(i<=j);
  59.  
  60. if (left<j) qs_struct(address,left,j);
  61. if (i<right) qs_struct(address,i,right); }
  62.  
  63. void swap_all_fields(struct address list[],int i,int j) { struct address swap; s
  64. wap.zip=list[i].zip; strcpy(swap.name,list[i].name); swap.nothing=list[i].nothin
  65. g;
  66.  
  67. list[i].zip=list[j].zip;
  68. strcpy(list[i].name,list[j].name);
  69. list[i].nothing=list[j].nothing;
  70.  
  71. list[j].zip=swap.zip;
  72. strcpy(list[j].name,swap.name);
  73. list[j].nothing=swap.nothing;
  74.  
  75. }
  76.  
  77. ===
  78.  * TLX v2.00 * Every time I make my mark, someone paints the wall...
  79.  
  80.  * DeLuxe2/386 1.25 #483 *
  81.  
  82. --- DB 1.53/002503
  83.  * Origin: Programmer's Guild BBS - Hamilton, Ont. (416)525-7616 (1:244/117)
  84. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  85. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  86. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  87.