[Code example] struct pointer keywords

User avatar

Topic author
BJT
Site Admin
Site Admin
Posts: 141
Joined: Thu Apr 20, 2017 4:43 pm
Instruction: University Student
Gender:
Age: 27
Venezuela

struct pointer keywords

Post by BJT » Sun May 14, 2017 8:51 pm

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. struct key {
  6.     char* word;
  7.     int count;
  8. } keytab[] = {
  9.     {"#define", 0},
  10.     {"#elif", 0},
  11.     {"#elifdef", 0},
  12.     {"#elifndef", 0},
  13.     {"#else", 0},
  14.     {"#endif", 0},
  15.     {"#error", 0},
  16.     {"#if", 0},
  17.     {"#ifdef", 0},
  18.     {"#ifndef", 0},
  19.     {"#include", 0},
  20.     {"#line", 0},
  21.     {"#pragma", 0},
  22.     {"#undef", 0},
  23.     {"/*", 0},
  24.     {"//", 0},
  25.     {"_Alignas", 0},
  26.     {"_Alignosf", 0},
  27.     {"_Atomic", 0},
  28.     {"_Bool", 0},
  29.     {"_Complex", 0},
  30.     {"_Generic", 0},
  31.     {"_Imaginary", 0},
  32.     {"_Noreturn", 0},
  33.     {"_Static_assert", 0},
  34.     {"_Thread_local", 0},
  35.     {"auto", 0},
  36.     {"break", 0},
  37.     {"case", 0},
  38.     {"char", 0},
  39.     {"char*", 0},
  40.     {"const", 0},
  41.     {"continue", 0},
  42.     {"default", 0},
  43.     {"do", 0},
  44.     {"double", 0},
  45.     {"double*", 0},
  46.     {"else", 0},
  47.     {"enum,", 0},
  48.     {"extern", 0},
  49.     {"for", 0},
  50.     {"float", 0},
  51.     {"float*", 0},
  52.     {"goto", 0},
  53.     {"if", 0},
  54.     {"inline", 0},
  55.     {"int", 0},
  56.     {"int*", 0},
  57.     {"long", 0},
  58.     {"register", 0},
  59.     {"return", 0},
  60.     {"restricted", 0},
  61.     {"short", 0},
  62.     {"signed", 0},
  63.     {"sizeof", 0},
  64.     {"static", 0},
  65.     {"struct", 0},
  66.     {"switch", 0},
  67.     {"typedef", 0},
  68.     {"union", 0},
  69.     {"unsigned", 0},
  70.     {"void", 0},
  71.     {"void*", 0},
  72.     {"volatile", 0},
  73.     {"while", 0}
  74. };
  75.  
  76. #define MAXWORD 100
  77. #define NKEYS (int)(sizeof keytab/sizeof(struct key))
  78.  
  79. int getword(char*, int);
  80. struct key *binsearch(char *, struct key *, int);
  81.  
  82. /* count C keywords */
  83. int main()
  84. {
  85.     char word[MAXWORD];
  86.     struct key *p;
  87.  
  88.     while (getword(word, MAXWORD) != EOF)
  89.         if (isalpha(*word)||*word=='#'||*word=='/'||*word=='_')
  90.             if ((p=binsearch(word, keytab, NKEYS)) != NULL)
  91.                 p->count++;
  92.     for (p = keytab; p < keytab + NKEYS; p++)
  93.         if (p->count > 0)
  94.             printf("%4d %s\n", p->count, p->word);
  95. }
  96.  
  97. /* binsearch: find word in tab[0]...tab[n-1] */
  98. struct key *binsearch(char *word, struct key *tab, int n)
  99. {
  100.     int cond;
  101.     struct key *low = &tab[0];
  102.     struct key *high = &tab[n];
  103.     struct key *mid;
  104.     while (low < high)
  105.     {
  106.         mid = low + (high-low) / 2;
  107.         if ((cond = strcmp(word, mid->word)) < 0)
  108.             high = mid;
  109.         else if (cond > 0)
  110.             low = mid + 1;
  111.         else
  112.             return mid;
  113.     }
  114.     return NULL;
  115. }
  116.  
  117. /* getword: get next word or character from input */
  118. int getword(char* word, int lim)
  119. {
  120.     int c, getch(void);
  121.     void ungetch(int);
  122.     char* w = word;
  123.  
  124.     while (isspace(c = getch()))
  125.         ;
  126.  
  127.     if (c != EOF)
  128.         *w++ = c;
  129.  
  130.     if (!isalpha(c) && c!='#' && c!='/'&& c!='_')
  131.     {
  132.         *w = '\0';
  133.         return c;
  134.     }
  135.  
  136.     if( (c=getch())=='/' || c=='*' )
  137.     {
  138.         *w++=c;
  139.         *w='\0';
  140.         return word[0];
  141.     }
  142.     else
  143.         ungetch(c);
  144.  
  145.     for ( ; --lim > 0; w++)
  146.         if (!isalnum(*w = getch()) && *w!='_' && *w!='*' )
  147.         {
  148.             ungetch(*w);
  149.             break;
  150.         }
  151.  
  152.     *w = '\0';
  153.     return word[0];
  154. }
  155.  
  156. #define MAX_ALLOC 100
  157. int alloc[MAX_ALLOC];
  158. int bufp=0;
  159.  
  160. int getch(void)
  161. {
  162.     return (bufp>0) ? alloc[--bufp]:getchar();
  163. }
  164.  
  165. void ungetch(int c)
  166. {
  167.     if(bufp>=MAX_ALLOC)
  168.         printf("No espace to allocate the character\n");
  169.     else
  170.         alloc[bufp++]=c;
  171. }

input

Code: Select all

int number
const char* array[100]
void*
stactic count_number( int number)
extern char letter
output

Code: Select all

   1 char
   1 char*
   1 const
   1 extern
   2 int
   1 void*
word count: 438

Link:
BBcode:
HTML:
Hide post links
Show post links

Who is online

Users browsing this forum: No registered users and 1 guest