-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
399 lines (398 loc) · 12.9 KB
/
calc.js
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
// Physics constants
//Declaring variables
var decimalPoint = enter = entered = operatorSign = rootNpower_Sign = flo = math = M = firstI = first = second = secondI = answer = theanswer = result = peSign = "";
var opsCheck = dotCounter = 0;
var removeFirstZero = "";
//To prevent printing more than one dot
function dot(b) {
if (decimalPoint == "") {
enter = entered = b;
first+= enter;
entered+= enter;
decimalPoint = ".";
dotCounter = 0;
return first;
} else {
return first;
}
}
//To control what happen when Pi and Euler is clicked
function PE(b) {
decimalPoint = ".";
dotCounter = 15;
var cons = b;
if (peSign == "") {
if (operatorSign != "" && first == "" + operatorSign) {
first = (cons = "PI") ? Math.PI: Math.E;
} else if (operatorSign != "" && first > 0 || first < 0) {
first = (cons == "PI") ? first + "*" + Math.PI: first + "*" + Math.E;
} else if (first != "" && operatorSign != "") {
first += (cons = "PI") ? Math.PI: Math.E;
} else if (operatorSign == "" && first != "") {
first = (cons == "PI") ? first + "*" + Math.PI: first + "*" + Math.E;
} else {
first = (cons == "PI") ? Math.PI: Math.E;
}
} else if (first !== "") {
first = (cons == "PI") ? first + "*" + Math.PI: first + "*" + Math.E;
} else {
first = (cons == "PI") ? Math.PI: Math.E;
}
return first;
}
function mp() {
M = first;
first = M;
return first ;
}
function mr() {
first = M;
return first ;
}
//Cancel making all variable empty. All variable = ""
function c(c) {
document.getElementById("display1").innerHTML = decimalPoint = operatorSign = rootNpower_Sign = entered = math = first = firstI = second = secondI = answer = theanswer = flo = M = "";
return "";
}
//To calculate squares, cubes, factorials e.t.c. For calculations which use only one value. Variable 'firstI' was isolated by maths() function to be solved here
function mathematics() {
if (math == "sqr") {
result = firstI * firstI;
} else if (math == "cube") {
result = firstI * firstI * firstI;
} else if (math == "Sqrt") {
result = Math.sqrt(firstI);
} else if (math == "cubert") {
result = Math.cbrt(firstI);
} else if (math == "negpos") {
result = firstI * -1;
} else if (math == "sine") {
result = Math.sin(firstI);
} else if (math == "cosine") {
result = Math.cos(firstI);
} else if (math == "tangent") {
result = Math.tan(firstI);
} else if (math == "ln") {
result = Math.log(firstI);
} else if (math == "logTen") {
result = Math.log10(firstI);
} else if (math == "rand") {
result = Math.round(firstI);
}else if (math == "res") {
result = 1 / firstI;
} else if (math == "fact") {
n = firstI;
firstI = 1;
while (n > 1){
firstI *= n;
n -= 1;
}
result = firstI;
} decimalPoint = (Math.round(result) == result) ? "": ".";
}
//To make variable 'first' and 'second' keep result of its values. e.g if it was 3 + 2 it must now be 5. This is done to prepare them for use in maths() function
function prep() {
second = eval(second);
first = eval(first);
}
//When sqr, cube e.t.c are clicked, this function, math(), does the following: 1. It extract the last number you entered from variable 'first' and keeps it as variable 'firstI'. 2. It calls mathematics function to work on the on variable 'firstI'. The answer is kept in 'result' variable. 3.It displays the answer of other previous numbers entered and the 'result' from mathematics e.g. 5 + 4 which was 3 + 2 + 2^2. Here 3 + 2 has become 5 and because of prep() function.
function maths(a) {
math = a;
try {
if (operatorSign == "+") {
prep();
firstI = first - second;
mathematics();
first = second + "+" + result;
} else if (operatorSign == "-") {
prep();
firstI = second - first;
mathematics();
first = second + "-" + "(" + result + ")";
} else if (operatorSign == "*") {
prep();
firstI = first / second;
mathematics();
first = second + "*" + result;
} else if (operatorSign == "/") {
prep();
firstI = second / first;
mathematics();
first = second + "/" + result;
} else {
firstI = first;
mathematics();
first = result;
}
return first;
} catch (first ) {
first = second + operatorSign;
return first;
}
}
//This is just my notes:--> To remove first zero in second number zero must have its own function so that every entry of zero is checked to make sure that no number greater than zero should start with a zero
function digit(b) {
opsCheck = 0;
dotCounter++;
if (first == Infinity || first == NaN) {
first = 0;
}
peSign = "pes";
entered = b;
if (rootNpower_Sign != "") {
first = (first === "0" && entered !== ".") ? entered: first + entered;
return secondI + firstI + rootNpower_Sign + first;
} else {
first = (first === "0" && entered !== ".") ? entered: first + entered;
theanswer = eval(first) + "";
if (theanswer.length > 14) {
theanswer = Math.abs((theanswer*1).toPrecision(14));
}
document.getElementById("display1").innerHTML = first;
return theanswer;
}
}
var thebase = "";
function base(a) {
thebase = a;
first *= 1;
if (thebase == "bin") {
firstII = first.toString(2);
} else if (thebase == "oct") {
firstII = first.toString(8);
} else if (thebase == "hex") {
firstII = first.toString(16);
}
return firstII;
}
//Four functions below are for themes
function themes(thm) {
theme = thm;
el = document.getElementsByClassName("div");
if (theme == 1) {
el[0].id="theme1";
} else if (theme == 2) {
el[0].id="theme2";
} else if (theme == 3) {
el[0].id="theme3";
} else {
el[0].id="theme4";
}
}
//back space
function bsp() {
first += "";
dotCounter--;
decimalPoint = dotCounter >= 0 ? ".": "";
first = first.substr(0, first.length - 1);
document.getElementById("display1").innerHTML = first;
try{
eval(first);
return first;
} catch(first) {
eval(first);
return "";
}
}
//Two functions below calculate power and root
function pow() {
first *= 1;
result = Math.pow(firstI, first);
}
function roots() {
first *= 1;
result = Math.pow(first, 1 / firstI).toPrecision(12);
result = Math.abs(result);
}
//When operators ,+ - / * are clicked op() function does a number of things. It checks if power or root were clicked. If yes, it calculates the previous numbers and the add the new operator. If not it add the operator clicked
function operators(b) {
peSign = "";
if (opsCheck == 0) {
opsCheck = 1;
document.getElementById("display1").innerHTML = first;
try {
if (rootNpower_Sign == "^") {
if (operatorSign == "+") {
pow();
answer = result + second;
} else if (operatorSign == "-") {
pow();
answer = second - result;
} else if (operatorSign == "*") {
pow();
answer = result * second;
} else if (operatorSign == "/") {
pow();
answer = second / result;
} else {
pow();
answer = result;
}
} else if (rootNpower_Sign == "√") {
if (operatorSign == "+") {
roots();
answer = second + result;
} else if (operatorSign == "-") {
roots();
answer = second - result;
} else if (operatorSign == "*") {
roots();
answer = result * second;
} else if (operatorSign == "/") {
roots();
answer = second / result;
} else {
roots();
answer = result;
}
} else if (a == "%") {
answer = second % first;
} else {
operatorSign = b;
first += operatorSign;
decimalPoint = "";
}
rootNpower_Sign = "";
operatorSign = b;
firstI = "";
second = answer;
first = answer + operatorSign;
decimalPoint = "";
document.getElementById("display1").innerHTML = first;
return eval(second);
} catch(x) {
if (first != "<span class='red'>Press ON to start</span>") {
operatorSign = b;
second = eval(first);
first += operatorSign;
decimalPoint = "";
} else {
first = "<span class='red'>Press ON to start</span>" ;
}
document.getElementById("display1").innerHTML = first;
return (second == undefined ) ? 0 : eval(second);
}
} else {
operatorSign = b;
first += "";
first = first.substr(0, first.length - 1);
first = first + operatorSign;
document.getElementById("display1").innerHTML = first;
return (second == undefined ) ? 0 : eval(second);
}
}
function percent() {
first = eval(first) * 100;
return first + "%";
}
//toggles the negative sign
function negpos() {
first = (operatorSign == "") ? first *= -1: first;
return first ;
}
function power(b) {
rootNpower_Sign = b;
if (operatorSign == "+" && second != "") {
prep();
firstI = first - second;
first = "";
secondI = second + "+";
return second + "+" + firstI + rootNpower_Sign;
} else if (operatorSign == "-" && second != "") {
prep();
firstI = second - first;
first = "";
secondI = second + "-";
return second + "-" + firstI + rootNpower_Sign;
} else if (operatorSign == "*" && second != "") {
prep();
firstI = first / second;
first = "";
secondI = second + "*";
return second + "*" + firstI + rootNpower_Sign;
} else if (operatorSign == "/" && second != "") {
prep();
firstI = second / first;
first = "";
secondI = second + "/";
return second + "/" + firstI + rootNpower_Sign;
} else {
rootNpower_Sign = b;
firstI = first;
first = "";
return firstI + rootNpower_Sign;
}
}
function equal() {
document.getElementById("display1").innerHTML = first;
try {
if (rootNpower_Sign == "^") {
if (operatorSign == "+") {
pow();
first = result + second;
} else if (operatorSign == "-") {
pow();
first = second - result;
} else if (operatorSign == "*") {
pow();
answer = result * second;
first = answer;
} else if (operatorSign == "/") {
pow();
first = second / result;
} else {
pow();
first = result;
}
} else if (rootNpower_Sign == "√") {
if (operatorSign == "+") {
roots();
first = result + second;
} else if (operatorSign == "-") {
roots();
first = second - result;
} else if (operatorSign == "*") {
roots();
first = result * second;
} else if (operatorSign == "/") {
roots();
first = second / result;
} else {
roots();
first = result;
}
} else if (operatorSign == "%") {
answer = second % first;
} else {
if (first == "") {
first = first ;
} else {
try{
first = eval(first) + "";
if (first.length > 14) {
first = Math.abs((first*1).toPrecision(14));
}
} catch (first) {
first = "<small><small>Incorrect input. Click C to clear</small></small>";
return first;
first = "";
}
}
}
rootNpower_Sign = operatorSign = answer = firstI = second = "";
flo = first;
flo = Math.floor(flo);
decimalPoint = (flo == first) ? "": ".";
return first;
} catch(operatorSign) {
operatorSign = "";
first = eval(first) + "";
if (first.length > 14) {
first = Math.abs((first*1).toPrecision(14));
}
flo = first;
flo = Math.floor(flo);
decimalPoint = (flo == first) ? "": ".";
return first;
}
}