-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
167 lines (150 loc) · 4.98 KB
/
script.js
File metadata and controls
167 lines (150 loc) · 4.98 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
'use strict'
const buttonsContainer = document.querySelector(".buttons");
const inputDisplay = document.querySelector(".input");
const outputDisplay = document.querySelector(".output");
let equation = '';
function cleanEquation(equation){
equation = equation + '@';
let cleanEquation = [];
const singleDigitOrDecimalRegex = /^[\d.]$/;
const singleSymbolRegex = /[+−÷×]/;
let prev = '';
for (let i = 0; i < equation.length; i++) {
if (singleDigitOrDecimalRegex.test(equation[i])) {
prev += equation[i];
} else if (singleSymbolRegex.test(equation[i])) {
if (prev !== '') {
cleanEquation.push(+prev);
}
cleanEquation.push(equation[i]);
prev = '';
} else if (equation[i] === '(') {
cleanEquation.push(equation[i]);
prev = '';
} else if (equation[i] === ')') {
cleanEquation.push(+prev);
cleanEquation.push(equation[i]);
prev = '';
} else if (equation[i] === '@') {
if (prev !== '') {
cleanEquation.push(+prev);
}
prev = '';
}
}
return cleanEquation;
}
function postfixConversion(cleanEquation) {
let stack = [];
let postfix = [];
const singleSymbolRegex = /[+−÷×]/;
const priority = {
'×': 2,
'÷': 2,
'−': 1,
'+': 1,
'(': 0,
')': 0
}
for (let token of cleanEquation) {
if (typeof token === 'number') {
postfix.push(token);
} else if (singleSymbolRegex.test(token)) {
let lastSymbolPriority = stack.length === 0 ? 0 : priority[stack.at(-1)];
let currSymbolPriority = priority[token];
if (currSymbolPriority > lastSymbolPriority) stack.push(token);
else if (currSymbolPriority <= lastSymbolPriority) {
while (currSymbolPriority <= lastSymbolPriority) {
let symbol = stack.pop();
postfix.push(symbol);
lastSymbolPriority = stack.length === 0 ? 0 : priority[stack.at(-1)];
}
stack.push(token);
}
} else if (token === '(') {
stack.push(token);
} else if (token === ')') {
let symbol = stack.pop();
while (symbol !== '(') {
postfix.push(symbol);
symbol = stack.pop();
}
} else {
console.warn('Unexpected token in cleaned equation:', token);
}
}
while (stack.length > 0) {
let symbol = stack.pop();
if (singleSymbolRegex.test(symbol)) {
postfix.push(symbol);
}
}
return postfix;
}
function calculate(postfixEquation) {
const singleSymbolRegex = /[+−÷×]/;
let stack = [];
while (postfixEquation.length !== 0) {
let token = postfixEquation.shift();
if (typeof token === 'number') stack.push(token);
else if (singleSymbolRegex.test(token)) {
let num1 = stack.pop();
let num2 = stack.pop();
let result = processBinaryEquation(num1, num2, token);
stack.push(result);
}
}
return stack.pop();
}
function processBinaryEquation(num1, num2, operator){
if (operator === '+') return num2 + num1;
else if (operator === '−') return num2 - num1;
else if (operator === '÷') return num2 / num1;
else if (operator === '×') return num2 * num1;
else console.warn('Unexpected operator: ', operator);
}
function input() {
buttonsContainer.addEventListener('click', (event) => {
let target = event.target;
switch (target.id) {
case 'zero':
case 'one':
case 'two':
case 'three':
case 'four':
case 'five':
case 'six':
case 'seven':
case 'eight':
case 'nine':
case 'decimal':
case 'plus':
case 'minus':
case 'divide':
case 'multiply':
case 'opening-bracket':
case 'closing-bracket':
equation += target.textContent;
inputDisplay.textContent = equation;
break;
case 'clear':
equation = '';
inputDisplay.textContent = equation;
outputDisplay.textContent = '';
break;
case 'backspace':
equation = equation.slice(0, -1);
inputDisplay.textContent = equation;
break;
case 'equal':
let cleanerEquation = cleanEquation(equation);
let postfix = postfixConversion(cleanerEquation);
let result = calculate(postfix);
outputDisplay.textContent = result;
break;
default:
console.error("clicked unhandled button of id:", target.id);
}
})
}
input();