forked from letterly/letterly.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harriscalc.html
217 lines (216 loc) Β· 9.26 KB
/
harriscalc.html
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
<html>
<head>
<style>
body, DOM{
background-color: #221a0f;
font-family: Verdana;
}
#content{
background-color: #fbebd4;
height: 54px;
width: 100%;
margin-bottom: 10px;
font-size: 44px;
font-weight: 700;
}
.number{
background-color: #f79a32;
}
.n{
color: #f79a32;
}
.operator{
background-color: #dc3958;
}
.o{
color: #dc3958;
}
.syntax{
background-color: #a57a4c;
}
.s{
color: #a57a4c;
}
.constant{
background-color: #889b4a;
}
.c{
color: #889b4a;
}
.equals{
background-color: #088649;
}
.input{
background-color: #8ab1b0;
}
h1{
text-align: center;
font-size: 48px;
color: #fbebd4;
}
button{
color: #fbebd4;
border: none;
height: 50px;
font-size: 28px;
}
.o-:first-child, .o + .o-, .s + .o-{
color: #f79a32;
}
</style>
<script>
function balancedParentheses(phrase){
if(phrase.includes("()")) return false
n = 0
for(x of phrase){
if(x == "(") n++
if(x == ")") n--
if(n < 0) return false
}
if(n == 0) return true
else return false
}
function render(id){
tp = id.charAt(0)
cn = id.slice(1)
document.getElementById("content").innerHTML += `<span class="${tp} ${id}">${cn}</span>`
}
function equals(){
if(!balancedParentheses(document.getElementById("content").textContent)){
alert("parentheses error")
return 0;
}
e = Array.from(document.getElementById("content").childNodes)
equation = []
// parses equation
for(ex of e){
ex = ex.textContent
console.log(ex)
ex = ex.replace(/π₯/g, "0").replace(/π₯/g, "1").replace(/π₯/g, "2").replace(/π₯/g, "3").replace(/π₯/g, "4").replace(/π₯/g, "5").replace(/π₯/g, "6").replace(/π₯/g, "7").replace(/π₯/g, "8").replace(/π₯/g, "9")
if((equation.length > 0) && (!isNaN(ex) || ex == ".")){
n = equation[equation.length-1]
if(!isNaN(n) || n == "." || (n == "-" && (equation.length == 1 ||"+-*/^".includes(equation[equation.length-2])))) equation[equation.length-1] += ex
else equation.push(ex)
}
else equation.push(ex)
}
console.log(equation)
newequation = []
// adds implicit multiplication sign x
for(second of equation){
first = newequation[newequation.length-1]
if(first == undefined) first = "+"
if(second == "(" && !("+-*/^".includes(first))) newequation.push("x")
else if("Οe".includes(second) && !(isNaN(first))) newequation.push("x")
if(second == "Ο") second = "3.14159"
else if(second == "e") second = "2.71828"
newequation.push(second)
}
console.log(+(bruhmoment(newequation)[0]).toFixed(5))
document.getElementById("content").innerHTML = `<span class="n">${((+(bruhmoment(newequation)[0]).toFixed(5)) + "").replace(/0/g, "π₯").replace(/1/g, "π₯").replace(/2/g, "π₯").replace(/3/g, "π₯").replace(/4/g, "π₯").replace(/5/g, "π₯").replace(/6/g, "π₯").replace(/7/g, "π₯").replace(/8/g, "π₯").replace(/9/g, "π₯")}</span>`
}
function del(){
d = document.getElementById("content")
if(d.hasChildNodes()) d.removeChild(d.lastChild)
}
//non button functions
function solvephrase(phrase){
newphrase = []
for(item of phrase.reverse()){
if(newphrase[newphrase.length-1] == "^"){
exp = Math.pow(item, newphrase[newphrase.length-2])
newphrase.pop()
newphrase.pop()
newphrase.push(exp)
}
else newphrase.push(item)
}
phrase = newphrase.reverse()
newphrase = []
for(item of phrase){
if(newphrase[newphrase.length-1] == "x"){
mult = newphrase[newphrase.length-2] * item
newphrase.pop()
newphrase.pop()
newphrase.push(mult)
}
else newphrase.push(item)
}
phrase = newphrase
newphrase = []
for(item of phrase){
if(newphrase[newphrase.length-1] == "*"){
mult = newphrase[newphrase.length-2] * item
newphrase.pop()
newphrase.pop()
newphrase.push(mult)
}
else if(newphrase[newphrase.length-1] == "/"){
div = newphrase[newphrase.length-2] / item
newphrase.pop()
newphrase.pop()
newphrase.push(div)
}
else newphrase.push(item)
}
phrase = newphrase
newphrase = []
for(item of phrase){
if(newphrase[newphrase.length-1] == "+"){
add = +newphrase[newphrase.length-2] + +item
newphrase.pop()
newphrase.pop()
newphrase.push(add)
}
else if(newphrase[newphrase.length-1] == "-"){
sub = newphrase[newphrase.length-2] - item
newphrase.pop()
newphrase.pop()
newphrase.push(sub)
}
else newphrase.push(item)
}
return newphrase
}
function bruhmoment(phrase){
phrase.unshift("(")
phrase.push(")")
while(phrase.includes(")")){
x = phrase.slice(0, phrase.indexOf(")"))
n = x.lastIndexOf("(")
g = phrase.slice(n+1, phrase.indexOf(")"))
phrase = phrase.slice(0, n).concat(solvephrase(g), phrase.slice(phrase.indexOf(")")+1))
}
return phrase
}
</script>
</head>
<body>
<h1>HarrisCalc</h1>
<div id="content" dir="rtl"></div>
<div id="buttons">
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="nπ₯" class="number" onclick="render(this.id)">π₯</button>
<button id="n." class="number" onclick="render(this.id)">.</button>
<button id="o+" class="operator" onclick="render(this.id)">+</button>
<button id="o-" class="operator" onclick="render(this.id)">-</button>
<button id="o*" class="operator" onclick="render(this.id)">*</button>
<button id="o/" class="operator" onclick="render(this.id)">/</button>
<button id="o^" class="operator" onclick="render(this.id)">^</button>
<button id="s(" class="syntax" onclick="render(this.id)">(</button>
<button id="s)" class="syntax" onclick="render(this.id)">)</button>
<button id="cΟ" class="constant" onclick="render(this.id)">Ο</button>
<button id="ce" class="constant" onclick="render(this.id)">e</button>
<button id="e=" class="equals" onclick="equals()">=</button>
<button id="iDEL" class="input" onclick="del()">DEL</button>
</div>
</body>
</html>