home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / games / wordy / anagram2.c < prev    next >
C/C++ Source or Header  |  1994-11-04  |  6KB  |  226 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. /*      Special version created for David Thomas of Penrith, Australia    */
  13. /*             [Prints words to file in columns of 6]                     */
  14. /*                                                                        */
  15. /**************************************************************************/
  16.  
  17.  
  18. /**********************************WORDTEST********************************/
  19. /*       Function tests if word is constructible from Letterset            */
  20. /*                 Args in: char *letterset, char *word                          */
  21. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  22. /**************************************************************************/
  23.  
  24. #include <conio.h>
  25. #include "srch.h"
  26.  
  27.  
  28. #define FILE_OPENING_ERROR 3
  29. #define FILENAME_MAXLEN 8
  30. #define CR "\n"
  31. #define FILE_SUFFIX ".wds"
  32. #define MAXLEN 30
  33. #define LINE_LEN 80
  34. #define NOARGS 1
  35. #define INCREMENT 1
  36. #define SPACE ' '
  37.  
  38. char ad[] =
  39. "ANAGRAM utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  40.  
  41.  
  42. void getword( char *lset );
  43. void center( char *strng );
  44.  
  45. typedef enum { FALSE, TRUE } Boolean;
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset[ MAXLEN ];
  51.  
  52.      if( argc == NOARGS )
  53.         {
  54.         clrscr();
  55.         puts("Enter a LETTERSET to test ... ");
  56.         gets( letterset );
  57.         }
  58.      else
  59.         strcpy( letterset, *( argv + 1 ) );
  60.  
  61.      getword( letterset );
  62. }
  63.  
  64.  
  65.  
  66. Boolean wordtest( char *letterset, char *word )
  67. {
  68.     Boolean error_flag = TRUE;
  69.     static char dup_lset[ MAXLEN ];
  70.     register char *letpos;
  71.  
  72. //     dup_lset = strdup( letterset );
  73.      strcpy( dup_lset, letterset );
  74.          
  75.         while( *word )
  76.             {
  77.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  78.                 *letpos = '*';     //As long as letter contained...
  79.             else
  80.                 { error_flag = FALSE; break; } //test fails (not contained)
  81.             }
  82.  
  83.         return( error_flag );
  84. }
  85.  
  86. /*************************************************************/
  87. void getword( char *letter_set )
  88. {
  89.  
  90.     char    l_set [ MAXLEN ],
  91.         word [ MAXLEN ],
  92.         tempstr [ 3 * MAXLEN ],
  93.         targetfile [ MAXLEN ],
  94.         bar [ LINE_LEN + 1 ],
  95.         double_bar [ LINE_LEN + 1 ];
  96.  
  97.     FILE *fptr,
  98.         *tfile;
  99.     int fnamelen;
  100.     long wcount = 0L;
  101.  
  102.        memset( bar, '-', LINE_LEN );
  103.        *( bar + LINE_LEN ) = NULL;
  104.        memset( double_bar, '=', LINE_LEN );
  105.        *( double_bar + LINE_LEN ) = NULL;
  106.  
  107.        /*************opening credits*************/
  108.        clrscr();
  109.        printf( double_bar );
  110.        strcpy( tempstr, ad );
  111.        center ( tempstr );
  112.        printf( tempstr );
  113.        printf( CR );
  114.        printf( double_bar );
  115.        printf( CR );
  116.        /****************************************/
  117.  
  118.  
  119.        strcpy ( l_set, letter_set );
  120.        strcat ( letter_set, CR );
  121.  
  122.        /*   Create name of file to store derived words in   */
  123.        /*********************************************************/
  124.        fnamelen = strlen( l_set );
  125.        if( fnamelen  > FILENAME_MAXLEN )
  126.           fnamelen = FILENAME_MAXLEN;
  127.        strncpy( targetfile, l_set, fnamelen );
  128.        *( targetfile + fnamelen ) = NULL;
  129.        //NULL-terminate string, so strcat works, ha, ha.
  130.        strcat( targetfile, FILE_SUFFIX );
  131.        /*********************************************************/
  132.  
  133.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  134.          {
  135.          printf( "\7\7\7Cannot open Wordfile!" );
  136.          exit( FILE_OPENING_ERROR );
  137.          }
  138.  
  139.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  140.          {
  141.          printf( "\7\7\7Cannot open file to save words in!" );
  142.          exit ( FILE_OPENING_ERROR + 1 );
  143.          }
  144.  
  145.        /**************'Wait' Message************/
  146.        printf( CR CR );
  147.        printf( "WORKING...\n\n" );
  148.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  149.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  150.        printf( "Now searching 94,000 word file and writing file of valid words.\n\n" );
  151.        /*****************************************/
  152.  
  153.  
  154.  
  155.  
  156.  
  157.        sprintf( tempstr, "Words created from: %s\n", strupr( l_set ) );
  158.        center( tempstr );
  159.        fprintf( tfile, double_bar );
  160.        fprintf( tfile, tempstr );
  161.        fprintf( tfile, double_bar );
  162.        fprintf( tfile, CR );
  163.  
  164.  
  165.          /*********************Main Loop*************/     
  166.           while( fgets( word, MAXLEN, fptr ) != NULL )
  167.  
  168.             if( wordtest( letter_set, word ) )
  169.                {
  170.       *( word + strlen( word ) -1 ) = NULL;  //Gets rid of CR
  171.  
  172.                fprintf( tfile, "%s", word );
  173.  
  174.       if( !( wcount % 6 ) && wcount > 5 )
  175.          fprintf( tfile, CR );
  176.       else
  177.          fprintf( tfile, "\t" );
  178.  
  179.                wcount++;
  180.                }
  181.           /*******************************************/
  182.       fprintf( tfile, CR );
  183.           fprintf( tfile, bar );
  184.           sprintf( tempstr, "%ld words can be constructed from %s.",
  185.                  wcount - INCREMENT, l_set );
  186.           center( tempstr );              
  187.           fprintf( tfile, tempstr );
  188.           fprintf( tfile, "\n\n" );
  189.  
  190.           center( ad );
  191.           fprintf( tfile, ad );
  192.  
  193.           fcloseall();
  194.  
  195.           sprintf( tempstr,
  196.                  "The file %s has %ld words derived from %s\7.",
  197.                  targetfile, wcount - INCREMENT, l_set );
  198.           center( tempstr );
  199.           printf( CR CR );
  200.           printf( tempstr );
  201.  
  202.       strcpy( tempstr,
  203.       "This custom version of ANAGRAM created for David Thomas of Australia." );
  204.       center( tempstr );
  205.       printf( CR CR );
  206.       printf( tempstr );
  207.       printf( CR CR );
  208.  
  209. }
  210.  
  211.  
  212.  
  213. void center( char *str )
  214. {
  215.    int padding;
  216.    char st [ LINE_LEN + INCREMENT ];
  217.  
  218.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  219.      memset( st, SPACE, padding );
  220.      *( st + padding ) = NULL;  //Terminate string
  221.      strcat( st, str );
  222.      strcpy( str, st );
  223.  
  224.      return;
  225. }
  226.