diff options
author | Michael Hunteman <michael@huntm.net> | 2023-07-04 17:03:53 -0500 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2023-07-06 17:23:45 -0500 |
commit | bfce8f0d0d828209ec0bec71371ee94a7ad62d3e (patch) | |
tree | bdf49ca788ca1ca030d5b1cccfd0c9dffeb3f69f /four/getop.c |
Initial commit
Diffstat (limited to 'four/getop.c')
-rw-r--r-- | four/getop.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/four/getop.c b/four/getop.c new file mode 100644 index 0000000..8870895 --- /dev/null +++ b/four/getop.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <ctype.h> + +#define NUMBER '0' + +int getch(void); +void ungetch(int); + +/* get next operator or numeric operand */ +int +getop(char s[]) +{ + int i, c; + + while((s[0] = c = getch()) == ' ' || c == '\t'); + s[1] = '\0'; + /* not a number */ + if (!isdigit(c) && c != '.') { + return c; + } + i = 0; + /* collect integer part */ + if (isdigit(c)) { + while (isdigit(s[++i] = c = getch())); + } + /* collect fraction part */ + if (c == '.') { + while (isdigit(s[++i] = c = getch())); + } + s[i] = '\0'; + if (c != EOF) { + ungetch(c); + } + return NUMBER; +} |