Exercises 4-11


#include <ctype.h>
   int getch(void);
   void ungetch(int);
   /* getop:  get next character or numeric operand */
   int getop(char s[])
   {
   	   static int lastc=0;
       int i, c;
       if(lastc==0)
       {
       	c=getchar();
       }
       else
       {
       	c=lastc;
       	lastc=0;
       }
       while ((s[0] = c ) == ' ' || c == '\t')
           c=getchar();
       s[1] = '\0';
       if (!isdigit(c) && c != '.')
           return c;      /* not a number */
       i = 0;
       if (isdigit(c))    /* collect integer part */
           while (isdigit(s[++i] = c = getch()))
              ;
       if (c == '.')      /* collect fraction part */
           while (isdigit(s[++i] = c = getch()))
              ;
       s[i] = '\0';
       if (c != EOF)
           lastc=c;
       return NUMBER;
   }
思路:用lastc静态变量存储最后一个字符,下次再调用此函数就可以从lastc变量中取,因为它是静态变量。

你可能感兴趣的:(exe)