summaryrefslogtreecommitdiff
path: root/ex4-3.c
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2022-11-21 21:28:36 +0000
committerMartin Ashby <martin@ashbysoft.com>2022-11-21 21:28:36 +0000
commit7fa502ba4e60ce762ab529996c66a1a831cb789f (patch)
treea9520fc309cb63c1c33cfd0be67f61c1cc0e9060 /ex4-3.c
parentcc66f02855428590fa4172aa6a8b88fa0a8109bf (diff)
downloadlearn-c-7fa502ba4e60ce762ab529996c66a1a831cb789f.tar.gz
learn-c-7fa502ba4e60ce762ab529996c66a1a831cb789f.tar.bz2
learn-c-7fa502ba4e60ce762ab529996c66a1a831cb789f.tar.xz
learn-c-7fa502ba4e60ce762ab529996c66a1a831cb789f.zip
Switch getch implementation for getline instead
Diffstat (limited to 'ex4-3.c')
-rw-r--r--ex4-3.c129
1 files changed, 49 insertions, 80 deletions
diff --git a/ex4-3.c b/ex4-3.c
index ca5739a..78e0944 100644
--- a/ex4-3.c
+++ b/ex4-3.c
@@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
+#include <string.h>
#define NUMBER '0'
-#define MAXOP 100
+#define MAXLINE 1000
#define MAXVAL 100
int getop(char[]);
@@ -22,48 +23,54 @@ double frem(double f1, double f2) {
* Reverse polish notation calculator
*/
int main(void) {
- int type;
double op2;
- char s[MAXOP];
- while ((type = getop(s)) != EOF) {
- switch (type) {
- case NUMBER:
- push(atof(s));
- break;
- case '+':
- push(pop() + pop());
- break;
- case '*':
- push(pop() * pop());
- break;
- case '-':
- op2 = pop();
- push(pop() - op2);
- break;
- case '/':
- op2 = pop();
- if (op2 != 0.0)
- push(pop() / op2);
- else
- printf("error: zero division attempted!\n");
- break;
- case '%':
- op2 = pop();
- if (op2 != 0.0)
- push(frem(pop(), op2));
- else
- printf("error: zero division attempted!\n");
- break;
- case '~':
- clear();
- case '\n':
- printf("\t%.8g\n", peek());
- break;
- default:
- printf("unknown command %s\n", s);
- break;
- }
+ char* line = NULL;
+ size_t line_len = 0;
+ while (getline(&line, &line_len, stdin) != -1) {
+ char* tok = strtok(line, " ");
+ do {
+ int type = getop(tok);
+ switch (type) {
+ case NUMBER:
+ push(atof(tok));
+ break;
+ case '+':
+ push(pop() + pop());
+ break;
+ case '*':
+ push(pop() * pop());
+ break;
+ case '-':
+ op2 = pop();
+ push(pop() - op2);
+ break;
+ case '/':
+ op2 = pop();
+ if (op2 != 0.0)
+ push(pop() / op2);
+ else
+ printf("error: zero division attempted!\n");
+ break;
+ case '%':
+ op2 = pop();
+ if (op2 != 0.0)
+ push(frem(pop(), op2));
+ else
+ printf("error: zero division attempted!\n");
+ break;
+ case '~':
+ clear();
+ break;
+ default:
+ //printf("unknown command %s\n", tok); // Just ignore it?
+ break;
+ }
+ } while ((tok = strtok(NULL, " ")) != NULL);
+
+ printf("\t%.8g\n", pop());
}
+ free(line); // or, don't bother since we exit the program next...
+
return 0;
}
@@ -99,47 +106,9 @@ void clear(void) {
sp = 0;
}
-int getch(void);
-void ungetch(int);
-
int getop(char s[]) {
- int i,c;
- // Skip to teh first non-space or tab character
- while ((s[0] = c = getch()) == ' ' || c == '\t');
- // Then add a null for some reason?
- s[1] = '\0';
- // if it's not a part of a number, it's an operation
+ char c = s[0];
if (!isdigit(c) && c != '.')
return c;
- // Integer part
- i = 0;
- if (isdigit(c))
- while (isdigit(s[++i] = c = getch()));
- // fraction part
- if (c == '.')
- while (isdigit(s[++i] = c = getch()));
- s[i] = '\0';
- if (c != EOF)
- ungetch(c);
return NUMBER;
}
-
-#define BUFSIZE 100
-
-int buf = -2;
-
-int getch(void) {
- if (buf != -2) {
- int z = buf;
- buf = -2;
- return z;
- }
- return getchar();
-}
-
-void ungetch(int c) {
- if (buf != -2)
- printf("ungetch: buffer full\n");
- else
- buf = c;
-} \ No newline at end of file