home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-19 | 3.2 KB | 131 lines | [TEXT/CWIE] |
- /*
- Problem 01 - Mode Sort
-
- This problem is to sort an input string of N characters, N<1000000, based on
- the number of times a character occurs in the input. The most frequently
- occurring character should be sorted to the front of the string, followed by
- the next most frequently occurring character, etc. For characters occurring
- the same number of times, the character that occurs first in the input should
- be sorted to the front.
-
- Header specification
-
- pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile );
-
- Input specification
-
- The infile input file contains the characters. Input characters other than
- those printable low ascii characters c, 0x20<c<0x7f, must be ignored.
-
- Output specification
-
- The outfile must be created and then filled with the sorted characters. It's
- final length should be exactly the same as the count of characters in the
- allowed range (0x20<c<0x7f) (which may be shorter than the infile file length).
-
- Sample input
-
- abcdefghabbcccdddeee
- or
- 012345678911234567892123456789312345678941234567895123456789612345678971234567891
-
- Sample output
-
- ccccddddeeeebbbaafgh
- or
- 111111111122222222233333333344444444455555555566666666677777777788888888999999990
- */
-
- #include "Solution.h"
-
- // Fill in your solution and then submit this folder
-
- // Team Name: Team Keith!
-
- #include <stdio.h>
- #include <string.h>
-
- #define CLEARBLOCK(x) { memset( & x, 0, sizeof(x) ); }
-
- static OSErr FSWriteCountedChars ( short refNum, unsigned long count, char c )
- { OSErr err = noErr;
-
- while ( err == noErr && count -- > 0 )
- { long temp = sizeof( c );
-
- err = FSWrite ( refNum, & temp, & c );
- }
-
- return err;
- }
-
- pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
- { OSErr err;
- short inRefNum, outRefNum;
-
- err = FSpOpenDF ( infile, fsRdPerm, & inRefNum );
- if ( err == noErr )
- { unsigned long fileCounts[ 256 ];
- long fileFirstOffset [ 256 ];
-
- // Ignore error on create.
- err = FSpCreate ( outfile, 'xxxx', 'TEXT', smRoman );
-
- err = FSpOpenDF ( outfile, fsRdWrPerm, & outRefNum );
- if ( err == noErr )
- {
- long offset = 0;
-
- CLEARBLOCK ( fileCounts );
- CLEARBLOCK ( fileFirstOffset );
-
- for ( offset = 0; err == noErr; offset ++ )
- { char c;
- long count = 1;
-
- err = FSRead ( inRefNum, & count, & c );
- if ( err == noErr )
- {
- fileCounts[ c ] ++;
-
- if ( fileFirstOffset[c] == 0 )
- fileFirstOffset[c] = offset;
- }
- }
-
- if ( err == eofErr )
- { short i;
- char nextHighest;
-
- err = noErr;
- fileCounts[0] = 0;
- fileFirstOffset[0] = 0;
-
- while ( err == noErr )
- {
- nextHighest = 0;
-
- for ( i = 33; i < 0x7f; i ++ )
- if ( fileCounts [ i ] > 0 )
- {
- if ( ( fileCounts[i] > fileCounts[ nextHighest ] ) ||
- ( fileCounts[i] == fileCounts[ nextHighest ] && ( fileFirstOffset[i] < fileFirstOffset[ nextHighest ] ) ) )
- nextHighest = i;
- }
-
- if ( nextHighest )
- {
- printf ( "Char %c count %d firstOffset=%d\n", nextHighest, fileCounts[ nextHighest ], fileFirstOffset [ nextHighest ] );
-
- err = FSWriteCountedChars ( outRefNum, fileCounts[ nextHighest ], nextHighest );
- fileCounts[ nextHighest ] = 0;
- }
- else
- break;
- }
- }
- }
- }
-
- return err;
- }