home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / DICTIONA.C < prev    next >
C/C++ Source or Header  |  1996-06-04  |  1KB  |  67 lines

  1. /* Copyright 1992 Digital Equipment Corporation
  2.    All Rights Reserved
  3. */
  4.  
  5. /* Dictionary program in C. */
  6. /* Author: Peter Van Roy */
  7.  
  8. #include <stdio.h>
  9. #define FALSE 0
  10. #define TRUE 1
  11.  
  12. typedef char *string;
  13.  
  14. typedef struct _base_tree *tree;
  15.  
  16. typedef struct _base_tree {
  17.     string name, def;
  18.     tree left, right;
  19. } base_tree;
  20.  
  21. int contains(t,name1,def1)
  22. tree *t;
  23. string *name1, *def1;
  24. {
  25.     if (*t) {
  26.         if (*name1) {
  27.         int c=strcmp((*t)->name,*name1);
  28.         if (c<0)
  29.             return contains(&((*t)->left),  name1, def1);
  30.         else if (c>0)
  31.             return contains(&((*t)->right), name1, def1);
  32.         else {
  33.         if (*def1) {
  34.             if ((*t)->def)
  35.                 return (strcmp((*t)->def,*def1)==0);
  36.                 else
  37.             (*t)->def = *def1;
  38.             } else {
  39.                     if ((*t)->def)
  40.             *def1 = (*t)->def;
  41.             else
  42.             printf("Error: can't handle var-var case\n");
  43.                 }
  44.         }
  45.         } else {
  46.         *name1= (*t)->name;
  47.         *def1 = (*t)->def;
  48.         }
  49.     } else {
  50.     (*t) = (tree) calloc(1, sizeof(struct _base_tree));
  51.     (*t)->name = *name1;
  52.     (*t)->def  = *def1;
  53.     }
  54.     return TRUE;
  55. }
  56.  
  57. main() {
  58.     tree t=NULL;
  59.     string cn="cat", cd="furry feline";
  60.     string dn="dog", dd="furry canine";
  61.     string def=NULL;
  62.     contains(&t, &cn, &cd);
  63.     contains(&t, &dn, &dd);
  64.     contains(&t, &cn, &def);
  65.     printf("A %s is a %s\n",cn,def);
  66. }
  67.