-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
287 lines (244 loc) · 9.68 KB
/
app.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Functions for basic math operations */
// Addition function
const add = (num1, num2) => num1 + num2;
// Subtraction function
const subtract = (num1, num2) => num1 - num2;
// Multiplication function
const multiply = (num1, num2) => num1 * num2;
// Division function
const divide = (num1, num2) => num1 / num2;
/* Operate function: Performs a mathematical operation on two numbers based on the given operator */
const operate = (operator, num1, num2) => {
let result;
switch (operator) {
case "+":
result = add(num1, num2);
break;
case "-":
result = subtract(num1, num2);
break;
case "×":
result = multiply(num1, num2);
break;
case "÷":
result = divide(num1, num2);
break;
default:
result = "Invalid Operation!";
break;
}
return result;
}
/* Tokenize function: Takes a mathematical expression as a string and tokenizes it, breaking it down into individual elements such as numbers, operators, and parentheses. The resulting array represents the expression in infix notation */
const tokenize = expression => {
const infixNotation = [];
let currentToken = "";
for (const char of expression) {
// Check if the character is a digit or decimal point
if (/\d|\./.test(char)) {
currentToken += char;
}
// Check if the character is an operator or parenthesis
else if (/[\+\-×÷%()]/.test(char)) {
// If there's a current token, push it to infixNotation[]
if (currentToken !== "") {
infixNotation.push(parseFloat(currentToken));
// Preparing the currentToken for the next iteration
currentToken = "";
}
// Push the operator or parenthesis after the currentToken is pushed
infixNotation.push(char);
}
}
// Push the last token in the input expression if it's not an empty string
if (currentToken !== "")
infixNotation.push(parseFloat(currentToken));
return infixNotation;
}
/* ShuntingYardConversion function: Takes an array representing a mathematical expression in infix notation and converts it to postfix notation using the Shunting Yard Algorithm. It uses a stack to manage operators and applies precedence rules to create the postfix expression.
Parameters:
- infixNotation: An array representing the mathematical expression in infix notation.
Returns:
- An array representing the mathematical expression in postfix notation. */
const shuntingYardConversion = infixNotation => {
// Result array for postfix notation
const postfixNotation = [];
// Stack to manage operators during conversion
const operatorsStack = [];
// Precedence rules for different operators
const operators = [
{symbol: "+", precedence: 1, associativity: "left"},
{symbol: "-", precedence: 1, associativity: "left"},
{symbol: "×", precedence: 2, associativity: "left"},
{symbol: "÷", precedence: 2, associativity: "left"},
{symbol: "%", precedence: 3, associativity: "right"},
]
for (const token of infixNotation) {
if (typeof token === "number")
postfixNotation.push(token);
// If the token is an operator or parenthesis
else if (/[\+\-×÷%()]/.test(token)) {
// If the operators stack is empty or the operator is open parenthesis
if (operatorsStack.length === 0 || token === "(")
operatorsStack.push(token);
else if (token === ")")
// Pop operators from the stack and push them to the postfixNotation array until an opening parenthesis is encountered
while (true) {
const lastOperator = operatorsStack.pop();
if (lastOperator === "(")
break;
postfixNotation.push(lastOperator);
}
else {
// Handling precedence rules for operators
const currentOperatorObj = operators.find(op => op.symbol === token);
while (operatorsStack.length > 0) {
const lastOperator = operatorsStack[operatorsStack.length - 1];
const lastOperatorObj = operators.find(op => op.symbol === lastOperator);
if ( lastOperator !== "(" && (lastOperatorObj.precedence > currentOperatorObj.precedence || (lastOperatorObj.precedence === currentOperatorObj.precedence && currentOperatorObj.associativity === "left")))
postfixNotation.push(operatorsStack.pop());
else
break;
}
operatorsStack.push(token);
}
}
}
// Pop any remaining operators from the stack and push them to the postfix notation array
while (operatorsStack.length !== 0)
postfixNotation.push(operatorsStack.pop());
// Return the final postfix notation array
return postfixNotation;
}
/* RpnEvaluation function: Takes an array representing a mathematical expression in postfix notation and evaluates it using the Reverse Polish Notation (RPN) algorithm.
Parameters:
- postfixNotation: An array representing the mathematical expression in postfix notation.
Returns:
- The result of the evaluated mathematical expression. */
const rpnEvaluation = postfixNotation => {
// Operand stack for RPN evaluation
const operandsStack = [];
postfixNotation.forEach((token, index) => {
// If the token is an operator
if (typeof token !== "number") {
// Pop the required operands from the stack
const secondOperand = operandsStack.pop();
const firstOperand = operandsStack.pop();
// If the operator is a % sign
// https://devblogs.microsoft.com/oldnewthing/20080110-00/?p=23853
if (token === "%") {
if (firstOperand !== undefined) {
// Push the first operand back to the operands stack
operandsStack.push(firstOperand);
// If the next operator is (× || ÷) we need to change the behavior of the % sign to not use the 1st operand in the calculation
const nextToken = postfixNotation[index + 1];
if (nextToken === "×" || nextToken === "÷")
operandsStack.push(secondOperand / 100);
// Calculate the percentage using the 1st and 2nd operands and push back the result to the operands stack, when the next operator is (+ || -)
else
operandsStack.push(firstOperand * secondOperand / 100);
}
else
operandsStack.push(secondOperand / 100);
}
// If the operator is (+ || - || × || ÷), apply the operator and push the result back onto the stack
else
operandsStack.push(operate(token,firstOperand,secondOperand));
}
// If the token is an operand, push it onto the stack
else
operandsStack.push(token);
});
// The final result of the RPN evaluation, rounded to avoid floating-point precision issues
return Math.round(operandsStack.pop() * 100) / 100;
}
/* Handling UI Interactions */
const btnsContainer = document.querySelector(".controls");
const expressionField = document.querySelector("#expressionField");
const operators = ["%", "÷", "×", "-", "+"];
const parentheses = ["(", ")"];
const openParenthesis = "(";
const closeParenthesis = ")";
const decimalPoint = ".";
let openParenthesisCounter = 0;
const handleMouseUp = event => {
event.target.classList.remove("btn-radius");
setTimeout(() => {
event.target.classList.remove("btn-overlay");
}, 100);
}
const handleMouseDown = event => {
let btn = event.target;
btn.classList.add("btn-radius");
btn.classList.add("btn-overlay");
btn.addEventListener("mouseup", handleMouseUp);
addToInputField(btn);
}
btnsContainer.addEventListener("mousedown", handleMouseDown);
expressionField.addEventListener("blur", event => event.target.focus());
const addToInputField = btn => {
if (btn.classList.contains("clear"))
handleClearBtnClick(expressionField);
else if(btn.classList.contains("delete"))
handleDeleteBtnClick(expressionField);
else if (btn.classList.contains("digit"))
handleDigitBtnClick(btn.textContent, expressionField);
else if (btn.classList.contains("operator"))
handleOperatorBtnClick(btn.textContent, expressionField);
else if (btn.classList.contains("dicimal-point"))
handleDicimalBtnClick(decimalPoint, expressionField);
else if(btn.classList.contains("parentheses"))
handleParenthesesBtnClick(expressionField);
else if(btn.classList.contains("equals"))
handleEqualsBtnClick(expressionField);
}
// Helper Functions
function handleClearBtnClick(expressionField) {
expressionField.value = '';
}
function handleDeleteBtnClick(expressionField) {
expressionField.value = expressionField.value.slice(0, -1);
}
function handleDigitBtnClick(digit, expressionField) {
expressionField.value += digit;
}
function handleOperatorBtnClick(operator, expressionField) {
const lastCharacter = expressionField.value.slice(-1);
if (lastCharacter === decimalPoint)
return;
else if (expressionField.value === '' || lastCharacter === openParenthesis)
return;
else if ((operators.includes(lastCharacter) && lastCharacter !== '%') || (lastCharacter === '%' && operator === '%')) {
expressionField.value = expressionField.value.slice(0, -1); // Pop
expressionField.value += operator; // Push
}
else
expressionField.value += operator;
}
function handleDicimalBtnClick(dicimalPoint, expressionField) {
const tokenizedExp = expressionField.value.split(/[\+\-\÷\×]/);
const lastToken = tokenizedExp.pop();
const lastCharacter = lastToken.slice(-1);
if (lastToken.includes(dicimalPoint) || lastCharacter === closeParenthesis)
return;
else
expressionField.value += dicimalPoint;
}
function handleParenthesesBtnClick(expressionField) {
const lastCharacter = expressionField.value.slice(-1);
if (expressionField.value === '' || operators.includes(lastCharacter) || lastCharacter === openParenthesis) {
expressionField.value += openParenthesis;
++openParenthesisCounter;
}
else if (openParenthesisCounter > 0) {
expressionField.value += closeParenthesis;
--openParenthesisCounter;
}
}
function handleEqualsBtnClick(expressionField) {
const expression = expressionField.value;
const infixNotation = tokenize(expression);
const postfixNotation = shuntingYardConversion(infixNotation);
const result = rpnEvaluation(postfixNotation);
expressionField.value = result;
}