diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-20 19:29:05 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-20 19:29:05 +0800 |
| commit | ba659edcbf7690d13b3ac86a0f448fb6909c6045 (patch) | |
| tree | 3e62094ba2f989c4cda370c30e279b7a5b832f5b /4/11.c | |
| parent | 4eaf8f943b1cfe68364c3334d09c61ae0d42e41e (diff) | |
| download | k&r-exercises-ba659edcbf7690d13b3ac86a0f448fb6909c6045.tar.gz | |
4.11
Diffstat (limited to '4/11.c')
| -rw-r--r-- | 4/11.c | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -124,6 +124,48 @@ char buf[BUFSIZE]; int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); } +int getop(char s[]) { + int c, i, rc; + static int pc; + + pc = EOF; + + if (pc != EOF) { + c = pc; + pc = EOF; + } else { + while ((s[0] = c = getch()) == ' ' || c == '\t') + ; + s[1] = 0; + } + + // special characters and operators + if (c == 0 || c == '+' || c == '*' || c == '/' || c == '%' || c == '\n' || + (c == '-' && !isdigit(c)) || c == EOF) + return c; + + i = 0; + + if (isdigit(c) || c == '-' || c == '.') { + while (isdigit((s[++i] = c = getch())) || c == '.') + ; + pc = c; + rc = NUM; + } else if (isalpha(c) && getch() == '=') { + s[++i] = c; + rc = '='; + } else { + while (isalnum(s[++i] = c = getch())) + ; + s[i] = 0; + pc = c; + rc = strcmp("LOUT", s) == 0 || strlen(s) == 1 ? VAR : FUN; + } + + s[i] = 0; + return rc; +} + int sp = 0; double stack[HEIGHT]; |
