summaryrefslogtreecommitdiffstats
path: root/4/4.c
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-08 18:54:13 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-08 18:54:13 +0800
commit3bbc1d91f1679f990eeb01b14772440940840cd0 (patch)
treea088eac6185fe98caf2d72ddd62908a370947d33 /4/4.c
parentf8d523af706aa4dd8c5ff23dfa33f2a7c5e4ef2c (diff)
downloadk&r-exercises-3bbc1d91f1679f990eeb01b14772440940840cd0.tar.gz
4.4
Diffstat (limited to '4/4.c')
-rw-r--r--4/4.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/4/4.c b/4/4.c
index ccea5e8..ed443bf 100644
--- a/4/4.c
+++ b/4/4.c
@@ -28,6 +28,9 @@ double pop();
/* returns a value from stack without removing it */
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) */
int main(int argc, char *argv[]) {
@@ -76,6 +79,9 @@ int main(int argc, char *argv[]) {
push(op2);
push(op1);
break;
+ case 'd':
+ clear();
+ break;
case '\n':
printf("\t%.8g\n", pop());
break;
@@ -112,6 +118,11 @@ double peek() {
}
}
+void clear() {
+ for (; sp > 0; sp--)
+ ;
+}
+
int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); }
void ungetch(int c) {