[Code example] undcl page 126

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

undcl page 126

Post by BJT » Sat May 13, 2017 7:33 pm

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define MAXTOKEN 100
  6.  
  7. enum { NAME, PARENS, BRACKETS };
  8. int gettoken(void);
  9. char token[MAXTOKEN];   /*last token string */
  10. char out[1000];
  11. int tokentype;          /*type of last token */
  12.  
  13. int main() /* convert declaration to words */
  14. {
  15.     int type;
  16.     char temp[MAXTOKEN];
  17.  
  18.     while (gettoken() != EOF)
  19.     {
  20.         strcpy(out, token);
  21.         while ((type = gettoken()) != '\n')
  22.         {
  23.             if (type == PARENS || type == BRACKETS)
  24.             strcat(out, token);
  25.             else if (type == '*')
  26.             {
  27.                 sprintf(temp, "(*%s)", out);
  28.                 strcpy(out, temp);
  29.             }
  30.             else if (type == NAME)
  31.             {
  32.                 sprintf(temp, "%s %s", token, out);
  33.                 strcpy(out, temp);
  34.             }
  35.             else
  36.                 printf("invalid input at %s\n", token);
  37.         }
  38.  
  39.     }
  40.     printf("%s\n", out);
  41.     return 0;
  42. }
  43.  
  44. int gettoken(void) /* return next token */
  45. {
  46.     int c, getch(void);
  47.     void ungetch(int);
  48.     char *p = token;
  49.     while ((c = getch()) == ' ' || c == '\t')
  50.         ;
  51.     if (c == '(')
  52.     {
  53.         if ((c = getch()) == ')')
  54.         {
  55.             strcpy(token, "()");
  56.             return tokentype = PARENS;
  57.         }
  58.         else
  59.         {
  60.             ungetch(c);
  61.             return tokentype = '(';
  62.         }
  63.     }
  64.     else if (c == '[')
  65.     {
  66.         for (*p++ = c; (*p++ = getch()) != ']'; )
  67.             ;
  68.         *p = '\0';
  69.         return tokentype = BRACKETS;
  70.     }
  71.     else if (isalpha(c))
  72.     {
  73.         for (*p++ = c; isalnum(c = getch()); )
  74.             *p++ = c;
  75.         *p = '\0';
  76.         ungetch(c);
  77.         return tokentype = NAME;
  78.     }
  79.     else
  80.         return tokentype = c;
  81. }
  82.  
  83. #define BUFSIZE 100
  84. char buf[BUFSIZE]; /* buffer for ungetch */
  85. int bufp = 0;      /* next free position in buf */
  86.  
  87. int getch(void) /* get a (possibly pushed-back) character */
  88. {
  89.     return (bufp > 0) ? buf[--bufp] : getchar();
  90. }
  91.  
  92. void ungetch(int c)
  93. /* push character back on input */
  94. {
  95.     if (bufp >= BUFSIZE)
  96.         printf("ungetch: too many characters\n");
  97.     else
  98.         buf[bufp++] = c;
  99. }

input

Code: Select all

x () * [] * () char
output

Code: Select all

char (*(*x())[])()
word count: 244

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

Who is online

Users browsing this forum: No registered users and 1 guest