home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 01 - Keith / Solution.cp < prev    next >
Encoding:
Text File  |  1998-06-19  |  3.2 KB  |  131 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. // Fill in your solution and then submit this folder
  42.  
  43. // Team Name: Team Keith!
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47.  
  48. #define    CLEARBLOCK(x)    { memset( & x, 0, sizeof(x) ); }
  49.  
  50. static OSErr FSWriteCountedChars ( short refNum, unsigned long count, char c )
  51. {    OSErr err = noErr;
  52.  
  53.     while ( err == noErr && count -- > 0 )
  54.     {    long temp = sizeof( c );
  55.     
  56.         err = FSWrite ( refNum, & temp, & c );
  57.     }
  58.     
  59.     return err;
  60. }
  61.  
  62. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
  63. {    OSErr err;
  64.     short inRefNum, outRefNum;
  65.     
  66.     err = FSpOpenDF ( infile, fsRdPerm, & inRefNum );
  67.     if ( err == noErr )
  68.     {    unsigned long        fileCounts[ 256 ];
  69.         long        fileFirstOffset [ 256 ];
  70.  
  71.         //    Ignore error on create.        
  72.         err = FSpCreate ( outfile, 'xxxx', 'TEXT', smRoman );
  73.         
  74.         err = FSpOpenDF ( outfile, fsRdWrPerm, & outRefNum );
  75.         if ( err == noErr )
  76.         {    
  77.             long        offset = 0;
  78.             
  79.             CLEARBLOCK ( fileCounts );
  80.             CLEARBLOCK ( fileFirstOffset );
  81.             
  82.             for ( offset = 0; err == noErr; offset ++ )
  83.             {    char c;
  84.                 long count = 1;
  85.                 
  86.                 err = FSRead ( inRefNum, & count, & c );
  87.                 if ( err == noErr )
  88.                 {
  89.                     fileCounts[ c ] ++;
  90.  
  91.                     if ( fileFirstOffset[c] == 0 )
  92.                         fileFirstOffset[c] = offset;
  93.                 }
  94.             }
  95.             
  96.             if ( err == eofErr )
  97.             {    short i;
  98.                 char nextHighest;
  99.                 
  100.                 err = noErr;
  101.                 fileCounts[0] = 0;
  102.                 fileFirstOffset[0] = 0;
  103.                 
  104.                 while ( err == noErr )
  105.                 {
  106.                     nextHighest = 0;
  107.                     
  108.                     for ( i = 33; i < 0x7f; i ++ )
  109.                         if ( fileCounts [ i ] > 0 )
  110.                         {
  111.                             if ( ( fileCounts[i] > fileCounts[ nextHighest ] ) ||
  112.                                     ( fileCounts[i] == fileCounts[ nextHighest ] && ( fileFirstOffset[i] < fileFirstOffset[ nextHighest ] ) ) )
  113.                                     nextHighest = i;
  114.                         }
  115.                     
  116.                     if ( nextHighest )
  117.                     {
  118.                         printf ( "Char %c count %d firstOffset=%d\n", nextHighest, fileCounts[ nextHighest ], fileFirstOffset [ nextHighest ] );
  119.                         
  120.                         err = FSWriteCountedChars ( outRefNum, fileCounts[ nextHighest ], nextHighest );
  121.                         fileCounts[ nextHighest ] = 0;
  122.                     }
  123.                     else
  124.                         break;
  125.                 }
  126.             }
  127.         }
  128.     }
  129.     
  130.     return err;
  131. }