-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (89 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
100 lines (89 loc) · 2.76 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
const display = document.getElementById('display');
let lastAnswer = '';
let memory = 0;
let angle = 'DEG'; // or 'RAD'
let expInputMode = false;
// For basic functions
function appendValue(val) {
if (display.value === 'Error') display.value = '';
if (val === 'EXP') {
display.value += 'E';
expInputMode = true;
return;
}
// Proper input format fixes for fraction, reciprocal, powers, 10^x, e^x
if (val === '1/') {
display.value += '1/(';
return;
}
if (val === '10^') {
display.value += '10^';
return;
}
display.value += val;
}
function clearDisplay() {
display.value = '';
expInputMode = false;
}
function deleteChar() {
display.value = display.value.slice(0, -1);
}
function calculate() {
let expr = display.value.trim();
if (!expr) return;
// Handle Ans
expr = expr.replace(/Ans/g, lastAnswer || '0');
// Handle degree/radian for trig
expr = expr.replace(/sin\(/g, angle === 'DEG' ? 'sin(degToRad(' : 'sin(');
expr = expr.replace(/cos\(/g, angle === 'DEG' ? 'cos(degToRad(' : 'cos(');
expr = expr.replace(/tan\(/g, angle === 'DEG' ? 'tan(degToRad(' : 'tan(');
expr = expr.replace(/asin\(/g, angle === 'DEG' ? 'radToDeg(asin(' : 'asin(');
expr = expr.replace(/acos\(/g, angle === 'DEG' ? 'radToDeg(acos(' : 'acos(');
expr = expr.replace(/atan\(/g, angle === 'DEG' ? 'radToDeg(atan(' : 'atan(');
// Custom conversion functions for math.js
function degToRad(x) { return x * Math.PI / 180; }
function radToDeg(x) { return x * 180 / Math.PI; }
try {
const scope = { pi: Math.PI, e: Math.E, Ans: lastAnswer, degToRad, radToDeg };
let result = math.evaluate(expr, scope);
lastAnswer = result;
display.value = result;
} catch (err) {
display.value = 'Error';
}
}
// Scientific memory functions
function handleFn(fn) {
if (fn === 'MC') memory = 0;
else if (fn === 'MR') display.value += memory;
else if (fn === 'M+') memory += parseFloat(display.value || '0');
else if (fn === 'M-') memory -= parseFloat(display.value || '0');
else if (fn === 'Ans') display.value += lastAnswer;
}
// Angle mode switch
function angleMode() {
angle = angle === 'DEG' ? 'RAD' : 'DEG';
alert(`Angle mode: ${angle}`);
}
// Trig/grouped handler (for UI clarity)
function handleTrig(fn) {
appendValue(fn + '(');
}
// Random number insert
function randomNumber() {
display.value += Math.random();
}
// Keyboard support
document.addEventListener('keydown', (e) => {
if ((e.key >= '0' && e.key <= '9') || ['+', '-', '*', '/', '.', '(', ')', '^', 'E', 'e'].includes(e.key)) {
appendValue(e.key);
} else if (e.key === 'Enter' || e.key === '=') {
calculate();
e.preventDefault();
} else if (e.key === 'Backspace') {
deleteChar();
} else if (e.key.toLowerCase() === 'c') {
clearDisplay();
}
});