-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.js
More file actions
177 lines (153 loc) · 5.65 KB
/
Copy pathcal.js
File metadata and controls
177 lines (153 loc) · 5.65 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
function check() {
let x = document.getElementById("textfeild").value;
x = x.replaceAll('x', '*');
x = x.replaceAll('÷', '/');
x = x.replaceAll('pi', 22 / 7);
const regex = /[a-zA-Z]/;
if (regex.test(x)) {
document.querySelector("#result").innerText = 'invalid expression';
} else {
function infixToPostfix(expression) {
const precedence = { '+': 1, '-': 1, '*': 2, '/': 2, '!': 1, '√': 1, '%': 2 };
const stack = [];
const output = [];
expression.match(/(\d+(\.\d+)?|\+|\-|\*|\%|\!|\/|\(|\√|\))/g).forEach(token => {
if (!isNaN(token)) {
output.push(token); // Operand
} else if (token === '(') {
stack.push(token);
} else if (token === ')') {
while (stack.length && stack[stack.length - 1] !== '(') {
output.push(stack.pop());
}
stack.pop(); // Remove '('
} else { // Operator
while (stack.length && precedence[stack[stack.length - 1]] >= precedence[token]) {
output.push(stack.pop());
}
stack.push(token);
}
});
while (stack.length) {
output.push(stack.pop());
}
return output.join(' ');
}
let postfix = infixToPostfix(x); // Output: "3 4 + 2 *"
console.log(postfix);
function evaluatePostfix(expression) {
const stack = [];
const tokens = expression.split(/\s+/); // Split by spaces
for (const token of tokens) {
console.log(expression);
if (!isNaN(token)) {
// If the token is a number, push it to the stack
stack.push(Number(token));
}
else {
// Token is an operator
const b = stack.pop(); // Right operand
const a = stack.pop(); // Left operand
switch (token) {
case '+':
stack.push(a + b);
break;
case '-':
stack.push((a * 10 - b * 10) / 10);
break;
case '*':
stack.push(a * b);
break;
case '/':
stack.push(a / b);
break;
case '%':
stack.push(a % b);
break;
case '!':
if (a) {
stack.push(factorial(a));
} else {
stack.push(factorial(b));
} break;
case '√': if (a) {
stack.push(Math.sqrt(b) * a);
} else {
stack.push(Math.sqrt(b));
}
break;
default:
throw new Error(`Unsupported operator: ${token}`);
}
}
}
return stack.pop();
}
let postfixExpression = postfix.replaceAll("(", "");
console.log(postfixExpression);
document.querySelector("#result").innerText = '=' + evaluatePostfix(postfixExpression); // Output: 64
}
}
function factorial(n) {
if (n < 0) return "Undefined for negative numbers"; // Handle negative inputs
if (n === 0 || n === 1) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
function display(val) {
document.getElementById("textfeild").setAttribute("style", 'font-size: 2.5rem;')
document.getElementById("result").setAttribute("style", 'font-size: 1.7rem;')
document.getElementById("textfeild").value += val;
check();
}
document.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
eva();
}
});
function eva() {
let A = document.getElementById("textfeild");
A.setAttribute("style", 'font-size: 2rem;');
let B = document.getElementById("result");
B.setAttribute("style", 'font-size: 2.5rem;');
let C = document.getElementById("history");
C.innerText = A.value + B.innerText;
check();
}
function clr() {
var l = document.getElementById("textfeild").value.length;
var f = document.getElementById("textfeild").value.substr(0, l - 1);
console.log(f.length);
if (f.length > 1) {
document.getElementById("textfeild").value = f;
check();
} else {
let a = document.getElementById("textfeild");
let b = document.getElementById("result");
a.value = " ";
b.innerText = " ";
}
}
function AC() {
document.getElementById("textfeild").value = " ";
document.getElementById("result").innerText = " ";
document.getElementById('history').innerText = '';
}
function toggle() {
let flip = document.querySelector('.flip');
if (flip.classList.contains('flipforward')) {
flip.classList.remove("flipforward");
} else {
flip.classList.add("flipforward");
}
let btn = document.querySelectorAll(".toggle");
if (btn[0].classList.contains("dis")) {
btn.forEach(element => {
element.classList.remove("dis");
});
}
else {
btn.forEach(element => {
element.classList.add("dis");
});
}
}