home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 01 - Johns / Solution.c next >
Encoding:
C/C++ Source or Header  |  1998-06-19  |  2.6 KB  |  124 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.  
  21. Output specification
  22.  
  23. The outfile must be created and then filled with the sorted characters.  It's
  24. final length should be exactly the same as the count of characters in the
  25. allowed range (0x20<c<0x7f) (which may be shorter than the infile file length).
  26.  
  27. Sample input
  28.  
  29. abcdefghabbcccdddeee
  30. or
  31. 012345678911234567892123456789312345678941234567895123456789612345678971234567891
  32.  
  33. Sample output
  34.  
  35. ccccddddeeeebbbaafgh
  36. or
  37. 111111111122222222233333333344444444455555555566666666677777777788888888999999990
  38. */
  39.  
  40. #include "Solution.h"
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. // Fill in your solution and then submit this folder
  47.  
  48. // Team Name: Mark Johns & Charlie M-T
  49.  
  50. typedef struct
  51. {
  52.     char value;
  53.     long num;
  54. //    long index;
  55. } La;
  56.  
  57. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
  58. {
  59.     char *source;
  60.     short f1, f2;
  61.     short numDat = 0;
  62.     long length, size, i, j, k;
  63.     La *dat, swap;
  64.  
  65.     if(FSpOpenDF(infile, fsRdWrPerm, &f1)!=noErr)
  66.         ExitToShell();
  67.     
  68.     FSpCreate(outfile, '????', '????', smSystemScript);
  69.     if(FSpOpenDF(outfile, fsRdWrPerm, &f2)!=noErr)
  70.         ExitToShell();
  71.         
  72.     GetEOF(f1, &length);
  73. //    SetEOF(f2, length);
  74.     source = malloc(length);
  75.     for(i = 0; i < length; i++)
  76.         source[i] = 0;
  77.     FSRead(f1, &length, source);
  78.     
  79.     printf("%*s\n", length, source);
  80.     dat = malloc(0);
  81.     for(i=0;i<length;i++)
  82.     {
  83.         for(k = 0; k < numDat; k++)
  84.         {
  85.             if(dat[k].value == source[i])
  86.             {
  87.                 dat[k].num++;
  88.                 break;
  89.             }
  90.         }
  91.         if(k == numDat)
  92.         {
  93.             numDat++;
  94.             dat = realloc(dat, sizeof(La) * numDat);
  95.             dat[numDat - 1].value = source[i];
  96.             dat[numDat - 1].num = 1;
  97.         }
  98.     }
  99.     for(i = 0; i < numDat; i++)
  100.     {
  101.         for(j = 0; j < numDat; j++)
  102.         {
  103.             if(dat[i].num > dat[j].num)
  104.             {
  105.                 swap = dat[i];
  106.                 dat[i] = dat[j];
  107.                 dat[j] = swap;
  108.             }
  109.         }
  110.     }
  111.     size = sizeof(char);
  112.     for(i = 0; i < numDat; i++)
  113.     {
  114.         for(j = 0; j < dat[i].num; j++)
  115.         {
  116.             putchar(dat[i].value);
  117.             FSWrite(f2, &size, &dat[i].value);
  118.         }
  119.     }
  120.     putchar('\n');
  121.     free(source);
  122.     free(dat);
  123.     return noErr;
  124. }