diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-10 19:41:31 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-10 19:41:31 +0800 |
| commit | a34e1ed98dd2dd22721f2dd6fa5af06f771b403a (patch) | |
| tree | 2c5aaeef9cfe05957fe64d2b77bdbb062d5ae85f /4 | |
| parent | 5dd75358262f5bf0fa7b9ef9828f5f83aaf7ef47 (diff) | |
| download | k&r-exercises-a34e1ed98dd2dd22721f2dd6fa5af06f771b403a.tar.gz | |
4.4
Diffstat (limited to '4')
| -rw-r--r-- | 4/4.c | 36 | ||||
| -rw-r--r-- | 4/5.c | 1 |
2 files changed, 23 insertions, 14 deletions
@@ -4,6 +4,7 @@ #define MAXOP 100 /* max size of operand or operator */ #define NUMBER '0' /* signal that a number was found */ +#define COP '1' /* signal that a custom op was found */ #define MAXVAL 100 /* max depth of val stack */ #define BUFSIZE 100 @@ -28,8 +29,8 @@ double peek(); /* clears the stack */ void clear(); -/* adds commands p (print top val without popping), c (duplicate top val), s - * (swap top two stack positions), d (clear stack) */ +/* adds commands peek (print top val without popping), dup (duplicate top val), + * swap (swap top two stack positions), clear (clear stack) */ int main(int argc, char *argv[]) { int type; double op2, op1; @@ -130,29 +131,38 @@ void ungetch(int c) { } int getop(char s[]) { - int i, c; + int i, c, rv; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = 0; - if (!isdigit(c) && c != '.' && c != '-') + // operators or line feed + if (c == '\n' || c == '+' || c == '*' || c == '/' || c == '%') return c; i = 0; - if (isdigit(c)) - while (isdigit(s[++i] = c = getch())) - ; - if (c == '.') - while (isdigit(s[++i] = c = getch())) - ; - if (c == '-') - while (isdigit(s[++i] = c = getch())) + + // numbers + if (isdigit(c) || c == '-') { + if (c == '-') { + // minus operator + if (!isdigit(s[++i] = c = getch())) { + ungetch(c); + s[i] = 0; + return '-'; + } + } + + while (isdigit(s[++i] = c = getch()) || c == '.') ; + rv = NUMBER; + } + s[i] = 0; if (c != EOF) ungetch(c); - return NUMBER; + return rv; } @@ -1,4 +1,3 @@ - #include <ctype.h> #include <math.h> #include <stdio.h> |
