-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
180 lines (161 loc) · 6.21 KB
/
shared.js
File metadata and controls
180 lines (161 loc) · 6.21 KB
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
$(document).ready(function() {
var shared = {};
shared.init = function() {
let a = shared.calculate("3 + 4 * 2 / (( 1 - 5 ) ^ 2) ^ 3");
console.log(a);
let b = shared.calculate("2 / 0.5 * (6000 - 4000 / 40) ^ 2");
console.log(b);
$('#calc').on("click", shared.onClick);
};
shared.calculate = function(expression) {
// Use Shunting-yard algorithm to transform infix expression to postfix notation.
// This will remove the need for parenthesis when evaluation the expression
const calc = function(expression) {
try {
let expr = splitExpression(expression);
expr = lookupValues(expr);
expr = infixToPostfix(expr);
return evaluatePostfixExpression(expr);
}
catch(err) {
return NaN;
}
};
// Split expression string into an array of operators, operands, and parenthesis
// keeping the order that they appear.
// For Example: "#c * (#a + #b)" -> ['#c', '*', '(', '#a', '+', '#b', ')']
const splitExpression = function(expr) {
let array = [];
let i = 0;
let token = '';
const splitValues = ['^', '*', '/', '+', '-', '(', ')'];
while (i < expr.length) {
let exists = splitValues.includes(expr.charAt(i));
if(exists && token.trim() !== '') {
array.push(token.trim());
token = '';
}
token += expr.charAt(i);
if (exists) {
array.push(token.trim());
token = '';
}
i++;
}
if(token.trim() !== '') {
array.push(token.trim());
}
return array;
};
const lookupValues = function(array) {
let i = 0;
while(i < array.length) {
if(array[i].charAt(0) === '#') {
let inputId = $(array[i]);
if(inputId.length > 0 && $.isNumeric(inputId.val())) {
array[i] = inputId.val();
}
else {
array[i] = 0;
}
}
i++;
}
return array;
};
// Shunting-yard algorithm
// https://en.wikipedia.org/wiki/Shunting-yard_algorithm
const infixToPostfix = function(tokens) {
let outputQueue = [];
let operatorStack = [];
while(tokens.length > 0) {
let token = tokens.shift();
if($.isNumeric(token)) {
outputQueue.push(token);
}
else if (token in operators) {
while(loop(token, operatorStack[operatorStack.length-1])) {
outputQueue.push(operatorStack.pop());
}
operatorStack.push(token);
}
else if (token === '(') {
operatorStack.push(token);
}
else if (token === ')') {
while(operatorStack[operatorStack.length-1] !== '(') {
outputQueue.push(operatorStack.pop());
}
if(operatorStack[operatorStack.length-1] === '(') {
operatorStack.pop();
}
}
}
while(operatorStack.length > 0) {
outputQueue.push(operatorStack.pop());
}
return outputQueue;
};
// Returns true if:
// (the operator at the top of the operator stack is not a left parenthesis) AND
// ((there is an operator at the top of the operator stack with greater precedence) OR
// (the operator at the top of the operator stack has equal precedence and is left associative))
const loop = function(token, topStack) {
if(topStack === null || topStack === undefined)
return false;
let tokenInfo = operators[token];
let topStackInfo = operators[topStack];
if(topStack !== '(' &&
(topStackInfo.precedence > tokenInfo.precedence ||
(topStackInfo.precedence === tokenInfo.precedence && topStackInfo.associativity === 'left'))) {
return true;
}
return false;
};
const evaluatePostfixExpression = function(queue) {
let stack = [];
while(queue.length > 0) {
let token = queue.shift();
if($.isNumeric(token)) {
stack.push(token);
}
else {
let a = parseFloat(stack.pop());
let b = parseFloat(stack.pop());
switch(token) {
case '^':
stack.push(Math.pow(b, a));
break;
case '*':
stack.push(b * a)
break;
case '/':
if(a === 0) throw "divide by zero";
stack.push(b / a);
break;
case '+':
stack.push(b + a);
break;
case '-':
stack.push(b - a);
break;
}
}
}
return stack.pop();
};
const operators = {
'^': { precedence: 4, associativity: 'right' },
'*': { precedence: 3, associativity: 'left' },
'/': { precedence: 3, associativity: 'left' },
'+': { precedence: 2, associativity: 'left' },
'-': { precedence: 2, associativity: 'left' }};
return calc(expression);
};
shared.onClick = function() {
let answer = shared.calculate("10+#a+(#b-#c)*#d");
console.log(answer);
$('#value').val(answer);
}
shared.init();
});