[Exercise] Exercise 6-1 struct write a better version page 136

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

Exercise 6-1 struct write a better version page 136

Post by BJT » Sun May 14, 2017 5:46 pm

Exercise 6-1. Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version.

  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. int binsearch(char* word, struct key tab[], int n);
  81.  
  82. /* count C keywords */
  83. int main()
  84. {
  85.     int n;
  86.     char word[MAXWORD];
  87.  
  88.     while (getword(word, MAXWORD) != EOF)
  89.         if (isalpha(*word)||*word=='#'||*word=='/'||*word=='_')
  90.             if ((n = binsearch(word, keytab, NKEYS)) >= 0)
  91.                 keytab[n].count++;
  92.  
  93.     for (n = 0; n < NKEYS; n++)
  94.         if (keytab[n].count > 0)
  95.             printf("%4d %s\n", keytab[n].count, keytab[n].word);
  96.  
  97.     return 0;
  98. }
  99.  
  100. /* binsearch: find word in tab[0]...tab[n-1] */
  101. int binsearch(char* word, struct key tab[], int n)
  102. {
  103.     int cond;
  104.     int low, high, mid;
  105.     low = 0;
  106.     high = n - 1;
  107.  
  108.     while (low <= high)
  109.     {
  110.         mid = (low+high) / 2;
  111.         if ((cond = strcmp(word, tab[mid].word)) < 0)
  112.             high = mid - 1;
  113.         else if (cond > 0)
  114.             low = mid + 1;
  115.         else
  116.             return mid;
  117.     }
  118.  
  119.     return -1;
  120. }
  121.  
  122. /* getword: get next word or character from input */
  123. int getword(char* word, int lim)
  124. {
  125.     int c, getch(void);
  126.     void ungetch(int);
  127.     char* w = word;
  128.  
  129.     while (isspace(c = getch()))
  130.         ;
  131.  
  132.     if (c != EOF)
  133.         *w++ = c;
  134.  
  135.     if (!isalpha(c) && c!='#' && c!='/'&& c!='_')
  136.     {
  137.         *w = '\0';
  138.         return c;
  139.     }
  140.  
  141.     if( (c=getch())=='/' || c=='*' )
  142.     {
  143.         *w++=c;
  144.         *w='\0';
  145.         return word[0];
  146.     }
  147.     else
  148.         ungetch(c);
  149.  
  150.     for ( ; --lim > 0; w++)
  151.         if (!isalnum(*w = getch()) && *w!='_' && *w!='*' )
  152.         {
  153.             ungetch(*w);
  154.             break;
  155.         }
  156.  
  157.     *w = '\0';
  158.     return word[0];
  159. }
  160.  
  161. #define MAX_ALLOC 100
  162. int alloc[MAX_ALLOC];
  163. int bufp=0;
  164.  
  165. int getch(void)
  166. {
  167.     return (bufp>0) ? alloc[--bufp]:getchar();
  168. }
  169.  
  170. void ungetch(int c)
  171. {
  172.     if(bufp>=MAX_ALLOC)
  173.         printf("No espace to allocate the character\n");
  174.     else
  175.         alloc[bufp++]=c;
  176. }

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: 463

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

Who is online

Users browsing this forum: No registered users and 2 guests