summaryrefslogtreecommitdiffstats
path: root/4/4.c
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-08 18:41:53 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-08 18:41:53 +0800
commit9d768814f08ff5502fbfb6863fd7507d64983625 (patch)
tree59db4d26a077a066ba3a66aa00031e2cdceae68f /4/4.c
parent6a1918ac8fd0c54a4f81479370e474ccebd56f22 (diff)
downloadk&r-exercises-9d768814f08ff5502fbfb6863fd7507d64983625.tar.gz
4.4
Diffstat (limited to '4/4.c')
-rw-r--r--4/4.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/4/4.c b/4/4.c
index 6112d82..5333ba3 100644
--- a/4/4.c
+++ b/4/4.c
@@ -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;