generated from daakibenja/scientific-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
584 lines (515 loc) · 17.9 KB
/
index.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
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
var display = document.querySelector('.screen')
var expr = document.getElementById('input');
var output = document.getElementById('result');
var operators = ['!', '%', '^', '*', '(', ')', '-', '+', '+', 'x', '/', '<', '>'];
var functions = ['tan', 'cos', 'sin', 'arcsin', 'arccos', 'arctan', 'e', 'sqrt', 'abs','log','ln'];
var numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
var messages = [];
function bracketsMatch(expr = []){
var open = 0;
var closing = 0;
for (var i = 0; i < expr.length; i++){
if (expr[i]==='(') {
// console.log('found',expr[i],'opening');
open++;
}
else if (expr[i] === ')') {
// console.log('found', expr[i], 'closing');
closing++;
}
}
// console.log(open,closing);
if (open == closing) {
// console.log('matching');
return 0;
}
else if (open > closing) {
return 1;
}
else if (open < closing) {
return -1;
}
}
function calculate(e) {
var entered = expr.innerText;
if (bracketsMatch(entered) == 0) {
var tokens = tokenize(entered);
var parsed = parser(tokens);
parsed = computeFns(parsed);
var postifix = infixToPostfix(parsed);
var answer = evaluatePostfix(postifix);
}
else {
answer = 'brackets mismatch';
}
output.innerText = answer;
console.log(expr.innerText);
if (e.key == "Enter") {
e.preventDefault();
}
}
document.body.addEventListener('clickp', (e) => { alert('fuck')});
// event.preventDefault();
// e.preventDefault();
// console.log('yes');
// output.innerText = evaluatePostfix(infixToPostfix(parser(tokenize(expr.value))));
// });
function integral(input, x) {
var res = 0;
for (var i = 0; i < input.length; i++) {
power = i + 1;
res += ((input[i] * Math.pow(x, power)) / power);
}
return res;
}
function quadratic() {
// program to solve quadratic equation
let root1, root2;
// take input from the user
let a = prompt("Enter the first number: ");
let b = prompt("Enter the second number: ");
let c = prompt("Enter the third number: ");
// calculate discriminant
let discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
// result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
// result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// if roots are not real
else {
let realPart = (-b / (2 * a)).toFixed(2);
let imagPart = (Math.sqrt(-discriminant) / (2 * a)).toFixed(2);
// result
console.log(
`The roots of quadratic equation are ${realPart} + ${imagPart}i and ${realPart} - ${imagPart}i`
);
}
}
function precedence(operator) {
switch (operator) {
case "(":
return 4;
case "^":
return 3;
case "*":
case "/":
return 2;
case "+":
case "-":
return 1;
default:
return 0;
}
}
function evaluatePostfix(pfixArray = []) {
var evalStack = []
for (var i = 0; i < pfixArray.length; i++) {
var elem = pfixArray[i];
if (!isNaN(Number(elem))) {
elem = Number(elem);
}
// console.log(evalStack, elem);
if (operators.includes(elem)) {
switch (elem) {
case '+':
if (evalStack.length>=2) {
var second = evalStack.pop();
var first = evalStack.pop();
evalStack.push(first + second);
}
else if (evalStack.length==1) {
// alert('wrong syntax');
// console.log('syntax wrong *')
}
break;
case '-':
if (evalStack.length >= 2) {
var second = evalStack.pop();
var first = evalStack.pop();
evalStack.push(first - second);
}
else if (evalStack.length == 1) {
var popped = evalStack.pop();
evalStack.push((popped * -1));
}
else if (evalStack.length == 0) {
// alert('wrong syntax');
// console.log('syntax wrong *')
}
break;
case '/':
if (evalStack.length >= 2) {
var second = evalStack.pop();
var first = evalStack.pop();
evalStack.push(first / second);
}
else if (evalStack.length == 1) {
// console.log('the / missing an operand');
}
else if (evalStack.top == 0) {
// alert('wrong syntax');
console.log('syntax wrong *')
}
break;
case '*':
if (evalStack.length >=2) {
var second = evalStack.pop();
var first = evalStack.pop();
evalStack.push(first * second);
}
else if (evalStack.length == 1) {
// console.log('thee * operator expects two, one supplied');
}
else if (evalStack.length == 0) {
// alert('wrong syntax');
console.log('syntax wrong *');
}
break;
case '^':
if (evalStack.length >=2) {
var second = evalStack.pop();
var first = evalStack.pop();
evalStack.push(Math.pow(first,second));
}
else if (evalStack.length == 1) {
// var popped = evalStack.pop();
// console.log('syntax wrong *')
// evalStack.push((popped * -1));
}
else if (evalStack.length == 0) {
// alert('wrong syntax');
// console.log('syntax wrong *')
}
break;
}
}
else {
evalStack.push(Number(elem));
}
}
return evalStack.join('');
}
function infixToPostfix(expression = []) {
var pfixString = [];
var infixStack = new Stack();
expression.unshift('(');
expression.push(')');
// Helper function to get the precedence of the operator
for (var i = 0; i < expression.length; i++) {
// console.log(infixStack);
var c = expression[i];
if (c === '(') {
infixStack.push(c);
}
else if (isLetter(c) || !isNaN(Number(c))) {
if (c && c != '(') {
pfixString.push(c);
}
} else if (c === "+" || c === "-" || c === "*" || c === "/" || c === "^") {
while (c != "^" && !infixStack.isEmpty() && (precedence(c) <= precedence(infixStack.peek())) && infixStack.peek() != '(') {
var popped = infixStack.pop();
if (popped === '(') {
infixStack.push('(');
break;
}
if (popped && popped != '(') {
pfixString.push(popped);
}
}
infixStack.push(c);
// console.log(infixStack.__elements, pfixString);
}
else if (c === ')') {
// console.log(infixStack);
while (c != "(" && !infixStack.isEmpty()) {
c = infixStack.pop()
if (c && c != '(') {
pfixString.push(c);
}
}
}
}
while (!infixStack.isEmpty()) {
if (c && c != '(') {
pfixString.push(c);
}
}
return pfixString;
}
//Retrieve tokens from the input string
function tokenize(str) {
var result = [];
//remove white space
str.replace(/\s+/g, '');
//split the str into an array of the individual characters
str = str.split('');
//categorize the elements into tokens
str.forEach(function (char, idx) {
if (isDigit(char)) {
result.push(new Token("Literal", char));
} else if (isLetter(char)) {
result.push(new Token("Variable", char));
} else if (isOperator(char)) {
result.push(new Token("Operator", char));
} else if (isLeftParenthesis(char)) {
result.push(new Token("Left Parenthesis", char));
} else if (isRightParenthesis(char)) {
result.push(new Token("Right Parenthesis", char));
} else if (isComma(char)) {
result.push(new Token("Function Argument Separator", char));
}
else if (isDot(char)) {
result.push(new Token('Point', char));
}
});
return result;
}
function degToRad(angle) {
var result = Math.round((angle * Math.PI / 180)*1e10)/1e10;
// console.log('Dege ' + angle + ' rads ' + result);
return result;
}
function computeFns(parsed = []) {
var newParsed = [];
for (var i = 0; i < parsed.length; i++){
var token = parsed[i];
if (isLetter(token[0])&&token.length>1) {
if (!functions.includes(token)) {
messages.push('Unknown function ' + token);
return messages;
}
else {
switch (token) {
case 'cos':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.cos(degToRad(Number(argument)));
newParsed.push(Math.round(result*1e10)/1e10);
break;
case 'sin':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.sin(degToRad(Number(argument)));
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'tan':
var argument = parsed[i + 2];
// console.log(argument);
i = i + 3;
var result = Math.tan(degToRad(Number(argument)));
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'arccos':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.acos(argument);
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'arctan':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.atan(Number(argument));
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'arcsin':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.asin(Number(argument));
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'log':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.log10(Number(argument));
// alert(result);
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
case 'ln':
var argument = parsed[i + 2];
i = i + 3;
var result = Math.log(Number(argument));
// alert(result);
newParsed.push(Math.round(result * 1e10) / 1e10);
break;
}
}
}
else {
newParsed.push(token);
}
}
return newParsed;
}
//Parse for numbers and give them meaning
function parser(tokens = []) {
//array to hold result
var result = [];
//buffer to hold individual components such that we can
//categorize them into numbers, functions, parameters etc
var buffer = {
literal: [],
operator: [],
decimal: [],
variable: [],
param:[]
}
var errors = {
}
//Helper to know which buffer is currently active
//such that we can easily associate a group of tokens
//to some class eg variable, param or float etc
var active = {
literal: 0,
operator: 0,
decimal: 0,
param:0
}
//look throught the tokens one by one
tokens.forEach(function (token, index) {
// console.log(token.value);
//use the token type to group similar tokens
switch (token.type) {
//for a point first check if we are working on function params
//if yes push to the params buffer and break else push the point to
//the literal buffer
case 'Point':
if (active.param) {
buffer.param.push(token.value);
break;
}
buffer.literal.push(token.value);
break;
//still check if we are working on function params
//if yes push and break else push it to the literal buffer
case 'Literal':
if (active.param) {
buffer.param.push(token.value);
break;
}
buffer.literal.push(token.value);
break;
//still check if we are looking for params and act as usual
//else first join the literal buffer together and push it to result
case 'Variable':
if (active.param) {
buffer.param.push(token.value);
break;
}
if (buffer.literal.length >= 1) {
result.push(buffer.literal.join(''));
result.push('*');
buffer.literal = [];
}
//push it on both the result and buffer... we shall pop the out
result.push(token.value);
buffer.variable.push(token.value);
break;
case 'Operator':
if (active.param) {
buffer.param.push(token.value);
break;
}
if (buffer.literal[0]) {
result.push(buffer.literal.join(''));
buffer.literal = [];
}
result.push(token.value);
break;
case 'Left Parenthesis':
if (buffer.variable.length > 1) {
var i = 0;
for (; i < buffer.variable.length; i++){
result.pop();
}
result.push(buffer.variable.join(''));
buffer.variable = [];
active.param = 1;
}
if (buffer.literal[0]) {
result.push(buffer.literal.join(''));
buffer.literal = [];
}
result.push(token.value);
break;
case 'Right Parenthesis':
if (active.param) {
active.param = 0;
result.push(buffer.param.join(''));
buffer.param = [];
}
if (buffer.literal[0]) {
result.push(buffer.literal.join(''));
buffer.literal = [];
}
result.push(token.value);
break;
case 'Function Argument Separator':
result.push(token.value);
break;
}
});
if (buffer.literal) {
result.push(buffer.literal.join(''));
}
// console.log(buffer.variable);
return result;
}
class Error{
constructor(type, description) {
this.type = type;
this.desc = description;
this.help;
}
getHelp() {
}
}
function isComma(ch) { return (ch === ","); }
function isDot(ch) { return (ch === ".");}
function isDigit(ch) { return /\d/.test(ch); }
function isLetter(ch) { return /[a-z]/i.test(ch); }
function isOperator(ch) { return /\+|-|\*|\/|\^/.test(ch); }
function isLeftParenthesis(ch) { return (ch === "("); }
function isRightParenthesis(ch) { return (ch == ")"); }
class Token {
constructor(type, value) {
this.type = type;
this.value = value;
}
}
class Stack {
constructor() {
this.__elements = [];
this.top = -1;
}
isEmpty() {
if (this.top === -1) {
return 1;
}
else {
return 0;
}
}
pop() {
this.top = this.top - 1;
return this.__elements.pop();
}
push(item) {
this.__elements.push(item);
this.top = this.top + 1;
}
show() {
console.log(this.__elements);
}
peek() {
return this.__elements[this.top];
}
}