-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
254 lines (229 loc) · 6.81 KB
/
main.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
//in this js program i will convert infix expression to postfix then i will evoluate it.
const expin = document.getElementById("expin");
const result = document.getElementById("result");
const user_input = document.getElementById("user-input");
user_input.addEventListener('keypress', enterPressed);
const history = document.getElementById("last-command");
let historyarr = ["This Array Stores History User Inputs."];
const helpobj = document.getElementById("help");
const helpmsg = `This is a Calculator Web Application with some basic operation like +,-,*,/,%,^. <br>
you can add '(' and ')' in the input field below history. <br>
if you found any bug or result error you can <a href="mailto:susantamandi.user@gmail.com">Email Us.</a>
`;
console.log(helpmsg);
let calvalue = 0;
let count = 0;
let help = false;
function outputToInput(){
if(typeof(Number(result.value)) == 'number' && !(result.value == "NaN" || result.value == "undefined")){
submit();
user_input.value = result.value;
result.value = "";
expin.value = user_input.value;
}
}
function submit() {
if(user_input.value == ""){
showhelp("Can't Calculate No Input");
return;
}
if(user_input.value != "" && result.value != "" && user_input.value != historyarr[count] && typeof(Number(result.value)) == 'number' && !(result.value == "NaN" || result.value == "undefined")){
history.style.visibility = "visible";
count = count + 1;
historyarr[count] = user_input.value;
history.innerHTML = "<p onclick=\"clickHistory('" + user_input.value + "')\">" + count + ":</br>" + " User: " + user_input.value + "</br>" + " JAGU: " + calvalue + "</p>" + history.innerHTML;
}
}
function inputChanged() {
user_input.value = user_input.value.replaceAll(/ /g,"");
helpobj.style.visibility = "hidden";
expin.value = user_input.value;
expin.scrollLeft = expin.scrollWidth; // scroll to the right
calvalue = evaluatePostfix(infixToPostfix(charToList(user_input.value)));
result.value = calvalue;
result.scrollLeft = result.scrollWidth; // scroll to the right
if(user_input.value == ""){
result.value = "";
expin.value = ""
}
}
function appendChar(c) {
user_input.value += c;
inputChanged();
}
function clearInput(c) {
if (c == "AC") {
user_input.value = "";
result.value = "";
expin.value = "";
}else {
let str = user_input.value;
str = str.substring(0, str.length - 1);
user_input.value = str;
inputChanged();
if (str == "") {
result.value = "";
expin.value = "";
}
}
}
function showhelp(c){
if(c == 1){
helpobj.innerHTML = helpmsg;
if(help == false){
help = true;
helpobj.style.visibility = "visible";
}else{
help = false;
helpobj.style.visibility = "hidden";
}
}else{
helpobj.style.visibility = "visible";
helpobj.innerHTML = "<p style=\"color: red;\">" + c + "</p>";
}
}
function clickHistory(c){
user_input.value = c;
inputChanged();
}
function enterPressed(event) {
// Check if the key is 'Enter'
if (event.key === 'Enter') {
// Do something
submit();
}
}
function doOperation(x, c, y) {
switch (c) {
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
case '%':
return x % y;
case '^':
return Math.pow(x, y);
default:
showhelp("Wrong Operation Symbol Ocured");
}
}
function isNum(c) {
return (c == '.' || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9');
}
function isBrace(c) {
if (c == '(' || c == ')')
return true;
return false;
}
function isOp(c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '%')
}
function opPref(c) {
switch (c) {
case '^':
return 6;
case '/':
return 4;
case '%':
case '*':
return 3;
case '+':
case '-':
return 1;
case '(':
case ')':
return 0;
default:
showhelp("Wrong Operator Encountered.");
}
}
function charToList(st) {
let num = '';
let infixList = [];
let prev = null;
let isNeg = 0;
let c;
for (let i = 0; i < st.length; i++) {
c = st[i];
if (isNum(c)) {
num = num + c;
} else if (isOp(c) || isBrace(c)) {
if (num != "") {
infixList.push(Number(num));
num = "";
}
if (c == '-') {
if (prev == null || isOp(prev) || prev == '(') {
num = num + c;
} else {
infixList.push(c);
}
} else {
infixList.push(c);
}
} else {
showhelp("Wrong Character Encountered.");
return null;
}
prev = c;
}
if (num != "") {
infixList.push(Number(num));
num = "";
}
return infixList;
}
function infixToPostfix(l) {
let l1 = l;
let postlist = [];
let oplist = [];
let len = l1.length;
let i = 0;
while (i < len) {
let value = l1[i];
if (typeof (value) == 'number') {
postlist.push(value);
} else {
if (oplist.length == 0 || value == '(' || opPref(value) > opPref(oplist[oplist.length - 1])) {
oplist.push(value);
} else {
if (value == ')') {
while (oplist.length != 0 && oplist[oplist.length - 1] != '(') {
postlist.push(oplist.pop());
}
if (oplist.length != 0 && oplist[oplist.length - 1] == '(') {
oplist.pop();
}
} else {
while (oplist.length != 0 && opPref(value) <= opPref(oplist[oplist.length - 1])) {
postlist.push(oplist.pop());
}
oplist.push(value);
}
}
}
i = i + 1;
}
while (oplist.length != 0) {
postlist.push(oplist.pop());
}
return postlist;
}
function evaluatePostfix(postfix) {
const stack = [];
for (let i = 0; i < postfix.length; i++) {
const elem = postfix[i];
if (typeof elem === 'number') {
stack.push(elem);
} else {
const num2 = stack.pop();
const num1 = stack.pop();
stack.push(doOperation(num1, elem, num2));
}
}
return stack[stack.length - 1];
}