diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-08 18:41:53 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-08 18:41:53 +0800 |
| commit | 9d768814f08ff5502fbfb6863fd7507d64983625 (patch) | |
| tree | 59db4d26a077a066ba3a66aa00031e2cdceae68f /4 | |
| parent | 6a1918ac8fd0c54a4f81479370e474ccebd56f22 (diff) | |
| download | k&r-exercises-9d768814f08ff5502fbfb6863fd7507d64983625.tar.gz | |
4.4
Diffstat (limited to '4')
| -rw-r--r-- | 4/4.c | 17 |
1 files changed, 9 insertions, 8 deletions
@@ -28,8 +28,8 @@ double pop(); /* returns a value from stack without removing it */ double peek(); -/* reverse Polish calculator with support for modulus operator and negative - * numbers */ +/* adds commands p (print top val without popping), c (duplicate top val), s + * (swap top two stack positions), d (clear stack) */ int main(int argc, char *argv[]) { int type; double op2; @@ -66,7 +66,10 @@ int main(int argc, char *argv[]) { break; case 'p': peek(); - break; + break; + case 'c': + push(peek()); + break; case '\n': printf("\t%.8g\n", pop()); break; @@ -96,16 +99,14 @@ double pop() { double peek() { if (sp > 0) - return val[sp]; + return val[sp - 1]; else { printf("error: stack empty\n"); return 0.0; } } -int getch() { - return (bufp > 0) ? buf[--bufp] : getchar(); -} +int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { if (bufp >= BUFSIZE) @@ -121,7 +122,7 @@ int getop(char s[]) { ; s[1] = 0; - if (!isdigit(c) && c != '.' && c != '-') + if (!isdigit(c) && c != '.' && c != '-') return c; i = 0; |
