summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--4/10.c94
1 files changed, 90 insertions, 4 deletions
diff --git a/4/10.c b/4/10.c
index 00ad493..df12ab1 100644
--- a/4/10.c
+++ b/4/10.c
@@ -1,9 +1,12 @@
#include <ctype.h>
+#include <math.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#define MAXOP 100
#define MAXLINE 1000
+#define HEIGHT 1000
#define NUM '0'
#define FUN '1'
@@ -14,9 +17,10 @@ void push(double);
double peek();
double pop();
void clear();
+double vartoval(char);
int main(int argc, char *argv[]) {
- int type;
+ int type, op1, op2;
char s[MAXOP];
printf("Press CTRL+C to exit\n");
@@ -24,16 +28,58 @@ int main(int argc, char *argv[]) {
while (type == getop(s)) {
switch (type) {
case NUM:
+ push(atof(s));
break;
case '+':
+ op1 = pop();
+ op2 = pop();
+ if (isalpha(op1))
+ op1 = vartoval(op1);
+ if (isalpha(op2))
+ op2 = vartoval(op2);
+ push(op1 + op2);
break;
case '-':
+ op1 = pop();
+ op2 = pop();
+ if (isalpha(op1))
+ op1 = vartoval(op1);
+ if (isalpha(op2))
+ op2 = vartoval(op2);
+ push(op1 - op2);
break;
case '*':
+ op1 = pop();
+ op2 = pop();
+ if (isalpha(op1))
+ op1 = vartoval(op1);
+ if (isalpha(op2))
+ op2 = vartoval(op2);
+ push(op1 * op2);
break;
case '/':
+ op1 = pop();
+ op2 = pop();
+ if (isalpha(op1))
+ op1 = vartoval(op1);
+ if (isalpha(op2))
+ op2 = vartoval(op2);
+ if (op1 == 0.0)
+ printf("error: division by zero\n");
+ else
+ push((double)op2 / op1);
break;
case '%':
+ op1 = pop();
+ op2 = pop();
+ if (isalpha(op1))
+ op1 = vartoval(op1);
+ if (isalpha(op2))
+ op2 = vartoval(op2);
+ if (op1 == 0.0)
+ printf("error: division by zero\n");
+ else
+ push((int)op2 % (int)op1);
break;
case '=':
break;
@@ -52,10 +98,10 @@ int line[MAXLINE];
void mgetline() {
int i, c;
-
+
for (i = 0; i < MAXLINE - 1 && (c = getchar()) != '\n' && c != EOF; i++) {
line[i] = c;
- }
+ }
line[i] = 0;
ptr = 0;
}
@@ -66,7 +112,7 @@ int getop(char s[]) {
if (line[ptr] == 0)
mgetline();
- // get next token
+ // get next token
for (i = 0; i < MAXOP - 1 && (c = line[ptr]) != ' ' && c != 0; i++, ptr++)
s[i] = c;
s[i] = 0;
@@ -81,5 +127,45 @@ int getop(char s[]) {
return s[0];
}
+ if (i > 1 && (isdigit(s[0]) || s[0] == '-' && isdigit(s[1])))
+ return NUM;
+
+ // last printed variable
+ if (strcmp("LOUT", s) == 0)
+ return VAR;
+
return FUN;
+}
+
+int sp = 0;
+double stack[HEIGHT];
+
+void push(double f) {
+ if (sp < HEIGHT)
+ stack[sp++] = f;
+ else
+ printf("error: stack empty\n");
+}
+
+double pop() {
+ if (sp > 0)
+ return stack[--sp];
+ else {
+ printf("error: stack empty\n");
+ return 0.0;
+ }
+}
+
+double peek() {
+ if (sp > 0)
+ return stack[sp - 1];
+ else {
+ printf("error: stack empty\n");
+ return 0.0;
+ }
+}
+
+void clear() {
+ for (; sp > 0; sp--)
+ ;
} \ No newline at end of file