Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable.

#include <stdio.h>
#include <ctype.h>

#define   NUMBER  '0'   /*signal that a number was found */

int getch(void);

/*getop: get next operator or numeric operand  */
int getop(char s[]){
    int c, i;
    static int lastc = 0;

    if(lastc == 0)
      c =getch();
    else{
      c =lastc;
      lastc = 0;
    }
    while((s[0] = c) == ' ' || c == '\t')
           c =getch();
    s[i] = '\0';
   if (!isdigit(c) && c != '.')
         return c;
   i = 0;
   if (isdigit(c))          /* collect integer part */
      while(isdigit(s[++i] = c =getch())
           ;
   s[i] = '\0';
   if (c != EOF)
       lastc =c ;
   return NUMBER;
}

Leave a comment