Datei: p8-1.c


    1   /*
    2    *      p8-1.c
    3    *      Beispielprogramm 1, Abschnitt 8
    4    *      Vorkommen reservierter Woerter zaehlen 
    5    */
    6   
    7   #include <stdio.h>
    8   #include <string.h>
    9   
   10   #define MAXWORD 20
   11   #define NKEYS (sizeof(keytab) / sizeof(struct key))
   12   #define LETTER 'a'
   13   #define DIGIT '0'
   14   
   15   struct key {   /* global, daher initialisierbar */
   16     char *keyword;
   17     int keycount;
   18   } keytab[] = {
   19     "auto", 0,
   20     "break", 0,
   21     "case", 0,
   22     "char", 0,
   23     "const", 0,
   24     "continue", 0,
   25     "default", 0,
   26     "do", 0,
   27     "double", 0,
   28     "else", 0,
   29     "enum", 0,
   30     "extern", 0,
   31     "float", 0,
   32     "for", 0,
   33     "goto", 0,
   34     "if", 0,
   35     "int", 0,
   36     "long", 0,
   37     "register", 0,
   38     "return", 0,
   39     "short", 0,
   40     "signed", 0,
   41     "sizeof", 0,
   42     "static", 0,
   43     "struct", 0,
   44     "switch", 0,
   45     "typedef", 0,
   46     "union", 0,
   47     "unsigned", 0,
   48     "void", 0,
   49     "volatile", 0,
   50     "while", 0,
   51   };
   52   
   53   int binary(char *, struct key[], int);
   54   int getword(char *, int);
   55   int type(int);
   56   
   57   void main(void)
   58   {
   59     int n, t;
   60     char word[MAXWORD];
   61     
   62     while ((t = getword(word, MAXWORD)) != EOF)
   63       if (t == LETTER)
   64         if ((n = binary(word, keytab, NKEYS)) >= 0)
   65   	keytab[n].keycount++;
   66   	
   67     for (n = 0; n < NKEYS; n++)
   68       if (keytab[n].keycount > 0)
   69         printf("%4d %s\n", keytab[n].keycount, keytab[n].keyword);
   70         
   71   } /* main() */
   72   
   73   /* word in tab[0],...,tab[n-1] finden */
   74   int binary(char *word, struct key tab[], int n)
   75   {
   76     int low, high, mid, cond;
   77     
   78     low = 0;
   79     high = n - 1;
   80     while (low <= high) {
   81       mid = (low + high) / 2;
   82       if ((cond = strcmp(word, tab[mid].keyword)) < 0)
   83         high = mid - 1;
   84       else if (cond > 0)
   85         low = mid + 1;
   86       else
   87         return(mid);
   88     }
   89     return(-1);
   90     
   91   } /* binary() */
   92   
   93   #define getch() getc(stdin)
   94   #define ungetch(c) ungetc(c, stdin)
   95    
   96   /* naechstes Wort aus der Eingabe holen */
   97   int getword(char *w, int lim)
   98   {
   99     int c, t;
  100     
  101     if (type(c = *w++ = getch()) != LETTER) {
  102       *w = '\0';
  103       return(c);
  104     }
  105     while (--lim > 0) {
  106       t = type(c= *w++ = getch());
  107       if (t != LETTER && t != DIGIT) {
  108         ungetch(c);
  109         break;
  110       }
  111     }
  112     *(w-1) = '\0';
  113     return(LETTER);
  114   
  115   } /* getword() */
  116   
  117   /* Typ eines ASCII Zeichens liefern */
  118   int type(int c)
  119   {
  120     if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
  121       return(LETTER);
  122     else if (c >= '0' && c <= '9')
  123       return(DIGIT);
  124     else
  125       return(c);
  126   
  127   } /* type() */


Erzeugt von c2html 1.01