-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathEval.js
152 lines (146 loc) · 5.33 KB
/
mathEval.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
/*
Evaluates a math expression
*/
function mathEval(expression) {
expression = expression.replaceAll(" ", "");
expression = replaceMinus(expression);
let node = convertToNodes(expression);
return node.getValue();
}
/*
Separate all individual parts of expression between operators
*/
function separateOperator(expression, operator) {
let arr = [];
let lastOperator = 0;
let index = expression.indexOf(operator, lastOperator);
while (index != -1) {
if (!insideParenthesis(expression, index)) {
arr.push(expression.substring(lastOperator, index));
lastOperator = index + 1;
index = expression.indexOf(operator, lastOperator);
} else {
index = expression.indexOf(operator, index + 1);
}
}
arr.push(expression.substring(lastOperator, expression.length));
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i][0] == "(" ? arr[i].substring(1, arr[i].length - 1) : arr[i];
}
return arr;
}
/*
Checks if index is inside of parenthesis
*/
function insideParenthesis(expression, index) {
let parenthesis = 0;
if (index > expression.length * 0.5) {
for (var i = index; i < expression.length; i++) {
if (expression[i] == "(") {
parenthesis++;
} else if (expression[i] == ")") {
parenthesis--;
}
}
} else {
for (var i = index; i > 0; i--) {
if (expression[i] == "(") {
parenthesis++;
} else if (expression[i] == ")") {
parenthesis--;
}
}
}
return parenthesis == 0 ? false : true;
}
/*
checks if contains operator inside
*/
function containOperators(expression) {
return !(/^[0-9.,\-]+$/.test(expression));
}
/*
converts string expression into nodes
*/
function convertToNodes(expression) {
var operators = ["+", "*", "/", "^"];
if (containOperators(expression)) {
for (var k = 0; k < operators.length; k++) {
if (includesInLevel(expression, operators[k])) {
let arr = separateOperator(expression, operators[k]);
let node = { node1: convertToNodes(arr[0]), getValue: function() { return this.node1.getValue(); } };
for (var i = 1; i < arr.length; i++) {
if (!containOperators(arr[i])) {
let node2 = { value: parseFloat(arr[i]), getValue: function() { return this.value } };
node = {
node1: node,
node2: node2,
operation: operators[k],
getValue: function() {
switch (this.operation) {
case "+":
return this.node1.getValue() + this.node2.getValue();
case "*":
return this.node1.getValue() * this.node2.getValue();
case "/":
return this.node1.getValue() / this.node2.getValue();
case "^":
return Math.pow(this.node1.getValue(), this.node2.getValue());
}
}
};
} else {
node = {
node1: node,
node2: convertToNodes(arr[i]),
operation: operators[k],
getValue: function() {
switch (this.operation) {
case "+":
return this.node1.getValue() + this.node2.getValue();
case "*":
return this.node1.getValue() * this.node2.getValue();
case "/":
return this.node1.getValue() / this.node2.getValue();
case "^":
return Math.pow(this.node1.getValue(), this.node2.getValue());
}
}
};
}
}
return node;
}
}
} else {
return { value: parseFloat(expression), getValue: function() { return this.value } };
}
}
/*
Checks if has operator in the current level
*/
function includesInLevel(expression, operator) {
let index = expression.indexOf(operator);
while (index != -1) {
if (!insideParenthesis(expression, index)) {
return true;
}
index = expression.indexOf(expression, index + 1);
}
return false;
}
/*
Replacing minus in correct way
*/
function replaceMinus(expression) {
let newExpression = expression[0];
for (var i = 1; i < expression.length - 1; i++) {
if (expression[i] == "-") {
newExpression += expression[i + 1] == "(" ? "+-1*" : (containOperators(expression[i - 1]) ? "-" : "+-");
} else {
newExpression += expression[i];
}
}
newExpression += expression[expression.length - 1];
return newExpression;
}