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

  1. /**************************************************************************/
  2. /*                             Anagram 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. "ANAGRAM utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  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 = TRUE;
  66.     static char dup_lset[ MAXLEN ];
  67.     register char *letpos;
  68.  
  69. //     dup_lset = strdup( letterset );
  70.      strcpy( dup_lset, letterset );
  71.          
  72.         while( *word )
  73.             {
  74.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  75.                 *letpos = '*';     //As long as letter contained...
  76.             else
  77.                 { error_flag = FALSE; break; } //test fails (not contained)
  78.             }
  79.  
  80.         return( error_flag );
  81. }
  82.  
  83. /*************************************************************/
  84. void getword( char *letter_set )
  85. {
  86.  
  87.     char    l_set [ MAXLEN ],
  88.         word [ MAXLEN ],
  89.         tempstr [ MAXLEN + 1 ],
  90.         targetfile [ MAXLEN ],
  91.         bar [ LINE_LEN + 1 ],
  92.         double_bar [ LINE_LEN + 1 ];
  93.  
  94.     FILE *fptr,
  95.         *tfile;
  96.     int fnamelen;
  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.        /*   Create name of file to store derived words in   */
  120.        /*********************************************************/
  121.        fnamelen = strlen( l_set );
  122.        if( fnamelen  > FILENAME_MAXLEN )
  123.           fnamelen = FILENAME_MAXLEN;
  124.        strncpy( targetfile, l_set, fnamelen );
  125.        *( targetfile + fnamelen ) = NULL;
  126.        //NULL-terminate string, so strcat works, ha, ha.
  127.        strcat( targetfile, FILE_SUFFIX );
  128.        /*********************************************************/
  129.  
  130.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  131.          {
  132.          printf( "\7\7\7Cannot open Wordfile!" );
  133.          exit( FILE_OPENING_ERROR );
  134.          }
  135.  
  136.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  137.          {
  138.          printf( "\7\7\7Cannot open file to save words in!" );
  139.          exit ( FILE_OPENING_ERROR + 1 );
  140.          }
  141.  
  142.        /**************'Wait' Message************/
  143.        printf( CR CR );
  144.        printf( "WORKING...\n\n" );
  145.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  146.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  147.        printf( "Now searching 94,000 word file and writing file of valid words.\n\n" );
  148.        /*****************************************/
  149.  
  150.  
  151.  
  152.  
  153.  
  154.        sprintf( tempstr, "Words created from: %s\n", strupr( l_set ) );
  155.        center( tempstr );
  156.        fprintf( tfile, double_bar );
  157.        fprintf( tfile, tempstr );
  158.        fprintf( tfile, double_bar );
  159.        fprintf( tfile, CR );
  160.  
  161.  
  162.          /*********************Main Loop*************/     
  163.           while( fgets( word, MAXLEN, fptr ) != NULL )
  164.  
  165.             if( wordtest( letter_set, word ) )
  166.                {
  167.                fprintf( tfile, "%s", word );
  168.                wcount++;
  169.                }
  170.           /*******************************************/
  171.  
  172.           fprintf( tfile, bar );
  173.           sprintf( tempstr, "%ld words can be constructed from %s.",
  174.                  wcount - INCREMENT, l_set );
  175.           center( tempstr );              
  176.           fprintf( tfile, tempstr );
  177.           fprintf( tfile, "\n\n" );
  178.  
  179.           center( ad );
  180.           fprintf( tfile, ad );
  181.  
  182.           fcloseall();
  183.  
  184.           sprintf( tempstr,
  185.                  "The file %s has %ld words derived from %s\7.",
  186.                  targetfile, wcount - INCREMENT, l_set );
  187.           center( tempstr );
  188.           printf( CR CR );
  189.           printf( tempstr );
  190.  
  191. }
  192.  
  193.  
  194.  
  195. void center( char *str )
  196. {
  197.    int padding;
  198.    char st [ LINE_LEN + INCREMENT ];
  199.  
  200.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  201.      memset( st, SPACE, padding );
  202.      *( st + padding ) = NULL;  //Terminate string
  203.      strcat( st, str );
  204.      strcpy( str, st );
  205.  
  206.      return;
  207. }
  208.