home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-19 | 2.8 KB | 137 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"
-
- void DoSort (long*, char**);
- // Fill in your solution and then submit this folder
-
- // Team Name: Team Victory
-
- pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
- {
- short inref, outref;
- OSErr err;
- long chars [256];
- long size, i;
- Handle buf;
- char c;
-
- err = FSpOpenDF (infile, fsCurPerm, &inref);
- if (err != noErr)
- return err;
-
- err = GetEOF (inref, &size);
- if (err != noErr)
- return err;
-
- buf = NewHandle (size);
- HLock (buf);
- char *bufp = *buf;
-
- err = FSRead (inref, &size, bufp);
- if (err != noErr)
- return err;
-
- err = FSClose (inref);
- if (err != noErr)
- return err;
-
- for (i = 0; i < 256; i++)
- chars [i] = 0;
-
- for (i = 0; i < size; i++) {
- c = bufp [i];
- if ((c > 0x20) && (c < 0x7f))
- chars [c]++;
- };
-
- HUnlock (buf);
-
- DoSort (chars, buf);
-
- HLock (buf);
- bufp = *buf;
- size = GetHandleSize (buf);
-
- err = FSpDelete (outfile);
-
- err = FSpCreate (outfile, 'R*ch', 'TEXT', 0);
- // if (err != noErr)
- // return err;
-
- err = FSpOpenDF (outfile, fsWrPerm, &outref);
- if (err != noErr)
- return err;
-
- err = FSWrite (outref, &size, bufp);
- if (err != noErr)
- return err;
-
- err = FSClose (inref);
- return err;
- }
-
- void DoSort (long* chars, char** buf) {
- long max, maxi, i;
- char *bufp, *curp;
-
- HLock (buf);
-
- bufp = curp = *buf;
-
- do {
- max = 0;
- for (i = 0x21; i < 0x7f; i++)
- if (chars [i] > max) {
- max = chars[i];
- maxi = i;
- };
-
- if (max != 0) {
- for (i = 0; i < max; i++)
- *(curp++) = (char) maxi;
- chars [maxi] = 0;
- };
-
- } while (max != 0);
-
- HUnlock (buf);
-
- SetHandleSize (buf, (curp - bufp));
- };