home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 01 - Team Victory / Solution.cp < prev    next >
Encoding:
Text File  |  1998-06-19  |  2.8 KB  |  137 lines  |  [TEXT/CWIE]

  1. /*
  2. Problem 01 - Mode Sort
  3.  
  4. This problem is to sort an input string of N characters, N<1000000, based on
  5. the number of times a character occurs in the input.  The most frequently
  6. occurring character should be sorted to the front of the string, followed by
  7. the next most frequently occurring character, etc.  For characters occurring
  8. the same number of times, the character that occurs first in the input should
  9. be sorted to the front.
  10.  
  11. Header specification
  12.  
  13. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile );
  14.  
  15. Input specification
  16.  
  17. The infile input file contains the characters.  Input characters other than
  18. those printable low ascii characters c, 0x20<c<0x7f, must be ignored.
  19.  
  20. Output specification
  21.  
  22. The outfile must be created and then filled with the sorted characters.  It's
  23. final length should be exactly the same as the count of characters in the
  24. allowed range (0x20<c<0x7f) (which may be shorter than the infile file length).
  25.  
  26. Sample input
  27.  
  28. abcdefghabbcccdddeee
  29. or
  30. 012345678911234567892123456789312345678941234567895123456789612345678971234567891
  31.  
  32. Sample output
  33.  
  34. ccccddddeeeebbbaafgh
  35. or
  36. 111111111122222222233333333344444444455555555566666666677777777788888888999999990
  37. */
  38.  
  39. #include "Solution.h"
  40.  
  41. void DoSort (long*, char**);
  42. // Fill in your solution and then submit this folder
  43.  
  44. // Team Name: Team Victory
  45.  
  46. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
  47. {
  48.     short    inref, outref;
  49.     OSErr    err;
  50.     long    chars [256];
  51.     long    size, i;
  52.     Handle    buf;
  53.     char    c;
  54.     
  55.     err = FSpOpenDF (infile, fsCurPerm, &inref);
  56.     if (err != noErr)
  57.         return err;
  58.     
  59.     err = GetEOF (inref, &size);
  60.     if (err != noErr)
  61.         return err;
  62.     
  63.     buf = NewHandle (size);
  64.     HLock (buf);
  65.     char    *bufp = *buf;
  66.     
  67.     err = FSRead (inref, &size, bufp);
  68.     if (err != noErr)
  69.         return err;
  70.     
  71.     err = FSClose (inref);
  72.     if (err != noErr)
  73.         return err;
  74.     
  75.     for (i = 0; i < 256; i++)
  76.         chars [i] = 0;
  77.  
  78.     for (i = 0; i < size; i++) {
  79.         c = bufp [i];
  80.         if ((c > 0x20) && (c < 0x7f))
  81.             chars [c]++;
  82.     };
  83.     
  84.     HUnlock (buf);
  85.     
  86.     DoSort (chars, buf);
  87.  
  88.     HLock (buf);
  89.     bufp = *buf;
  90.     size = GetHandleSize (buf);
  91.         
  92.     err = FSpDelete (outfile);
  93.     
  94.     err = FSpCreate (outfile, 'R*ch', 'TEXT', 0);
  95. //    if (err != noErr)
  96. //        return err;
  97.     
  98.     err = FSpOpenDF (outfile, fsWrPerm, &outref);
  99.     if (err != noErr)
  100.         return err;    
  101.     
  102.     err = FSWrite (outref, &size, bufp);
  103.     if (err != noErr)
  104.         return err;    
  105.     
  106.     err = FSClose (inref);
  107.     return err;
  108. }
  109.  
  110. void DoSort (long* chars, char** buf) {
  111.     long    max, maxi, i;
  112.     char    *bufp, *curp;
  113.     
  114.     HLock (buf);
  115.     
  116.     bufp = curp = *buf;
  117.     
  118.     do {
  119.         max = 0;
  120.         for (i = 0x21; i < 0x7f; i++)
  121.             if (chars [i] > max) {
  122.                 max = chars[i];
  123.                 maxi = i;
  124.             };
  125.         
  126.         if (max != 0) {
  127.             for (i = 0; i < max; i++)
  128.                 *(curp++) = (char) maxi;
  129.             chars [maxi] = 0;
  130.         };
  131.                 
  132.     } while (max != 0);
  133.     
  134.     HUnlock (buf);
  135.     
  136.     SetHandleSize (buf, (curp - bufp));
  137. };