-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScientificCalculator.html
More file actions
125 lines (106 loc) · 6.26 KB
/
ScientificCalculator.html
File metadata and controls
125 lines (106 loc) · 6.26 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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./ScientificCalculatorStyle.css">
<link rel="icon" type="image/svg" href="https://upload.wikimedia.org/wikipedia/commons/1/1e/Calculator_icon.svg">
<title>Scientific Calculator</title>
</head>
<body>
<div class="window">
<div class="back">
<form>
<p class="brand">CALCI</p>
<div class="inop">
<input type="text" name="op" class="output" disabled="" placeholder="0" id="op">
</div>
<div class="first" align="center">
<input type="button" name="operator" class="in" value="X²" onclick="op.value+='**2'">
<input type="button" name="operator" class="in" value="X³" onclick="op.value+='**3'">
<input type="button" name="operator" class="in" value="nCr" onclick="op.value+='nCr'">
<input type="button" name="operator" class="in" value="X^" onclick="op.value+='**'">
<input class="in red" type="button" name="c" value="C" onclick="op.value=op.value.slice(0,-1)">
<input class="in red" type="button" name="ac" value="AC" onclick="op.value=''">
</div>
<div class="first" align="center">
<input type="button" name="operator" class="in" value="√" onclick="op.value='Math.sqrt(' + op.value + ')';">
<input type="button" name="operator" class="in" value="∛" onclick="op.value='Math.cbrt(' + op.value + ')';">
<input type="button" name="operator" class="in" value="nPr" onclick="op.value+='nPr'">
<input type="button" name="operator" class="in" value="X!" onclick="op.value+='!'">
<input type="button" name="operator" class="in" value="(" onclick="op.value+='(';">
<input type="button" name="operator" class="in" value=")" onclick="op.value+=')';">
</div>
<div class="first" align="center">
<input class="in" type="button" name="num" value="7" onclick="op.value+='7'">
<input class="in" type="button" name="num" value="8" onclick="op.value+='8'">
<input class="in" type="button" name="num" value="9" onclick="op.value+='9'">
<input type="button" name="operator" class="in" value="sin" onclick="op.value='Math.sin(degToRad(' + op.value + '))'">
<input type="button" name="operator" class="in" value="cos" onclick="op.value='Math.cos(degToRad(' + op.value + '))'">
<input type="button" name="operator" class="in" value="tan" onclick="op.value='Math.tan(degToRad(' + op.value + '))'">
</div>
<div align="center">
<input class="in" type="button" name="num" value="6" onclick="op.value+='6'">
<input class="in" type="button" name="num" value="5" onclick="op.value+='5'">
<input class="in" type="button" name="num" value="4" onclick="op.value+='4'">
<input class="in" type="button" name="operator" value="-" onclick="op.value+='-'">
<input class="in" type="button" name="operator" value="X" onclick="op.value+='*'">
<input class="in" type="button" name="operator" value="/" onclick="op.value+='/'">
</div>
<div style="display: flex;">
<div style="width:84%;justify-content: space-around;">
<div class="first" align="center">
<input class="in" type="button" name="num" value="3" onclick="op.value+='3'">
<input class="in" type="button" name="num" value="2" onclick="op.value+='2'">
<input class="in" type="button" name="num" value="1" onclick="op.value+='1'">
<input class="in" type="button" name="operator" value="ℼ" onclick="op.value+='3.14'">
<input type="button" class="in" name="operator" value="%" onclick="op.value+='/' + 100">
</div>
<div class="first" align="center">
<input class="in" type="button" name="num" value="0" onclick="op.value+='0'">
<input class="in" type="button" name="num" value="00" onclick="op.value+='00'">
<input class="in" type="button" name="num" value="." onclick="op.value+='.'">
<input class="in" style="width: 23vh;" type="button" name="oprtr" value="=" onclick="calculate()">
</div>
</div>
<div>
<input class="in" style="height: 21.4vh;margin-left: -11px;" name="operator" type="button" value="+" onclick="op.value+='+'">
</div>
</div>
</form>
</div>
</div>
<script>
function calculate() {
let expression = op.value;
const factorialRegex = /(\d+)!/g;
expression = expression.replace(factorialRegex, (match, p1) => factorial(parseInt(p1)));
const nCrRegex = /(\d+)nCr(\d+)/g;
expression = expression.replace(nCrRegex, (match, p1, p2) => nCr(parseInt(p1), parseInt(p2)));
const nPrRegex = /(\d+)nPr(\d+)/g;
expression = expression.replace(nPrRegex, (match, p1, p2) => nPr(parseInt(p1), parseInt(p2)));
try {
op.value = eval(expression);
} catch (error) {
op.value = 'Error';
}
}
function factorial(n) {
if (n < 0) return NaN;
if (n === 0) return 1;
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
function nCr(n, r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
function nPr(n, r) {
return factorial(n) / factorial(n - r);
}
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
</script>
</body>
</html>