summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-12 12:36:15 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-12 12:36:15 +0800
commit2b43df526eede0a57fe38cf99ec21eaa5b310e98 (patch)
treeba0beeb686445ff8c1b9676722a497d605de5199
parentcf4d5f6dc98b1a76fea823a893077d23088989ba (diff)
downloadk&r-exercises-2b43df526eede0a57fe38cf99ec21eaa5b310e98.tar.gz
4.6
-rw-r--r--4/6.c41
1 files changed, 21 insertions, 20 deletions
diff --git a/4/6.c b/4/6.c
index f6892eb..55d5b76 100644
--- a/4/6.c
+++ b/4/6.c
@@ -12,14 +12,14 @@
#define MAXVAL 100 /* max depth of val and var stack */
#define BUFSIZE 100
#define VARCOUNT 52 /* supported variable count */
-#define LASTPRINT 'L' /* variable for most recently printed val */
+#define LPRINT "lout" /* variable for most recently printed val */
#define INDEX(x) (x <= 'Z' ? x - 'A' : x - 71)
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
-double L = 0;
+double lout = 0;
double varv[VARCOUNT]; /* variable value array */
int bufp = 0; /* next free position in buf */
@@ -77,8 +77,8 @@ int main(int argc, char *argv[]) {
break;
case FUNCTION:
if (strcmp("top", s) == 0) {
- L = peek();
- printf("peek top: %.8g\n", L);
+ lout = peek();
+ printf("peek top: %.8g\n", lout);
} else if (strcmp("dup", s) == 0)
push(peek());
else if (strcmp("swp", s) == 0) {
@@ -98,19 +98,15 @@ int main(int argc, char *argv[]) {
printf("error: unknown function %s\n", s);
break;
case ASSIGNMENT:
- if (s[0] == 'L')
- printf("error: reserved variable %c\n", s[0]);
- else {
- i = INDEX(s[0]);
- if (i < 0 || i > VARCOUNT - 1)
- printf("error: unknown symbol %s\n", s);
- else
- varv[i] = pop();
- }
+ i = INDEX(s[0]);
+ if (i < 0 || i > VARCOUNT - 1)
+ printf("error: unknown symbol %s\n", s);
+ else
+ varv[i] = pop();
break;
case VARIABLE:
- if (s[0] == 'L')
- printf("last printed: %.8g\n", L);
+ if (strcmp("lout", s) == 0)
+ push(lout);
else {
i = INDEX(s[0]);
if (i < 0 || i > VARCOUNT - 1)
@@ -120,8 +116,8 @@ int main(int argc, char *argv[]) {
}
break;
case '\n':
- L = pop();
- printf("\t%.8g\n", L);
+ lout = pop();
+ printf("\t%.8g\n", lout);
break;
default:
printf("error: unknown command %s\n", s);
@@ -200,9 +196,14 @@ int getop(char s[]) {
int count;
for (count = 0; isalnum(s[++i] = c = getch()); count++)
;
- if (count == 0) {
- // syntax "5 A =" means "A = 5"
- if (c != '\n' && (nc = getch()) == '=')
+
+ int islout = strncmp("lout", s, 4 * sizeof(char)) == 0;
+
+ if (count == 0 || islout) {
+ if (islout)
+ rv = VARIABLE;
+ else if (c != '\n' && (nc = getch()) == '=')
+ // syntax "5 A =" means "A = 5"
rv = ASSIGNMENT;
else {
if (c != '\n') {