-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalculator.html
More file actions
834 lines (735 loc) · 34.9 KB
/
Copy pathCalculator.html
File metadata and controls
834 lines (735 loc) · 34.9 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
<title>Academic Pro Calculator | Signal Share Suite</title>
<link rel="stylesheet" href="./android-optimizations.css?v=1.5">
<link rel="stylesheet" href="./questions-guide.css?v=1.3">
<script>
// Signal Share Android Optimization Suite - Detection
(function() {
const isNative = !!window.Capacitor && typeof window.Capacitor.getPlatform === 'function' && window.Capacitor.getPlatform() !== 'web';
if (isNative || /Android/i.test(navigator.userAgent)) {
document.documentElement.classList.add('platform-android');
document.documentElement.classList.add('is-signal-app');
}
})();
</script>
<style>
:root {
--bg: #08080b;
--surface: rgba(255, 255, 255, 0.03);
--border: rgba(255, 255, 255, 0.1);
--accent: #66c0f4;
--accent-glow: rgba(102, 192, 244, 0.3);
--danger: #ff3e3e;
--success: #a4d007;
--glass: rgba(255, 255, 255, 0.05);
}
body {
background: var(--bg);
color: #fff;
margin: 0;
font-family: 'Inter', system-ui, sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
height: 100dvh;
overflow: hidden;
}
.calculator-shell {
display: flex;
flex: 1;
overflow: hidden;
min-height: 0;
width: 100%;
}
@media (max-width: 900px) {
body {
flex-direction: column;
overflow: hidden;
}
.runner-header {
padding: 10px 12px;
}
.runner-title {
font-size: 0.8rem;
}
.close-runner {
padding: 6px 10px;
font-size: 0.65rem;
}
.calculator-shell {
flex-direction: column;
overflow-y: auto;
overflow-x: hidden;
}
.sidebar {
width: 100%;
border-right: none;
border-bottom: 1px solid var(--border);
padding: 8px 10px;
box-sizing: border-box;
min-height: auto;
height: auto;
gap: 8px;
display: flex;
flex-wrap: nowrap;
align-items: center;
overflow-x: auto;
overflow-y: hidden;
scrollbar-width: none;
}
.sidebar::-webkit-scrollbar {
display: none;
}
.sidebar > div:first-child,
.nav-label {
display: none;
}
.nav-group {
flex-direction: row;
gap: 8px;
flex: 0 0 auto;
}
.nav-item {
flex: 0 0 auto;
padding: 7px 11px;
border-radius: 999px;
font-size: 0.7rem;
letter-spacing: 0.4px;
border: 1px solid rgba(255,255,255,0.12);
color: rgba(255,255,255,0.75);
}
.nav-item.active {
border-left: 1px solid rgba(102, 192, 244, 0.55);
background: rgba(102, 192, 244, 0.18);
box-shadow: none;
}
.workspace {
padding: 12px;
overflow: visible;
}
.calc-wrapper {
grid-template-columns: 1fr;
gap: 12px;
max-width: 100%;
min-height: auto;
}
.display-container {
padding: 18px;
border-radius: 16px;
}
.mode-indicator {
top: 12px;
left: 18px;
}
#memory-badge {
top: 12px !important;
right: 18px !important;
}
.main-line {
font-size: 2rem;
}
.controls-grid {
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: 8px;
}
.btn {
min-height: 48px;
padding: 10px 8px;
font-size: 0.92rem;
}
.btn.function {
font-size: 0.72rem;
}
.btn.equals {
font-size: 1.2rem;
}
.history-panel {
padding: 14px;
border-radius: 16px;
min-height: 200px;
}
.history-list {
margin-top: 12px;
gap: 12px;
}
.converter-panel {
padding: 16px;
border-radius: 16px;
gap: 16px;
}
.conv-row {
grid-template-columns: 1fr;
gap: 14px;
}
.conv-row > div:nth-child(2) {
display: none;
}
.conv-input-box input {
font-size: 1.1rem;
padding: 12px;
}
}
/* Override generic Android app-shell clamp for this calculator layout */
html.platform-android.is-signal-app body.calculator-body {
align-items: stretch !important;
justify-content: flex-start !important;
max-width: none !important;
width: 100% !important;
height: 100dvh !important;
}
html.platform-android.is-signal-app body.calculator-body .calculator-shell {
width: 100% !important;
max-width: none !important;
overflow-y: auto !important;
overflow-x: hidden !important;
}
html.platform-android.is-signal-app body.calculator-body .workspace,
html.platform-android.is-signal-app body.calculator-body main.workspace,
html.platform-android.is-signal-app body.calculator-body .calc-wrapper {
max-width: none !important;
overflow: visible !important;
width: 100% !important;
flex: initial !important;
}
/* Custom Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }
/* Sidebar & Navigation */
.sidebar {
width: 280px;
background: rgba(0,0,0,0.4);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
padding: 40px 20px;
gap: 40px;
overflow-y: auto;
}
.nav-group { display: flex; flex-direction: column; gap: 8px; }
.nav-label { font-size: 0.65rem; font-weight: 900; color: rgba(255,255,255,0.25); text-transform: uppercase; letter-spacing: 2px; margin-left: 15px; margin-bottom: 10px; }
.nav-item { padding: 14px 20px; border-radius: 12px; font-size: 0.9rem; font-weight: 600; color: rgba(255,255,255,0.5); cursor: pointer; transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); display: flex; align-items: center; gap: 15px; }
.nav-item:hover, .nav-item.active { background: var(--glass); color: #fff; }
.nav-item.active { border-left: 4px solid var(--accent); background: linear-gradient(to right, rgba(102, 192, 244, 0.1), transparent); box-shadow: 0 4px 20px rgba(0,0,0,0.2); }
/* Workspace & Core UI */
.workspace { flex: 1; display: flex; flex-direction: column; padding: 40px; background: radial-gradient(circle at top right, rgba(102, 192, 244, 0.05), transparent); overflow-y: auto; }
.calc-wrapper { max-width: 1000px; width: 100%; margin: 0 auto; display: grid; grid-template-columns: 1fr 320px; gap: 40px; min-height: 100%; }
.display-container {
background: rgba(0,0,0,0.6);
border: 1px solid var(--border);
border-radius: 24px;
padding: 30px;
margin-bottom: 20px;
text-align: right;
box-shadow: inset 0 10px 30px rgba(0,0,0,0.5);
position: relative;
}
.mode-indicator { position: absolute; top: 20px; left: 30px; font-size: 0.6rem; font-weight: 900; color: var(--accent); opacity: 0.4; letter-spacing: 2px; text-transform: uppercase; }
.history-line { font-size: 0.9rem; color: rgba(255,255,255,0.25); min-height: 1.2em; margin-bottom: 10px; font-family: 'JetBrains Mono', monospace; }
.main-line { font-size: 3rem; font-weight: 700; word-break: break-all; line-height: 1; letter-spacing: -1px; }
.controls-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.btn {
background: rgba(255,255,255,0.03);
border: 1px solid rgba(255,255,255,0.05);
border-radius: 12px;
padding: 16px;
font-size: 1rem;
font-weight: 600;
color: #fff;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
display: flex;
align-items: center;
justify-content: center;
}
.btn:hover { background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.2); transform: translateY(-3px); box-shadow: 0 10px 20px rgba(0,0,0,0.3); }
.btn.operator { color: var(--accent); font-weight: 800; font-size: 1.2rem; background: rgba(102, 192, 244, 0.05); }
.btn.function { color: var(--success); font-size: 0.85rem; }
.btn.clear { color: var(--danger); background: rgba(255, 62, 62, 0.05); }
.btn.equals { background: var(--accent); color: #000; font-weight: 900; grid-column: span 2; font-size: 1.5rem; }
.history-panel { background: rgba(0,0,0,0.2); border: 1px solid var(--border); border-radius: 24px; padding: 30px; display: flex; flex-direction: column; overflow: hidden; }
.history-list { flex: 1; overflow-y: auto; margin-top: 25px; display: flex; flex-direction: column; gap: 20px; }
.history-item { padding-bottom: 20px; border-bottom: 1px solid rgba(255,255,255,0.05); cursor: pointer; transition: opacity 0.2s; }
.history-item:hover { opacity: 0.7; }
.hist-expr { font-size: 0.75rem; color: rgba(255,255,255,0.35); font-family: monospace; margin-bottom: 6px; }
.hist-res { font-size: 1.2rem; font-weight: 700; color: #fff; }
/* Converter View */
.converter-panel { display: none; background: rgba(255,255,255,0.02); padding: 50px; border-radius: 24px; border: 1px solid var(--border); flex-direction: column; gap: 40px; }
.conv-row { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; align-items: end; }
.conv-input-box { display: flex; flex-direction: column; gap: 15px; }
.conv-input-box input { background: rgba(0,0,0,0.4); border: 1px solid var(--border); padding: 15px; border-radius: 12px; color: #fff; font-size: 1.5rem; font-weight: 800; outline: none; transition: border-color 0.2s; }
.conv-input-box input:focus { border-color: var(--accent); }
.conv-input-box select { background: #1a2b3b; color: #fff; border: 1px solid var(--border); padding: 12px; border-radius: 8px; outline: none; font-weight: 600; cursor: pointer; }
/* Programmer View */
.programmer-panel { display: none; flex-direction: column; gap: 8px; margin-top: 15px; border-top: 1px solid var(--border); padding-top: 15px; }
.prog-line { display: flex; justify-content: space-between; padding: 10px 14px; background: rgba(255,255,255,0.02); border-radius: 8px; font-family: 'JetBrains Mono', monospace; font-size: 0.75rem; }
.prog-label { color: var(--accent); font-weight: 800; font-size: 0.65rem; letter-spacing: 1px; }
/* Toggle Switches */
.toggle-btn.active { background: var(--accent); color: #000; }
/* Signal Share Menu Bar Styles */
.runner-header {
background: rgba(0,0,0,0.6);
backdrop-filter: blur(20px);
padding: 12px 30px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
width: 100%;
z-index: 1000;
flex-shrink: 0;
box-sizing: border-box;
}
.close-runner {
background: rgba(255,255,255,0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
color: #fff;
padding: 8px 16px;
border-radius: 6px;
font-size: 0.75rem;
font-weight: 800;
cursor: pointer;
transition: all 0.2s;
display: flex;
align-items: center;
gap: 8px;
text-decoration: none;
}
.close-runner:hover {
background: #ff3e3e;
border-color: transparent;
}
.runner-title-group {
display: flex;
align-items: center;
gap: 15px;
}
.runner-icon {
width: 24px;
height: 24px;
border-radius: 4px;
object-fit: cover;
}
.runner-title {
font-weight: 900;
font-size: 0.9rem;
letter-spacing: 1px;
text-transform: uppercase;
color: #fff;
}
</style>
</head>
<body class="calculator-body">
<div class="runner-header">
<div class="runner-title-group">
<img src="calculator_tool_poster_1778466276736.png" class="runner-icon">
<span class="runner-title">Scientific Calculator</span>
</div>
<a href="mini-games.html" class="close-runner">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
EXIT SESSION
</a>
</div>
<div class="calculator-shell">
<aside class="sidebar">
<div style="padding: 0 15px; margin-bottom: 20px;">
<h2 style="font-size: 1.4rem; margin: 0; letter-spacing: -1px; font-weight: 900;">Academic Pro</h2>
<p style="font-size: 0.7rem; opacity: 0.3; margin-top: 5px; font-weight: 700; letter-spacing: 1px;">UNIVERSITY OF TORONTO EDITION</p>
</div>
<div class="nav-group">
<span class="nav-label">Core Computing</span>
<div id="nav-standard" class="nav-item active" onclick="setMode('standard')">Standard</div>
<div id="nav-scientific" class="nav-item" onclick="setMode('scientific')">Scientific</div>
<div id="nav-programmer" class="nav-item" onclick="setMode('programmer')">Programmer</div>
</div>
<div class="nav-group">
<span class="nav-label">Unit Analysis</span>
<div id="nav-length" class="nav-item" onclick="setMode('length')">Length Analysis</div>
<div id="nav-weight" class="nav-item" onclick="setMode('weight')">Weight Analysis</div>
<div id="nav-temp" class="nav-item" onclick="setMode('temp')">Thermal Units</div>
</div>
</aside>
<main class="workspace">
<div class="calc-wrapper">
<div id="calculator-hub">
<div id="calc-view">
<div id="trig-toggle" class="toggle-group" style="display: none;">
<div id="deg-btn" class="toggle-btn active" onclick="setTrigMode('deg')">DEG</div>
<div id="rad-btn" class="toggle-btn" onclick="setTrigMode('rad')">RAD</div>
</div>
<div class="display-container">
<span id="mode-badge" class="mode-indicator">Standard</span>
<span id="memory-badge" style="position: absolute; top: 20px; right: 30px; font-size: 0.6rem; font-weight: 900; color: var(--success); opacity: 0; transition: opacity 0.3s;">MEM</span>
<div id="history-line" class="history-line"></div>
<div id="main-line" class="main-line">0</div>
<div id="prog-info" class="programmer-panel">
<div class="prog-line"><span class="prog-label">HEX</span><span id="prog-hex">0</span></div>
<div class="prog-line"><span class="prog-label">DEC</span><span id="prog-dec">0</span></div>
<div class="prog-line"><span class="prog-label">BIN</span><span id="prog-bin" style="font-size: 0.7rem;">0</span></div>
</div>
</div>
<div id="grid-container" class="controls-grid"></div>
</div>
<div id="converter-view" class="converter-panel">
<h2 id="conv-title" style="margin: 0; font-size: 2rem; font-weight: 900; letter-spacing: -1px;">Unit Converter</h2>
<div class="conv-row">
<div class="conv-input-box">
<label style="font-size: 0.65rem; font-weight: 900; color: var(--accent); letter-spacing: 1px;">INPUT MAGNITUDE</label>
<input type="number" id="conv-input-1" value="1" oninput="convert()">
<select id="conv-unit-1" onchange="convert()"></select>
</div>
<div style="padding-bottom: 60px; font-size: 2.5rem; opacity: 0.2; font-weight: 300;">=</div>
<div class="conv-input-box">
<label style="font-size: 0.65rem; font-weight: 900; color: var(--success); letter-spacing: 1px;">RESULTANT</label>
<input type="number" id="conv-input-2" readonly>
<select id="conv-unit-2" onchange="convert()"></select>
</div>
</div>
</div>
</div>
<div class="history-panel">
<div style="display: flex; justify-content: space-between; align-items: center;">
<span style="font-size: 0.75rem; font-weight: 900; opacity: 0.3; letter-spacing: 1.5px;">CALCULATION LOG</span>
<button onclick="clearHistory()" style="background: none; border: none; color: var(--danger); font-size: 0.65rem; font-weight: 800; cursor: pointer; opacity: 0.6;">PURGE</button>
</div>
<div id="history-list" class="history-list"></div>
</div>
</div>
</main>
<script>
// Signal Share Android Optimization Suite - Detection
(function() {
const isNative = !!window.Capacitor && typeof window.Capacitor.getPlatform === 'function' && window.Capacitor.getPlatform() !== 'web';
if (isNative || /Android/i.test(navigator.userAgent)) {
document.documentElement.classList.add('platform-android');
}
})();
function vibrate(ms) {
if (navigator.vibrate) navigator.vibrate(ms);
}
let currentExpr = '';
let lastResult = null;
let memory = 0;
let trigMode = 'deg'; // or 'rad'
let history = JSON.parse(localStorage.getItem('calc-history') || '[]');
let currentMode = 'standard';
const GRIDS = {
standard: [
['MC','MR','M+','M-','AC'],
['sqrt','sqr','inv','/','del'],
['7','8','9','*','abs'],
['4','5','6','-','fact'],
['1','2','3','+','pi'],
['neg','0','.','=','']
],
scientific: [
['sin','cos','tan','log','AC'],
['asin','acos','atan','ln','del'],
['pow','sqrt','abs','exp','/'],
['7','8','9','*','pi'],
['4','5','6','-','e'],
['1','2','3','+','fact'],
['(','0',')','=','']
],
programmer: [
['A','B','C','D','AC'],
['E','F','<<','>>','del'],
['7','8','9','/','AND'],
['4','5','6','*','OR'],
['1','2','3','-','XOR'],
['(','0',')','+','=']
]
};
const CONVERTERS = {
length: { units: { 'Meters': 1, 'Feet': 3.280839895, 'Inches': 39.37007874, 'Kilometers': 0.001, 'Miles': 0.000621371, 'Centimeters': 100, 'Millimeters': 1000 } },
weight: { units: { 'Kilograms': 1, 'Pounds': 2.20462262, 'Grams': 1000, 'Ounces': 35.2739619, 'Metric Tons': 0.001 } },
temp: { units: { 'Celsius': 'c', 'Fahrenheit': 'f', 'Kelvin': 'k' } }
};
function setMode(mode) {
vibrate(8);
currentMode = mode;
document.querySelectorAll('.nav-item').forEach(n => n.classList.remove('active'));
document.getElementById(`nav-${mode}`).classList.add('active');
const calcView = document.getElementById('calc-view');
const convView = document.getElementById('converter-view');
const trigToggle = document.getElementById('trig-toggle');
const progInfo = document.getElementById('prog-info');
const badge = document.getElementById('mode-badge');
if (['standard', 'scientific', 'programmer'].includes(mode)) {
calcView.style.display = 'block';
convView.style.display = 'none';
trigToggle.style.display = (mode === 'scientific') ? 'flex' : 'none';
progInfo.style.display = (mode === 'programmer') ? 'flex' : 'none';
badge.textContent = mode;
renderGrid(mode);
} else {
calcView.style.display = 'none';
convView.style.display = 'flex';
setupConverter(mode);
}
}
function renderGrid(mode) {
const container = document.getElementById('grid-container');
container.innerHTML = '';
GRIDS[mode].forEach(row => {
row.forEach(btn => {
if (!btn) return;
const b = document.createElement('button');
b.className = 'btn vibrate-on-touch';
if (['+','-','*','/','='].includes(btn)) b.classList.add('operator');
if (['AC','del'].includes(btn)) b.classList.add('clear');
if (['sin','cos','tan','log','ln','sqrt','fact'].includes(btn)) b.classList.add('function');
if (btn === '=') b.classList.add('equals');
b.textContent = btn.replace('sqrt','√').replace('sqr','x²').replace('del','⌫').replace('*','×').replace('/','÷');
b.onclick = () => handleBtn(btn);
container.appendChild(b);
});
});
}
function handleBtn(btn) {
vibrate(10);
if (btn === 'AC') clearAll();
else if (btn === 'del') backspace();
else if (btn === '=') calculate();
else if (['MC','MR','M+','M-'].includes(btn)) handleMemory(btn);
else if (['sin','cos','tan','asin','acos','atan','log','ln','sqrt','abs','fact','sqr','inv','exp'].includes(btn)) {
if (currentMode === 'scientific') append(btn + '(');
else applyFunc(btn);
}
else if (btn === 'pi') append('Math.PI');
else if (btn === 'e') append('Math.E');
else if (btn === 'pow') append('**');
else if (btn === 'neg') toggleNeg();
else if (btn === 'AND') append('&');
else if (btn === 'OR') append('|');
else if (btn === 'XOR') append('^');
else if (btn === '<<') append('<<');
else if (btn === '>>') append('>>');
else append(btn);
}
function handleMemory(btn) {
calculate(); // Ensure current expression is evaluated
const val = parseFloat(lastResult || currentExpr || 0);
switch(btn) {
case 'MC': memory = 0; break;
case 'MR': append(memory.toString()); break;
case 'M+': memory += val; break;
case 'M-': memory -= val; break;
}
document.getElementById('memory-badge').style.opacity = memory !== 0 ? '0.8' : '0';
}
function append(val) {
if (lastResult !== null && !['+','-','*','/','**'].includes(val)) {
currentExpr = '';
lastResult = null;
}
currentExpr += val;
updateDisplay();
}
function updateDisplay() {
let display = currentExpr || '0';
const pretty = display
.replace(/\*\*/g, '^')
.replace(/Math\.PI/g, 'π')
.replace(/Math\.E/g, 'e')
.replace(/sin\(/g, 'sin(')
.replace(/cos\(/g, 'cos(')
.replace(/tan\(/g, 'tan(')
.replace(/log\(/g, 'log(')
.replace(/ln\(/g, 'ln(')
.replace(/sqrt\(/g, '√(');
document.getElementById('main-line').textContent = pretty;
if (currentMode === 'programmer') updateProgrammer();
}
function calculate() {
try {
let expr = currentExpr
.replace(/×/g,'*')
.replace(/÷/g,'/')
.replace(/sin\(/g, trigMode === 'deg' ? 'Math.sin((Math.PI/180)*' : 'Math.sin(')
.replace(/cos\(/g, trigMode === 'deg' ? 'Math.cos((Math.PI/180)*' : 'Math.cos(')
.replace(/tan\(/g, trigMode === 'deg' ? 'Math.tan((Math.PI/180)*' : 'Math.tan(')
.replace(/asin\(/g, trigMode === 'deg' ? '(180/Math.PI)*Math.asin(' : 'Math.asin(')
.replace(/acos\(/g, trigMode === 'deg' ? '(180/Math.PI)*Math.acos(' : 'Math.acos(')
.replace(/atan\(/g, trigMode === 'deg' ? '(180/Math.PI)*Math.atan(' : 'Math.atan(')
.replace(/log\(/g, 'Math.log10(')
.replace(/ln\(/g, 'Math.log(')
.replace(/sqrt\(/g, 'Math.sqrt(')
.replace(/abs\(/g, 'Math.abs(')
.replace(/exp\(/g, 'Math.exp(');
// Programmer mode: handle Hex/Bin literals if they don't have prefix
if (currentMode === 'programmer') {
// This is a simple heuristic: if it contains A-F, treat as hex
// A better way is to prefix all tokens that look like numbers
expr = expr.replace(/\b([0-9A-F]+)\b/g, (match) => {
if (/[A-F]/.test(match)) return '0x' + match;
return match;
});
}
const res = Function('"use strict";return (' + expr + ')')();
// Academic Precision formatting
let formattedRes = res;
if (typeof res === 'number') {
if (isNaN(res)) throw new Error('NaN');
if (!isFinite(res)) {
document.getElementById('main-line').textContent = 'Math Error';
setTimeout(() => { if (document.getElementById('main-line').textContent === 'Math Error') updateDisplay(); }, 1500);
return;
}
if (Math.abs(res) > 1e12 || (Math.abs(res) < 1e-6 && res !== 0)) {
formattedRes = res.toExponential(6);
} else {
formattedRes = Number(res.toPrecision(12));
}
}
document.getElementById('history-line').textContent = document.getElementById('main-line').textContent + ' =';
addHistory(document.getElementById('main-line').textContent, formattedRes);
currentExpr = formattedRes.toString();
lastResult = formattedRes;
updateDisplay();
} catch(e) {
console.error(e);
document.getElementById('main-line').textContent = 'Syntax Error';
setTimeout(() => { if (document.getElementById('main-line').textContent === 'Syntax Error') updateDisplay(); }, 1500);
}
}
function applyFunc(func) {
try {
calculate(); // Evaluate expression first
let val = parseFloat(currentExpr || '0');
let res;
// Handle Degree/Radian for Trig
let trigVal = trigMode === 'deg' ? val * (Math.PI / 180) : val;
switch(func) {
case 'sin': res = Math.sin(trigVal); break;
case 'cos': res = Math.cos(trigVal); break;
case 'tan': res = Math.tan(trigVal); break;
case 'asin': res = Math.asin(val) * (trigMode === 'deg' ? 180/Math.PI : 1); break;
case 'acos': res = Math.acos(val) * (trigMode === 'deg' ? 180/Math.PI : 1); break;
case 'atan': res = Math.atan(val) * (trigMode === 'deg' ? 180/Math.PI : 1); break;
case 'log': res = Math.log10(val); break;
case 'ln': res = Math.log(val); break;
case 'sqrt': res = Math.sqrt(val); break;
case 'sqr': res = val * val; break;
case 'inv': res = 1 / val; break;
case 'abs': res = Math.abs(val); break;
case 'exp': res = Math.exp(val); break;
case 'fact': res = factorial(val); break;
}
if (!isFinite(res)) throw new Error('Math Error');
currentExpr = Number(res.toPrecision(12)).toString();
updateDisplay();
} catch(e) {
console.error(func, e);
}
}
function factorial(n) {
if (n < 0 || n > 170) return NaN;
if (n === 0) return 1;
let res = 1;
for (let i = 2; i <= Math.floor(n); i++) res *= i;
return res;
}
function setTrigMode(m) {
trigMode = m;
vibrate(5);
document.getElementById('deg-btn').classList.toggle('active', m === 'deg');
document.getElementById('rad-btn').classList.toggle('active', m === 'rad');
}
function toggleNeg() {
if (currentExpr.startsWith('-')) currentExpr = currentExpr.slice(1);
else currentExpr = '-' + currentExpr;
updateDisplay();
}
function clearAll() { currentExpr = ''; lastResult = null; updateDisplay(); }
function backspace() { currentExpr = currentExpr.slice(0, -1); updateDisplay(); }
function updateProgrammer() {
let val;
if (/[A-F]/.test(currentExpr)) {
val = parseInt(currentExpr, 16);
} else {
val = parseInt(currentExpr) || 0;
}
document.getElementById('prog-hex').textContent = isNaN(val) ? '0' : val.toString(16).toUpperCase();
document.getElementById('prog-dec').textContent = isNaN(val) ? '0' : val.toString(10);
document.getElementById('prog-bin').textContent = isNaN(val) ? '0' : val.toString(2).padStart(8, '0').match(/.{1,4}/g).join(' ');
}
function setupConverter(mode) {
document.getElementById('conv-title').textContent = document.getElementById(`nav-${mode}`).textContent;
const s1 = document.getElementById('conv-unit-1');
const s2 = document.getElementById('conv-unit-2');
s1.innerHTML = s2.innerHTML = '';
Object.keys(CONVERTERS[mode].units).forEach(u => {
s1.add(new Option(u, u));
s2.add(new Option(u, u));
});
s2.selectedIndex = 1;
convert();
}
function convert() {
const mode = currentMode;
const val = parseFloat(document.getElementById('conv-input-1').value) || 0;
const u1 = document.getElementById('conv-unit-1').value;
const u2 = document.getElementById('conv-unit-2').value;
const units = CONVERTERS[mode].units;
let result;
if (mode === 'temp') {
result = convertTemp(val, units[u1], units[u2]);
} else {
result = (val / units[u1]) * units[u2];
}
document.getElementById('conv-input-2').value = Number(result.toPrecision(8));
}
function convertTemp(v, f, t) {
let c;
if (f === 'c') c = v;
else if (f === 'f') c = (v - 32) * 5/9;
else c = v - 273.15;
if (t === 'c') return c;
if (t === 'f') return (c * 9/5) + 32;
return c + 273.15;
}
function addHistory(e, r) {
history.unshift({expr: e, res: r});
if (history.length > 30) history.pop();
localStorage.setItem('calc-history', JSON.stringify(history));
renderHistory();
}
function renderHistory() {
document.getElementById('history-list').innerHTML = history.map(h => `
<div class="history-item" onclick="currentExpr='${h.res}';updateDisplay();vibrate(5);">
<div class="hist-expr">${h.expr}</div>
<div class="hist-res">${h.res}</div>
</div>
`).join('');
}
function clearHistory() { history = []; localStorage.setItem('calc-history', '[]'); renderHistory(); vibrate(20); }
// KEYBOARD SUPPORT
window.addEventListener('keydown', (e) => {
if (e.key >= '0' && e.key <= '9') handleBtn(e.key);
if (e.key === '.') handleBtn('.');
if (['+','-','*','/'].includes(e.key)) handleBtn(e.key);
if (e.key === 'Enter') handleBtn('=');
if (e.key === 'Backspace') handleBtn('del');
if (e.key === 'Escape') handleBtn('AC');
if (currentMode === 'programmer' && /^[a-fA-F]$/.test(e.key)) handleBtn(e.key.toUpperCase());
});
// Hide sidebar return if in iframe
if (window.self !== window.top) {
const header = document.querySelector('.runner-header');
if (header) header.style.display = 'none';
document.body.style.paddingTop = 'var(--safe-area-top, 0px)';
document.body.style.paddingLeft = 'var(--safe-area-left, 0px)';
document.body.style.paddingRight = 'var(--safe-area-right, 0px)';
}
setMode('standard');
renderHistory();
</script>
</div>
</body>
</html>