home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d03xx / d0366.lha / Makewords / Source / AllPhoneWord.c < prev    next >
C/C++ Source or Header  |  1990-08-12  |  4KB  |  155 lines

  1. /* AllPhoneWord.c - try to make words from a phone number.
  2.  
  3.                                 Ron Charlton
  4.                              9002 Balcor Circle
  5.                              Knoxville, TN 37923
  6.    
  7.                              Phone: (615)694-0800
  8.    
  9.                               PLINK: R*CHARLTON
  10.                            BINTNET: charltr@utkvx1
  11.  
  12.                                  05-Jul-90
  13.  
  14. This program is in the public domain.  It may be used for any purpose.
  15.  
  16. Algorithm:  Generate ALL of the letters in a phone number with a bunch of
  17. nested 'for' loops.  From 1 to 7 digits may be entered.
  18.  
  19. history:
  20. v1.1   changed to lowercase, added version number
  21. */
  22.  
  23. char version [] = "v1.1";
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. #define FALSE 0
  29. #define TRUE (!FALSE)
  30.  
  31. #ifdef MCH_AMIGA
  32. #define CSI 0x9b
  33. #define CLS 0x0c
  34. #endif
  35.  
  36. #define MAXDIGITS 7
  37. #define LINESPERSCREEN 10
  38.  
  39. int i [ MAXDIGITS ];            /* loop counter */
  40.  
  41. int WordsThisLine, MaxPerLine;
  42. int lines, c;
  43.  
  44. char letters [][4] = { "@@@", "@@@", "abc", "def", "ghi", "jkl", "mno",
  45.                "prs", "tuv", "wxy" };
  46.  
  47. char Dstr [ 255+1 ];            /* string from user */
  48. char work [ MAXDIGITS+1 ];        /* build string to print here */
  49.  
  50. /*------------------------------------------------------------------------*/
  51.  
  52. main()
  53. {
  54. int dig_pos, length, good;
  55.  
  56. intro();
  57.  
  58. do
  59.   {
  60.   good = TRUE;
  61.   printf ( "\tEnter 1 to 7 digits (no 1 or 0) <RETURN> to quit): " );
  62.   gets ( Dstr );
  63.   length = strlen ( Dstr );
  64.   if ( length == 0 ) exit ();
  65.   if ( length > MAXDIGITS )
  66.     {
  67.     printf ( "\t\tEnter no more than %d digits.\n\n", MAXDIGITS );
  68.     continue;
  69.     }
  70.   /* check for bad characters ( only 2-9 allowed ) */
  71.   for ( dig_pos = 0; dig_pos < length; dig_pos++ )
  72.     if ( !isdigit ( Dstr [ dig_pos ] ) || Dstr [ dig_pos ] == '1' ||
  73.         Dstr [ dig_pos ] == '0' )
  74.       {
  75.       printf ( "\t\tOnly digits 2-9 allowed.\n\n" );
  76.       good = FALSE;
  77.       break;
  78.       }
  79.   if ( good )
  80.     {
  81.     lines = 0;
  82.     MaxPerLine = 40 / length;        /* how many "words" per line */
  83.     WordsThisLine = 0;
  84.     printf ( "\n\t\t" );
  85.     generate ( 0, length );        /* generate all of the possibilies */
  86.     printf ( "\n\n" );
  87.     }
  88.   } while ( length > 0 );
  89. }
  90.  
  91. /*------------------------------------------------------------------------*/
  92.  
  93. intro()
  94. {
  95. #ifdef MCH_AMIGA
  96. printf ( "%c", CLS );            /* clear screen */
  97. printf ( "%c0;33;40m", CSI );        /* color 3,0 */
  98. #endif
  99. printf ( "\n\t\t\t    AllPhoneWord %s\n\n",version );
  100. printf (   "\t\t\t           by\n\n" );
  101. printf (   "\t\t\t      Ron Charlton\n" );
  102. printf (   "\t\t\t   9002 Balcor Circle\n" );
  103. printf (   "\t\t\t   Knoxville, TN 37923\n\n" );
  104. #ifdef MCH_AMIGA
  105. printf ( "%c0;31;40m", CSI );        /* color 1,0 */
  106. #endif
  107.  
  108. printf ( "\tAllPhoneWord tries to create words out of telephone numbers\n" );
  109. printf ( "\tthat you enter.  It will print out all of the possible\n" );
  110. printf ( "\tcombinations of letters in the phone number.  Note that this\n" );
  111. printf ( "\twill be 2,187 'words' for a 7-digit number (and a lot of time\n" );
  112. printf ( "\tlisting them all). Digits 0 and 1 are not accepted (why?).\n\n" );
  113.  
  114. printf ( "\tIf your number has 0 or 1 in it you can enter the other\n" );
  115. printf ( "\tdigits in several groups.  It is good to try different\n" );
  116. printf ( "\tgroups of digits anyway, for example, if your number is\n" );
  117. printf ( "\t234-5678, then try 234 & 5678; 2345 & 678; 23456 & 78;\n" );
  118. printf ( "\t2345678, etc.\n\n" );
  119. }
  120.  
  121. /*------------------------------------------------------------------------*/
  122.  
  123. generate ( pos, len )
  124.   /* generate the "words" recursively (creates "len" nested "for" loops) */
  125.   int pos, len;
  126.   {
  127.   int s;
  128.   if ( pos < len )
  129.     for ( i [ pos ] = 0; i [ pos ] < 3; ++i[pos] )
  130.       {
  131.       generate ( pos+1, len );
  132.       }
  133.   else
  134.     {
  135.     /* build string and print it */
  136.     for ( s = 0; s < len; s++ )
  137.       work [ s ] = letters [ Dstr[s] - '0'] [ i[s] ];
  138.     work [ s ] = '\0';
  139.     ++WordsThisLine;
  140.     if ( WordsThisLine > MaxPerLine )        /* time for new line */
  141.       {
  142.       WordsThisLine = 1;
  143.       ++lines;
  144.       if ( lines > LINESPERSCREEN )
  145.      {
  146.     lines = 0;
  147.     printf ( "\n\t<RETURN> to continue...  (%s)", Dstr );
  148.     c = getchar();
  149.     }
  150.       printf ( "\n\n\t\t" );
  151.       }
  152.     printf ( "%s ", work );
  153.     }
  154.   }
  155.