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

  1. /**************************************************************************/
  2. /*                             Inset   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 ".set"
  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. "INSET utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  37.  
  38.  
  39. void getword( char *lset );
  40. void center( char *strng );
  41.  
  42. typedef enum { FALSE, TRUE } Boolean;
  43.  
  44. void main( int argc, char **argv )
  45. {
  46.  
  47.    char letterset[ MAXLEN ];
  48.  
  49.      if( argc == NOARGS )
  50.         {
  51.         clrscr();
  52.         puts("Enter a LETTERSET to test ... ");
  53.         gets( letterset );
  54.         }
  55.      else
  56.         strcpy( letterset, *( argv + 1 ) );
  57.  
  58.      getword( letterset );
  59. }
  60.  
  61.  
  62.  
  63. Boolean wordtest( char *letterset, char *word )
  64. {
  65.     Boolean error_flag;
  66.  
  67.       if( strstr( word, letterset ) )
  68.          error_flag = TRUE;
  69.       else
  70.          error_flag = FALSE;
  71.         return( error_flag );
  72. }
  73.  
  74. /*************************************************************/
  75. void getword( char *letter_set )
  76. {
  77.  
  78.     char    l_set [ MAXLEN ],
  79.         word [ MAXLEN ],
  80.         tempstr [ MAXLEN + 1 ],
  81.         targetfile [ MAXLEN ],
  82.         bar [ LINE_LEN + 1 ],
  83.         double_bar [ LINE_LEN + 1 ];
  84.  
  85.     FILE *fptr,
  86.         *tfile;
  87.     int fnamelen;
  88.     long wcount = 0L;
  89.  
  90.        memset( bar, '-', LINE_LEN );
  91.        *( bar + LINE_LEN ) = NULL;
  92.        memset( double_bar, '=', LINE_LEN );
  93.        *( double_bar + LINE_LEN ) = NULL;
  94.  
  95.        /*************opening credits*************/
  96.        clrscr();
  97.        printf( double_bar );
  98.        strcpy( tempstr, ad );
  99.        center ( tempstr );
  100.        printf( tempstr );
  101.        printf( CR );
  102.        printf( double_bar );
  103.        printf( CR );
  104.        /****************************************/
  105.  
  106.  
  107.        strcpy ( l_set, letter_set );
  108. //       strcat ( letter_set, CR );
  109.  
  110.        /*   Create name of file to store derived words in   */
  111.        /*********************************************************/
  112.        fnamelen = strlen( l_set );
  113.        if( fnamelen  > FILENAME_MAXLEN )
  114.           fnamelen = FILENAME_MAXLEN;
  115.        strncpy( targetfile, l_set, fnamelen );
  116.        *( targetfile + fnamelen ) = NULL;
  117.        //NULL-terminate string, so strcat works, ha, ha.
  118.        strcat( targetfile, FILE_SUFFIX );
  119.        /*********************************************************/
  120.  
  121.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  122.          {
  123.          printf( "\7\7\7Cannot open Wordfile!" );
  124.          exit( FILE_OPENING_ERROR );
  125.          }
  126.  
  127.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  128.          {
  129.          printf( "\7\7\7Cannot open file to save words in!" );
  130.          exit ( FILE_OPENING_ERROR + 1 );
  131.          }
  132.  
  133.        /**************'Wait' Message************/
  134.        printf( CR CR );
  135.        printf( "WORKING...\n\n" );
  136.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  137.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  138.        printf( "Now searching 94,000 word file \nand writing file of valid words using -%s-.\n\n", 
  139.             letter_set );
  140.        /*****************************************/
  141.  
  142.  
  143.  
  144.  
  145.  
  146.        sprintf( tempstr, "Words created from: %s\n", strupr( l_set ) );
  147.        center( tempstr );
  148.        fprintf( tfile, double_bar );
  149.        fprintf( tfile, tempstr );
  150.        fprintf( tfile, double_bar );
  151.        fprintf( tfile, CR );
  152.  
  153.  
  154.          /*********************Main Loop*************/     
  155.           while( fgets( word, MAXLEN, fptr ) != NULL )
  156.  
  157.             if( wordtest( letter_set, word ) )
  158.                {
  159.                fprintf( tfile, "%s", word );
  160.                wcount++;
  161.                }
  162.           /*******************************************/
  163.  
  164.           fprintf( tfile, bar );
  165.           sprintf( tempstr, "%ld words can be constructed from %s.",
  166.                  wcount - INCREMENT, l_set );
  167.           center( tempstr );              
  168.           fprintf( tfile, tempstr );
  169.           fprintf( tfile, "\n\n" );
  170.  
  171.           center( ad );
  172.           fprintf( tfile, ad );
  173.  
  174.           fcloseall();
  175.  
  176.           sprintf( tempstr,
  177.                  "The file %s has %ld words derived from %s\7.",
  178.                  targetfile, wcount - INCREMENT, l_set );
  179.           center( tempstr );
  180.           printf( CR CR );
  181.           printf( tempstr );
  182.  
  183. }
  184.  
  185.  
  186.  
  187. void center( char *str )
  188. {
  189.    int padding;
  190.    char st [ LINE_LEN + INCREMENT ];
  191.  
  192.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  193.      memset( st, SPACE, padding );
  194.      *( st + padding ) = NULL;  //Terminate string
  195.      strcat( st, str );
  196.      strcpy( str, st );
  197.  
  198.      return;
  199. }
  200.