-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
112 lines (106 loc) · 3.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include "calc.h"
#define MAXOP 100 /* max size of operand or operator */
/* reverse Polish calculator */
main()
{
int type, _last_read_variable = 0, i = 0;
double op2, aux, _lp = 0.0;
char s[MAXOP];
double variables[26]; /* keeps track all variable values */
/* init variables value */
for (i = 0; i < 26; i++)
variables[i] = FAKE_DOUBLE_MIN;
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 divisor\n");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push((int) pop() % (int) op2);
else
printf("error: zero divisor\n");
case 'S':
push(sin(pop()));
break;
case 'E':
push(exp(pop()));
break;
case '^':
op2 = pop();
aux = pop();
if (aux == 0.0 && op2 <= 0.0) {
printf("error: pow(%f, %f) is not a valid operation.\n", aux, op2);
} else {
push(pow(aux, op2));
}
break;
case LAST:
_lp = last();
printf("top value: %f\n", _lp);
break;
case DUPLICATE:
duplicate();
break;
case SWAP:
swap();
break;
case CLEAR:
clear();
break;
case '\n':
_lp = pop();
printf("\t%.8g\n", _lp);
break;
case '_':
push(_lp);
break;
case '=':
variables[_last_read_variable] = pop();
break;
case '?':
/* print all variables that were set */
for (i = 0; i < 26; i++) {
if (variables[i] != FAKE_DOUBLE_MIN)
printf("'%c' == %.8g\n", ('a' + i), variables[i]);
}
break;
default:
/* variable was found */
if (type >= 'a' && type <= 'z') {
_last_read_variable = type - 'a';
/* if variable had a value assigned before, push it to stack */
if (variables[_last_read_variable] != FAKE_DOUBLE_MIN) {
push(variables[_last_read_variable]);
} else {
/* if variable wasn't used before, initialize it to 0 */
variables[_last_read_variable] = 0.0;
}
} else {
printf("error: unknown command %s\n", s);
}
break;
}
}
return 0;
}