home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol271 / namecont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-31  |  4.1 KB  |  225 lines

  1. #include   <stdio.h>
  2. #include   <cpm.h>
  3.  
  4.  
  5. #define   ALPHALENG      10
  6. #define   BLANK     "          "
  7. #define   RLEVMAX 8
  8.  
  9. struct    item {char name[ALPHALENG];
  10.                 int  count;
  11.                 struct item *left;
  12.                 struct item *right; };
  13. char      ch;
  14. char      w[ALPHALENG];
  15. int       eoln,eofmode;
  16. struct    item *root;
  17.  
  18. main()
  19. {
  20. int  prinname(),princont();
  21.      root=NULL;
  22.      ch=' ';
  23.      eofmode=NO;
  24.      while(eofmode==NO) {
  25.           if(('A'<=ch & ch<='Z') | ('a'<=ch & ch<='z')) {
  26.                scanname();
  27.                enter(&root);
  28.           }
  29.           else if(ch=='"') 
  30.                scanstr();
  31.           else if(ch=='\'')
  32.                scanquo();
  33.           else if(ch=='/') {
  34.                nextch();
  35.                if(ch=='*')
  36.                     skipcom();
  37.           }
  38.           else
  39.                nextch();
  40.      }
  41.      crlf();
  42.      crlf();
  43.      showtree(prinname);
  44.      crlf();
  45.      crlf();
  46.      showtree(princont);
  47. }
  48.           
  49. nextch()
  50. {
  51. int  prinname(),princont();
  52.      if(eofmode==YES) {
  53.           printf("?eof");
  54.           crlf();
  55.           crlf();
  56.           showtree(prinname);
  57.           crlf();
  58.           crlf();
  59.           showtree(princont);
  60.           exit();
  61.      }
  62.      else
  63.           if((ch=getchar())==EOF)
  64.                eofmode=YES;
  65. }
  66.  
  67. scanname()
  68. {
  69. int       wx;
  70.           filchr(w,' ',ALPHALENG);
  71.           wx=1;
  72.           w[wx]=ch;
  73.           nextch();
  74.           while((('A'<=ch) & (ch<='Z'))
  75.               | (('a'<=ch) & (ch<='z'))
  76.               | (('0'<=ch) & (ch<='9'))) {
  77.                wx+=1;
  78.                if(wx<=ALPHALENG)
  79.                     w[wx]=ch;
  80.                nextch();
  81.           }
  82. }
  83.  
  84. scanstr()
  85. {
  86.           nextch();
  87.           do {
  88.                while('"'!=ch)
  89.                     nextch();
  90.                nextch();
  91.           } while(ch=='"');
  92. }
  93. scanquo()
  94. {
  95.           nextch();
  96.           if(ch=='\\')        /* '\?' */
  97.                nextch();
  98.           nextch(); nextch();
  99. }
  100.  
  101. skipcom()
  102. {
  103.           nextch();
  104.           do {
  105.                while(ch!='*')
  106.                     nextch();
  107.                nextch();
  108.           } while(ch!='/');
  109.           nextch();
  110. }
  111.  
  112. filchr(wd,chr,leng)
  113. register char *wd;
  114. register char chr;
  115. int          leng;
  116. {
  117. int i;
  118.      i=0;
  119.      while((i++)<leng)
  120.           *(wd++)=chr;
  121. }
  122.  
  123. enter(pt)
  124. struct item *(*pt);
  125. {
  126. int    *malloc();
  127. struct item *ptr;
  128.      ptr=(*pt);
  129.      if(*pt==NULL) {
  130.           *pt=ptr=malloc(ALPHALENG+12);
  131.           strcpy(ptr->name,w);
  132.           ptr->count=1;
  133.           ptr->left=NULL;
  134.           ptr->right=NULL;
  135.      }
  136.      else {
  137.           if(strcmp(w,ptr->name)==0)
  138.                ptr->count++;
  139.           else if(strcmp(w,ptr->name)<0)
  140.                enter(&(ptr->left));
  141.           else                     /* strcmp(w,ptr->name)>0 */
  142.                enter(&(ptr->right));
  143.      }
  144. }
  145.  
  146. showtree(qq)
  147. int  (*qq) ();
  148. {
  149. int  printree ();
  150. int  vline[RLEVMAX];
  151.      printree(root,1,qq,vline);
  152. }
  153.  
  154. printree(pt,rlev,prinnode,vline)
  155. struct item    *pt;
  156. int            rlev;
  157. int            vline[];
  158. int            (*prinnode) ();
  159. {
  160. struct item    *ptr;
  161.      ptr=pt;
  162.      (*prinnode) (ptr);
  163.      if(ptr->left!=NULL)
  164.           vline[rlev]=YES;
  165.      else
  166.           vline[rlev]=NO;
  167.      if((ptr->right!=NULL) & (rlev<RLEVMAX-1)) {
  168.           printf("--");
  169.           printree(ptr->right,rlev+1,prinnode,vline);
  170.      }
  171.      else {
  172.           if(ptr->right!=NULL)
  173.                printf("+++++");
  174.           crlf();
  175.           printvl(rlev,vline);
  176.           crlf();
  177.      }
  178.      if(ptr->left!=NULL) {
  179.           printvl(rlev-1,vline);
  180.           printree(ptr->left,rlev,prinnode,vline);
  181.      }
  182. }
  183.  
  184. printvl(r,vline)
  185. int  r;
  186. int  vline[];
  187. {
  188. register  int  i;
  189.      for(i=1;i<=r;i++) {
  190.           if(vline[i]==YES)
  191.                printf("I            ");
  192.           else
  193.                printf("             ");
  194.      }
  195. }
  196.  
  197. prinname(p)
  198. struct    item      *p;
  199. {
  200. register  int  i,j;
  201.      strcpy(w,p->name);  /* Extract name */
  202.      i=ALPHALENG;
  203.      if(p->right!=NULL) {
  204.           while(w[i]==' ') {
  205.                w[i]='-';
  206.                --i;
  207.           }
  208.      }
  209.      printf("*%s",w);
  210. }
  211.  
  212. princont(p)
  213. struct    item      *p;
  214. {
  215.      printf("*< %d >",p->count);
  216.      if(p->right!=NULL)
  217.           printf("-----");
  218. }
  219.  
  220. crlf()
  221. {
  222.      printf("\n");
  223. }
  224.  
  225.