Calls getch.c to get an operator or an operand, put into the string s, which is supplied from the main program.
  • Skip white spaces.
  • If read in a non-digit or non-dot, assume it is a one-letter operator. The string s has one character. And the return integer is the ASCII code of that character.
  • If read in a digit, read all until a non-digit or non-dot appears. Put the digits into s, return a special integer NUMBER (which cannot be a printable ASCII character). Put the extra character back to buffer.

  • #include <stdio.h>
    #include <ctype.h>
    #include "getch.h"
    
    #define NUMBER '\007'
    
    int getop(char s[]) {
        int i, c;
    
        while ((s[0] = c = getch()) == ' ' || c == '\t');
    
        s[1] = '\0';
        if (!isdigit(c) && c != '.')
    	return c;
        
        i = 0;
        if (isdigit(c))
    	while (isdigit(s[++i] = c = getch()));
        if (c == '.')
    	while (isdigit(s[++i] = c = getch()));
        s[i] = '\0';
        if (c != EOF)
    	ungetch(c);
        return NUMBER;
    }
    

    Created: Nov 26, 1994
    Last Revised: Dec 6, 1994
    © Copyright 1994 Wei-Chang Shann

    shann@math.ncu.edu.tw