-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
213 lines (179 loc) · 5.07 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
let resultOperations = [];
function humanReadableExprFromArray(expr, start, end) {
let real = '';
for (let i = start; i < end; i++) {
real += ' ' + expr[i] + ' ';
}
return real;
}
function isFunction(str) {
switch (str) {
case 'pow(':
return true;
case 'sin(':
return true;
}
return false;
}
function pow(n) {
return n * n;
}
function sin(n) {
return Math.sin(n);
}
function isOperator(str) {
switch (str) {
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
case '&':
return true;
}
return false;
}
function operatorPriority(str) {
switch (str) {
case '+':
return 3;
case '-':
return 2;
case '*':
return 1;
case '/':
return 0;
case '&':
return 4;
}
return false;
}
function evalOperand(operand, p1, p2, pos) {
switch (operand) {
case '+':
return p1 + p2;
case '-':
return p1 - p2;
case '*':
return p1 * p2;
case '/':
return p1 / p2;
case '&':
return p1 & p2;
}
throw new Error(`EvalOperandError: unknown operand ${operand} at ${pos}`);
}
function evalUnary(operand, p) {
switch (operand) {
case '+':
return p;
case '-':
return -p;
}
throw new Error(`EvalOperandError: unknown unary operand ${operand} at ${pos}`);
}
function evalFunction(e, start, end) {
switch (e[start]) {
case 'pow(':
resultOperations.push(`pow`);
return pow(myeval(e, start + 1, end - 1));
case 'sin(':
resultOperations.push(`sin`);
return sin(myeval(e, start + 1, end - 1));
}
throw new Error(`EvalFunctionError: unknown function ${e[start]}`);
}
function myeval(e, start, end) {
//debug
console.log('eval ', start, end, ':::', humanReadableExprFromArray(e, start, end));
if ((end - start) < 1) {
return 0;
}
// if it is a single value return it
if ((end - start) <= 1) {
resultOperations.push(e[start]);
return e[start];
}
if (isOperator(e[start]) && (end - start) == 2) {
resultOperations.push(e[start] + '!');
return evalUnary(e[start], myeval(e, start + 1, end));
}
// if the whole expression is in brackets remove them
if ((e[start] === '(' || isFunction(e[start])) && e[end - 1] === ')') {
let open = false;
let opened = 0;
for (let i = start + 1; i < end - 1; i++) {
if (e[i] === '(' || isFunction(e[i])) {
opened++;
}
if (e[i] === ')') {
opened--;
}
if (opened < 0) {
open = true;
break;
}
}
if (open === false) {
if (isFunction(e[start])) {
return evalFunction(e, start, end);
} else {
return myeval(e, start + 1, end - 1);
}
}
}
// find the most priority operator outside of brackets
let maxOperatorPosition = -1;
let maxOperatorPriority = -1;
let openedBrackets = 0;
for (let i = start; i < end; i++) {
if (e[i] === '(' || isFunction(e[i])) {
if (maxOperatorPosition >= 0) {
break;
}
openedBrackets++;
}
if (e[i] === ')') {
openedBrackets--;
}
if (openedBrackets > 0) {
continue;
}
let type = typeof e[i];
if (type === 'string') {
if (isOperator(e[i])) {
if (i > start && isOperator(e[i - 1])) {
continue;
}
let priority = operatorPriority(e[i]);
if (priority >= maxOperatorPriority) {
maxOperatorPosition = i;
maxOperatorPriority = priority;
}
}
}
}
//console.log('op:', e[maxOperatorPosition], maxOperatorPosition);
resultOperations.push(e[maxOperatorPosition]);
return evalOperand(e[maxOperatorPosition], myeval(e, start, maxOperatorPosition), myeval(e, maxOperatorPosition + 1, end), maxOperatorPosition);
}
try {
let expr = ['pow(', '(', 1, '+', 'sin(', 1, '+', 2, ')', ')', '*', 2, ')'];
console.log('expression:', humanReadableExprFromArray(expr, 0, expr.length));
console.log('real true value:', eval(humanReadableExprFromArray(expr, 0, expr.length)));
console.log('evaluated: ', myeval(expr, 0, expr.length));
console.log('--- operations to solve the expression:');
let str = '';
for (let i = 0; i < resultOperations.length; i++) {
str += resultOperations[i] + ' ';
}
console.log(str);
} catch (err) {
console.log('ERROR', err.message);
console.log('-----------');
console.log('operations:');
console.log(resultOperations);
}