home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / games / wordy / unscr.c < prev    next >
C/C++ Source or Header  |  1994-11-04  |  5KB  |  194 lines

  1. /**************************************************************************/
  2. /*                           UNSCRAMBLE Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".wds"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34.  
  35. char ad[] =
  36. "UNSCRAMBLE utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  37.  
  38.  
  39.  
  40. void getword( char *letter_set, size_t w_len );
  41. void center( char *strng );
  42.  
  43. typedef enum { FALSE, TRUE } Boolean;
  44.  
  45. void main( int argc, char **argv )
  46. {
  47.  
  48.    char letterset[ MAXLEN ];
  49.  
  50.      if( argc == NOARGS )
  51.         {
  52.         clrscr();
  53.         puts( "Enter a LETTERSET to test ... " );
  54.         gets( letterset );
  55.         }
  56.      else
  57.         {
  58.         strcpy( letterset, *( argv + 1 ) );
  59.         getword( letterset, strlen( *( argv + 1 ) ) );
  60.         }
  61. }
  62.  
  63.  
  64.  
  65. Boolean wordtest( char *letterset, char *word )
  66. {
  67.     Boolean error_flag = TRUE;
  68.     static char dup_lset[ MAXLEN ];
  69.     register char *letpos;
  70.  
  71. //     dup_lset = strdup( letterset );
  72.      strcpy( dup_lset, letterset );
  73.          
  74.         while( *word )
  75.             {
  76.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  77.                 *letpos = '*';     //As long as letter contained...
  78.             else
  79.                 { error_flag = FALSE; break; } //test fails (not contained)
  80.             }
  81.  
  82.         return( error_flag );
  83. }
  84.  
  85. /*************************************************************/
  86. void getword( char *letter_set, size_t w_len )
  87. {
  88.  
  89.     char    l_set [ MAXLEN ],
  90.         word [ MAXLEN ],
  91.         tempstr [ MAXLEN + 1 ],
  92.         bar [ LINE_LEN + 1 ],
  93.         double_bar [ LINE_LEN + 1 ],
  94.    ts [ MAXLEN ];
  95.  
  96.     FILE *fptr;
  97.     long wcount = 0L;
  98.  
  99.        memset( bar, '-', LINE_LEN );
  100.        *( bar + LINE_LEN ) = NULL;
  101.        memset( double_bar, '=', LINE_LEN );
  102.        *( double_bar + LINE_LEN ) = NULL;
  103.  
  104.        /*************opening credits*************/
  105.        clrscr();
  106.        printf( double_bar );
  107.        strcpy( tempstr, ad );
  108.        center ( tempstr );
  109.        printf( tempstr );
  110.        printf( CR );
  111.        printf( double_bar );
  112.        printf( CR );
  113.        /****************************************/
  114.  
  115.  
  116.        strcpy ( l_set, letter_set );
  117.        strcat ( letter_set, CR );
  118.  
  119.  
  120.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  121.          {
  122.          printf( "\7\7\7Cannot open Wordfile!" );
  123.          exit( FILE_OPENING_ERROR );
  124.          }
  125.  
  126.        /**************'Wait' Message************/
  127.        printf( CR CR );
  128.        printf( "WORKING...\n\n" );
  129.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  130.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  131.        printf( "Now searching 94,000 word file ...\n\n" );
  132.        /*****************************************/
  133.  
  134.  
  135.  
  136.  
  137.  
  138.        sprintf( tempstr, "Words unscrambled from: %s\n", strupr( l_set ) );
  139.        center( tempstr );
  140.        printf( double_bar );
  141.        printf( tempstr );
  142.        printf( double_bar );
  143.        printf( CR );
  144.  
  145.  
  146.          /*********************Main Loop*************/     
  147.           while( fgets( word, MAXLEN, fptr ) != NULL )
  148.  
  149.             if( wordtest( letter_set, word ) )
  150.                if( strlen( word ) == w_len + INCREMENT )
  151.                  {
  152.                  printf( "%s", word );
  153.                  wcount++;
  154.                      }
  155.  
  156.       if( wcount == INCREMENT )
  157.          strcpy( ts, "word" );
  158.       else
  159.          strcpy( ts, "words" );
  160.  
  161.           /*******************************************/
  162.  
  163.           printf( bar );
  164.           sprintf( tempstr, "%ld %s can be unscrambled from %s.",
  165.                  wcount, ts, l_set );
  166.           center( tempstr );              
  167.           printf( tempstr );
  168.           printf( "\n\n" );
  169.  
  170.           center( ad );
  171.           printf( ad );
  172.       printf( "\7\n\n" );
  173.  
  174.           fcloseall();
  175.  
  176.  
  177. }
  178.  
  179.  
  180.  
  181. void center( char *str )
  182. {
  183.    int padding;
  184.    char st [ LINE_LEN + INCREMENT ];
  185.  
  186.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  187.      memset( st, SPACE, padding );
  188.      *( st + padding ) = NULL;  //Terminate string
  189.      strcat( st, str );
  190.      strcpy( str, st );
  191.  
  192.      return;
  193. }
  194.