-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (123 loc) · 3.88 KB
/
script.js
File metadata and controls
144 lines (123 loc) · 3.88 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
let currentInput = "";
const display = document.getElementById("display");
function updateDisplay(value) {
display.innerText = value || "0";
}
function appendNumber(num) {
// Only allow one decimal
if (num === '.' && currentInput.endsWith('.')) return;
currentInput += num;
updateDisplay(currentInput);
}
function appendOperator(op) {
if (currentInput === "" && op !== "(") return;
currentInput += ` ${op} `;
updateDisplay(currentInput);
}
function clearDisplay() {
currentInput = "";
updateDisplay("0");
}
function calculate() {
try {
const tokens = tokenize(currentInput);
if (!tokens) return;
const rpn = shuntingYard(tokens);
const result = evaluateRPN(rpn);
if (isNaN(result) || !isFinite(result)) {
throw new Error("Math Error");
}
updateDisplay(result);
currentInput = result.toString();
} catch (e) {
updateDisplay("ERROR");
currentInput = "";
}
}
function toggleSign() {
if (currentInput === "" || currentInput === "0") return;
let tokens = currentInput.trim().split(" ");
let lastToken = tokens[tokens.length - 1];
if (!isNaN(lastToken) && lastToken !== "") {
if (lastToken.startsWith("-")) {
tokens[tokens.length - 1] = lastToken.substring(1);
} else {
tokens[tokens.length - 1] = "-" + lastToken;
}
currentInput = tokens.join(" ");
updateDisplay(currentInput);
}
}
function tokenize(str) {
return str.match(/-?\d+\.?\d*|[+\-*/()]/g); // RegEx
}
function shuntingYard(tokens) {
const outputQueue = [];
const operatorStack = [];
const precedence = { '+': 1, '-': 1, '*': 2, '/': 2 };
tokens.forEach(token => {
if (!isNaN(parseFloat(token))) {
outputQueue.push(token);
} else if (token === '(') {
operatorStack.push(token);
} else if (token === ')') {
while (operatorStack.length && operatorStack[operatorStack.length - 1] !== '(') {
outputQueue.push(operatorStack.pop());
}
operatorStack.pop(); // Remove '('
} else {
while (operatorStack.length &&
operatorStack[operatorStack.length - 1] !== '(' &&
precedence[operatorStack[operatorStack.length - 1]] >= precedence[token]) {
outputQueue.push(operatorStack.pop());
}
operatorStack.push(token);
}
});
while (operatorStack.length) {
outputQueue.push(operatorStack.pop());
}
return outputQueue;
}
function evaluateRPN(tokens) {
const stack = [];
tokens.forEach(token => {
if (!isNaN(token)) {
stack.push(parseFloat(token));
} else {
const b = stack.pop();
const a = stack.pop();
switch (token) {
case '+': stack.push(a + b); break;
case '-': stack.push(a - b); break;
case '*': stack.push(a * b); break;
case '/':
if (b === 0) throw new Error("DivByZero");
stack.push(a / b);
break;
}
}
});
return stack[0];
}
// Keyboard Support (Recommended)
document.addEventListener('keydown', (event) => {
const key = event.key;
if (/[0-9]/.test(key)) {
appendNumber(key);
} else if (['+', '-', '*', '/'].includes(key)) {
appendOperator(key);
} else if (key === '(' || key === ')') {
appendOperator(key);
} else if (key === 'Enter' || key === '=') {
event.preventDefault();
calculate();
} else if (key === 'Escape') {
clearDisplay();
} else if (key === '.') {
appendNumber('.');
} else if (key === 'Backspace') {
currentInput = currentInput.trim().slice(0, -1);
updateDisplay(currentInput);
}
});