home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume3 / telno < prev    next >
Internet Message Format  |  1986-11-30  |  5KB

  1. From: genrad!decvax!ihnp4!homebru!ignatz
  2. Subject: Telno: a telephone number permutation program
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 65
  7. Submitted by: ihnp4!homebru!ignatz <Dave Ihnat>
  8.  
  9.  
  10. This is a little bit of fluff that arose in the course of a night of
  11. imbibing inspriational spirits; I'm sure it's been done before.
  12.  
  13. Simply enough, it takes a telephone number and provides all possible
  14. permutations of the letters on the dial keycaps.  It doesn't attempt to
  15. be nifty and throw out combinations that aren't pronouncable; you've got
  16. a neat array processor of your own that's quite a bit faster at complex
  17. pattern recognition than the poor program.  Besides, I was too hazy to
  18. think that hard...  The person who asked if this was possible is happy;
  19. that's as far as I'm going to take this thing.
  20.  
  21. As I say, I only apologize slightly for the code quality; I'm rather
  22. amazed that it worked...
  23.  
  24.     Dave Ihnat
  25.     ihnp4!homebru!ignatz
  26.  
  27. ===================== Cut me, hurt me, but do it here ==============
  28. /*
  29.  * telno - permute telephone number letters
  30.  *
  31.  * Author: David M. Ihnat
  32.  *
  33.  * This is a trivial program to accept a typical United States-type,
  34.  * 7-digit telephone number, and provide all possible unique permutations of
  35.  * the letters found on the standard dial positions.  Although this is
  36.  * a quick-and-dirty program, I did try to design it in such a manner
  37.  * that different characters and telephone number lengths should be
  38.  * easy to do.
  39.  *
  40.  * I freely grant anyone the right to do anything they wish with this
  41.  * thing, as long as it's (as usual) not for profit.  I apologize only
  42.  * casually for the code, as I was more than 3 sheets to the wind when
  43.  * I slapped it out...logic and Jack Daniel's are strange glass-fellows...
  44.  */
  45. #include <stdio.h>
  46.  
  47. #define    VERSION        "1.0"    /* As if there's going to be a 2.0...  */
  48.  
  49. #define    TEL_LEN        7    /* Length of a telephone number in digits */
  50. #define    MAX_COMB    2188    /* Maximum number of combinations */
  51.  
  52. #define    NULL_CHAR    '@'    /* Indicates not to do this character */
  53.  
  54. /*
  55.  * This must equal the length of the entries in telno_def[]. In this
  56.  * manner, you can add arbitrary combinations of letters, digits, etc.
  57.  * to satisfy different character sets; or, for instance, you may wish
  58.  * to allow the actual number in the string, as well.  If you *do*
  59.  * change SUBSTR_LEN, don't forget to change MAX_COMB.
  60.  */
  61. #define    SUBSTR_LEN    3
  62. char *telno_def[10] = {
  63. /*      0      1      2      3      4      5      6      7      8      9  */
  64.     "0@@", "1@@", "abc", "def", "ghi", "jkl", "mno", "prs", "tuv", "wxy"
  65.     };
  66.  
  67. /* The payoff; 3 possible/no, plus null string*/
  68. char listary[MAX_COMB][TEL_LEN+1];
  69.  
  70. list_idx = 0;
  71.  
  72. extern int errno;
  73.  
  74. main(argc,argv)
  75. int argc;
  76. char *argv[];
  77. {
  78.     register char *telptr;
  79.     register short index;
  80.     short tel_idx[TEL_LEN];
  81.  
  82.     argc--,argv++;        /* Most micros don't support argv[0] */
  83.     if(!argc)
  84.     {
  85.         fprintf(stderr,"Usage: xxx[-]yyyy\n",*argv);
  86.         exit(1);
  87.     }
  88.  
  89.     telptr = *argv;
  90.  
  91.     /* If there is a dash in the telephone string, cut it out. */
  92.     if(telptr[3] == '-')
  93.     {
  94.         register char *p1,*p2;
  95.         for(p1 = &telptr[3], p2 = &telptr[4];*p1 != '\0';)
  96.             *p1++ = *p2++;
  97.     }
  98.  
  99.     fprintf(stderr,"Processing for %s....",telptr);
  100.  
  101.     /*
  102.      * Now to break the telephone number apart into an array of
  103.      * indices
  104.      */
  105.     for(index=0;index < TEL_LEN;index++)
  106.         tel_idx[index] = (*telptr++) - '0';
  107.  
  108.     /* Now build the permuations */
  109.     proc_num(tel_idx);
  110.  
  111.     /* Null-terminate the array */
  112.     listary[list_idx][0] = '\0';
  113.  
  114.     /* Now sort the array */
  115.     sort_num();
  116.  
  117.     /* Finally, print it out. */
  118.     fprintf(stderr,"done.\n\n");
  119.     dump_num();
  120. }
  121.  
  122. proc_num(prim_idx)
  123. short prim_idx[];
  124. {
  125.     register int index;
  126.     short sec_idx[TEL_LEN];
  127.  
  128.     /*
  129.      * Permute the given TEL_LEN-digit array for all possible
  130.      * combinations of related key-top digits.  This could
  131.      * be done either iteratively or recursively; to keep
  132.      * stack usage down, I've selected an iterative approach.
  133.      *
  134.      * If you extend or shrink the length of a telepone number,
  135.      * change the number of for loops here.
  136.      */
  137.  
  138.     for(sec_idx[0] = 0;sec_idx[0] < SUBSTR_LEN;++sec_idx[0])
  139.      for(sec_idx[1] = 0;sec_idx[1] < SUBSTR_LEN;++sec_idx[1])
  140.       for(sec_idx[2] = 0;sec_idx[2] < SUBSTR_LEN;++sec_idx[2])
  141.        for(sec_idx[3] = 0;sec_idx[3] < SUBSTR_LEN;++sec_idx[3])
  142.         for(sec_idx[4] = 0;sec_idx[4] < SUBSTR_LEN;++sec_idx[4])
  143.          for(sec_idx[5] = 0;sec_idx[5] < SUBSTR_LEN;++sec_idx[5])
  144.           for(sec_idx[6] = 0;sec_idx[6] < SUBSTR_LEN;++sec_idx[6])
  145.           {
  146.         bld_str(prim_idx,sec_idx);
  147.           }
  148. }
  149.  
  150. bld_str(prim_idx,sec_idx)
  151. short prim_idx[],sec_idx[];
  152. {
  153.     register int idx;
  154.     register char c;
  155.     for(idx=0; idx < TEL_LEN; idx++)
  156.     {
  157.         c = telno_def[prim_idx[idx]][sec_idx[idx]];
  158.  
  159.         if(c == NULL_CHAR)
  160.             return;
  161.  
  162.         listary[list_idx][idx] = c;
  163.     }
  164.     listary[list_idx][8] = '\0';
  165.     list_idx++;
  166. }
  167.  
  168. sort_num()
  169. {
  170.     int strcmp();
  171.  
  172.     /*
  173.      * Cheat--use the library sort.  If your library doesn't have
  174.      * one, well...have fun.  It's not too hard...
  175.      */
  176.     qsort(listary,(list_idx-1),8,strcmp);
  177. }
  178.  
  179. dump_num()
  180. {
  181.     /*
  182.      * Dump the permutations, 10 per line.  (Just fits on 80-col
  183.      * printout)
  184.      */
  185.     register int idx1,idx2;
  186.     for(idx1 = 0;*listary[idx1] != '\0';)
  187.     {
  188.         for(idx2=0;(*listary[idx1] != '\0') && (idx2 < 10); idx1++,idx2++)
  189.         {
  190.             fputs(listary[idx1],stdout);
  191.             fputc(' ',stdout);
  192.         }
  193.         fputc('\n',stdout);
  194.     }
  195.  
  196.     fputc('\n',stdout);
  197.  
  198. }
  199.  
  200.  
  201.