summaryrefslogtreecommitdiffstats
path: root/4
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-20 19:29:05 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-20 19:29:05 +0800
commitba659edcbf7690d13b3ac86a0f448fb6909c6045 (patch)
tree3e62094ba2f989c4cda370c30e279b7a5b832f5b /4
parent4eaf8f943b1cfe68364c3334d09c61ae0d42e41e (diff)
downloadk&r-exercises-ba659edcbf7690d13b3ac86a0f448fb6909c6045.tar.gz
4.11
Diffstat (limited to '4')
-rw-r--r--4/11.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/4/11.c b/4/11.c
index 54bafcf..010a463 100644
--- a/4/11.c
+++ b/4/11.c
@@ -124,6 +124,48 @@ char buf[BUFSIZE];
int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); }
+int getop(char s[]) {
+ int c, i, rc;
+ static int pc;
+
+ pc = EOF;
+
+ if (pc != EOF) {
+ c = pc;
+ pc = EOF;
+ } else {
+ while ((s[0] = c = getch()) == ' ' || c == '\t')
+ ;
+ s[1] = 0;
+ }
+
+ // special characters and operators
+ if (c == 0 || c == '+' || c == '*' || c == '/' || c == '%' || c == '\n' ||
+ (c == '-' && !isdigit(c)) || c == EOF)
+ return c;
+
+ i = 0;
+
+ if (isdigit(c) || c == '-' || c == '.') {
+ while (isdigit((s[++i] = c = getch())) || c == '.')
+ ;
+ pc = c;
+ rc = NUM;
+ } else if (isalpha(c) && getch() == '=') {
+ s[++i] = c;
+ rc = '=';
+ } else {
+ while (isalnum(s[++i] = c = getch()))
+ ;
+ s[i] = 0;
+ pc = c;
+ rc = strcmp("LOUT", s) == 0 || strlen(s) == 1 ? VAR : FUN;
+ }
+
+ s[i] = 0;
+ return rc;
+}
+
int sp = 0;
double stack[HEIGHT];