-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptforCal.js
556 lines (496 loc) · 19.4 KB
/
scriptforCal.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
// 7 input dile output dibe na , then 8 dile no return then + dile 78 return
// 5 dile no output, 6 dile no output then = dile 56 return
// assignment- ekta simple mathmatical "5+6+7" deya thakbe, eta evaluate kore result return korbo
// function = takeInput
// input = digit or operator
// output = a String
// step1: initialize a global variable , inputString = ""
// step2: if input is not "=" then add it to inputString and show it to screen
// step3:if input "=" hole return inputString
var inputString = "";
function takeInput(symbol){
if (symbol != "="){
inputString += symbol;
document.getElementById("screen").innerHTML = inputString;
}
else{
try{
document.getElementById("showResult").innerHTML = evaluateExpression(getListOfToken(inputString));}
catch(err){
document.getElementById("showResult").innerHTML = err;
}
// inputString = "";
}
}
// function = takeMultiCharacter()
// input = multi character string
// output = string expression
// step1:take each character and add the character to input string
// step2: show the inputString in screen
function takeMultiCharacter(multiChar){
for(var index = 0; index < multiChar.length; index++ ){
inputString += multiChar[index];
document.getElementById("screen").innerHTML = inputString;
}
}
// 1.takeInput(7):
// 1.symbol = 7
// 2.if (symbol != "=")
// 3.if (7 != "=")
// 4.if true{
// 1.inputString += symbol
// 2.inputString = "" + 7
// 3.inputString = "7"
// 4.document.getElementById("screen").innerHTML = inputString;
// 5.document.getElementById("screen").innerHTML = "7";
// }
// 2.takeInput("+"):
// 1.symbol = "+"
// 2.if (symbol != "=")
// 3.if ("+" != "=")
// 4.if true{
// 1.inputString += symbol
// 2.inputString = "7" + "+"
// 3.inputString = "7+"
// 4.document.getElementById("screen").innerHTML = "7+";
// 5.document.getElementById("screen").innerHTML = "7+";
// }
// 2.takeInput(2):
// 1.symbol = 2
// 2.if (symbol != "=")
// 3.if (2 != "=")
// 4.if true{
// 1.inputString += symbol
// 2.inputString = "7+" + "2"
// 3.inputString = "7+2"
// 4.document.getElementById("screen").innerHTML = "7+2";
// 5.document.getElementById("screen").innerHTML = "7+2";
// }
// function = processInput
// input = String expression
// output = show result after operation
// step1:take a token from the list of token
// step2: if the token is a number :
// step2.1: if number1 == null then save token in number1 and go to step 1
// step 2.2: else number2 te token assign
// step 2.2.1: current operation korbo and result number1 e save korbo and go to step 1
// step3: else token is operator then save it to current operator and go to step 1
// test case1 :if operator is /, * then do that first
// test case2 :if the first character of the expression is minus then save the minus and the later character as number1
// 1.if the first token of the listOfToken is "-":
// 1.number1 += add "-" and the next token
// function evaluateExpression(expression){
// console.log(expression)
// var number1 = null;
// var number2 = null;
// var currentOperation = null;
// listOfToken = getListOfToken(expression);
// console.log(listOfToken);
// for(var index = 0 ; index < listOfToken.length; index++){
// if (listOfToken[index] == "-"){
// number1 += parseFloat(listOfToken[index] + listOfToken[index+1]);
// index++;
// }
// else if(isNaN(listOfToken[index]) == false){
// if(number1 == null){
// number1 = listOfToken[index];
// }
// else{
// number2 = listOfToken[index];
// number1 = doOperation(currentOperation, number1, number2)
// }
// }
// else{
// currentOperation = listOfToken[index]
// }
// }
// console.log(`resultant = ${number1}`)
// console.log(`number2 = ${number2}`)
// return number1
// }
// expression = "-500"
// 1.evaluateExpression(expression):
// 1.expression = "-500"
// 2.var number1 = null;
// var number2 = null;
// var currentOperation = null;
// 3.listOfToken = getListOfToken(expression)
// 4.listOfToken = ["-", 500]
// 5.for(var index = 0 ; index < listOfToken.length; index++)
// 6.for(var index = 0 ; 0 < 2 ; 0++){
// 1.if (listOfToken[0] == "-"
// 2.if ("-" == "-")
// 3.if true{
// 1.number1 = parseFloat(listOfToken[0] + listOfToken[1]);
// 2.number1 = parseFloat("-" + listOfToken[1]);
// 3.number1 = parseFloat("-" + 500);
// 4.number1 = parseFloat("-500");
// 5.number1 = -500.00
// 6.index++;
// 7.index = 1
// }
// }
// step1:puro expression ta itterate korte hobe j (BODMAS) ase kina
// step2:priority wise bodmas er kono operator er ager number and er porer number niye oi operation kore resultant ta number1 er place e rakhbo
// 1.expression = "12+6"
// 2.listOfToken = getListOfToken(expression)
// 3.listOfToken = getListOfToken("12+6")
// 4.listOfToken = [12, "+", "6"]
// 5.for(var index = 0 ; index < listOfToken.length; index++)
// 6.for(var index = 0 ; index < 3; 0++)
// 7.for(var index = 0 ; 0 < 3; 0++){
// 1.if(isNaN(listOfToken[index]) == false)
// 2.if(isNaN(listOfToken[0]) == false)
// 3.if(isNaN(12) == false)
// 4.if(false == false)
// 5.if true{
// 1.if(number1 == null)
// 2.if (null == null)
// 3.if true{
// 1.number1 = listOfToken[index]
// 2.number1 = listOfToken[0]
// 3.number1 =12
// index = 0+1 = 1
// }
// 8.for(var index = 1 ; 1 < 3; 1++)
// 1.if(isNaN(listOfToken[index]) == false)
// 2.if(isNaN(listOfToken[1]) == false)
// 3.if(isNaN("+") == false)
// 4.if (true == false)
// 5.if false
// 6.else{
// 1.currentOperation = listOfToken[index]
// 2.currentOperation = listOfToken[1]
// 3.currentOperation = "+"
// index = 1+1 = 2
// 9.for(var index = 2 ; 2 < 3; 3++)
// 1.if(isNaN(listOfToken[index]) == false)
// 2.if(isNaN(listOfToken[2]) == false)
// 3.if(isNaN(6) == false)
// 4.if(false == false)
// 5.if true{
// 1.if(number1 == null)
// 2.if(12 == null)
// 3.if false
// 4.else{
// 1.number2 = listOfToken[index]
// 2.number2 = listOfToken[2]
// 3.number2 = 6
// 4.doOperation(currentOperation)
// 5.doOperation("+"){
// 1.operator = "+"
// 2.if (operator == "+")
// 3.if ("+" == "+")
// 4.if true{
// 1.number1 = addition(number1, number2)
// 2.number1 = addition(12, 6){
// number 1 = 12, number2 = 6
// return number1 + number2
// return 12 + 6
// return 18
// }
// 3.number1 = 18
// }
// }
// }
// }
// }
// function = processListOfToken
// input = listOfToken
// output = listOfToken
// step1: srarch for "/" , if it's found then take number1 = previous token , number2 = next token and do the operation and replace number1,
// token and number2 with the resultant and return listOfToken
// step2:srarch for "*" , if it's found then do the operation and save the resultant at token's place and return listOfToken
// step3:srarch for "+" , if it's found then do the operation and save the resultant at token's place and return listOfToken
// step4:srarch for "-" , if it's found then do the operation and save the resultant at token's place and return listOfToken
// function processListOfToken(listOfToken){
// if (listOfToken.indexOf('/') !=-1){
// var number1 = listOfToken[listOfToken.indexOf('/')-1];
// var number2 = listOfToken[listOfToken.indexOf('/')+1];
// newToken = division(number1, number2);
// listOfToken = getUpdatedListOfToken(listOfToken, listOfToken.indexOf('/'), newToken);
// }
// else if(listOfToken.indexOf('*') !=-1){
// var number1 = listOfToken[listOfToken.indexOf('*')-1];
// console.log(number1)
// var number2 = listOfToken[listOfToken.indexOf('*')+1];
// console.log(number2)
// newToken = multiplication(number1, number2);
// console.log(newToken)
// listOfToken = getUpdatedListOfToken(listOfToken, listOfToken.indexOf('*'), newToken);
// console.log(listOfToken)
// }
// else if(listOfToken.indexOf('+') !=-1){
// var number1 = listOfToken[listOfToken.indexOf('+')-1];
// var number2 = listOfToken[listOfToken.indexOf('+')+1];
// newToken = addition(number1, number2);
// listOfToken = getUpdatedListOfToken(listOfToken, listOfToken.indexOf('+'), newToken);
// }
// else if(listOfToken.indexOf('-') !=-1){
// var number1 = listOfToken[listOfToken.indexOf('-')-1];
// var number2 = listOfToken[listOfToken.indexOf('-')+1];
// newToken = subtraction(number1, number2);
// listOfToken = getUpdatedListOfToken(listOfToken, listOfToken.indexOf('-'), newToken);
// console.log(listOfToken)
// };
// return listOfToken;
// }
// function = processListOfToken
// input = listOfToken
// outPut = listOfToken
// step1:initialize index = -1
// step2:find index of "/"
// step3:if index not equal -1 then save operator = "/"
// step4:else if find index of "*"
// step5:if index not equal to -1 then save operator = "*"
// step6:do the same thing for "+" and "-" operator
// step7:do the operation
// step8:return the new list
function processListOfToken(listOfToken){
var index = -1;
index = listOfToken.indexOf('/');
if (index != -1){
operator = "/";
}
else if(listOfToken.indexOf('*') != -1){
index = listOfToken.indexOf('*');
operator = "*";
}
else if(listOfToken.indexOf('+') != -1){
index = listOfToken.indexOf('+');
operator = "+";
}
else if(listOfToken.indexOf('-') != -1){
index = listOfToken.indexOf('-');
operator = "-";
}
if (operator != null){
var number1 = listOfToken[index-1];
var number2 = listOfToken[index+1];
var newToken = doOperation(operator, number1, number2)
listOfToken = getUpdatedListOfToken(listOfToken, index, newToken);
return listOfToken
}
}
list = [50, '+',240];
console.log(processListOfToken(list))
// problem in code =
// or , new ekta listOfToken banabo jetar ager sob token thakbe sudhu number1, token and number2 er jaygay resultant thakbe
// solution: input = list, index, newNumber
// 1.new list banabo, ager list er index-1 er ag porjonto copy HTMLObjectElement, index-1 er jaygay newNumber boshbe, index and index+1 baad jabe and er last porjonto copy hobe
// function = getUpdatedListOfToken
// input = listOfToken, indexOfOperator, newToken
// outPut = updated listOfToken
// step1: index 0 to (indexOfOperator-2) er element same thakbe
// step2:(indexOfOperator-1) index e newToken boshbe
// step3:(indexOfOperator+2) theke kono element thakle segulo boshbe
// step4: updated listOfToken return korbe
function getUpdatedListOfToken(listOfToken, indexOfOperator, newToken){
UpdatedListOfToken = [];
for(var index = 0; index < (indexOfOperator-1); index++){
UpdatedListOfToken.push(listOfToken[index])
};
UpdatedListOfToken.push(newToken);
for(var index = indexOfOperator+2; index < listOfToken.length; index++ ){
UpdatedListOfToken.push(listOfToken[index]);
}
return UpdatedListOfToken
}
list = [50, '*', 20];;
console.log(getUpdatedListOfToken(list, 1, 1000))
// listOfToken = [2,"*",3]
// step1:srarch for "/", false
// step2:srarch for "*", true:
// 1.number1 = "*" er ager token
// 2.number2 = "*" er porer token
// 3.do the operation
// 4.save the
// function = evaluate expression
// input = listOfToken
// outPut = listOfToken
// step1:listOfToken er length 1 howa na porjonto ekta kore operation korbo
// step2: length 1 hole return korbo
// testCase1:if two consecutive tokens are (**, //, */, /*) then show error
// testCase2:at index 0 token can't be / or *
// testCase3: last e operator
function evaluateExpression(listOfToken){
if(listOfToken[0] == "*" || listOfToken[0] == "/") throw "wrong input";
for(var index = 0; index<listOfToken.length-1; index++){
if(listOfToken[index] == "*" && listOfToken[index+1] =="/") throw "wrong input";
else if(listOfToken[index] == "*" && listOfToken[index+1] =="*") throw "wrong input";
else if(listOfToken[index] == "/" && listOfToken[index+1] =="*") throw "wrong input";
else if(listOfToken[index] == "/" && listOfToken[index+1] =="/") throw "wrong input";
}
if(isNaN(listOfToken[listOfToken.length-1])) throw "wrong input";
while(listOfToken.length > 2){
listOfToken = processListOfToken(listOfToken);
console.log(listOfToken);
}
return listOfToken
}
list = [50, '+', 21,'/',3,'*',4];
console.log(evaluateExpression(list))
// 1.evaluateExpression(["/", 3]):
// 1.lisofToken = ["/", 3]
// 2.try{if(listOfToken[0] == "*" || listOfToken[0] == "/") throw "wrong input";
// 3.try{if("/" == "*" || "/" == "/") throw "wrong input";
// 4.try{if(false || true) throw "wrong input";
// 5.try{if true}
// 6.try{ throw "wrong input"}
// 7.catch(error)
// 8.catch("wrong input"){
// 1.document.getElementById("showResult").innerHTML = `not valid $(error)`
// 2.document.getElementById("showResult").innerHTML = `not valid wrong input`
// }
// 2.undefind
// function = doOperation
// input = operator
// output = nothing
// step1:if the operator is "+" then call the addition function and save the result in number1
// step2:if the operator is "-" then call the subtraction function and save the result in number1
// step3:if the operator is "/" then call the division function and save the result in number1
// step4:if the operator is "*" then call the add function and save the result in number1
function doOperation(operator, number1, number2){
console.log(`operator = ${operator}`);
console.log(`number1 = ${number1}`);
console.log(`number2 = ${number2}`);
if (operator == "+"){
return addition(number1, number2)
}
else if (operator == "-"){
return subtraction(number1, number2)
}
else if(operator == "/"){
return division(number1, number2)
}
else if(operator == "*"){
return multiplication(number1, number2)
}
else{
return modulus(number1, number2)
}
}
function addition(number1, number2){
return number1 + number2
}
function subtraction(number1, number2){
return number1 - number2
}
function division(number1, number2){
return number1 / number2
}
function multiplication(number1, number2){
return number1 * number2
}
function modulus(number1, number2){
return number1 % number2
}
// function = getListOfToken
// input = expression
// output = list of token
// step1:expression theke ekta kore character nibo
// step2:character jodi digit hoy tahole puro number ta token hishebe save korbo and next index e jabo
// step3:chaaracter jodi operator hoy tahole token e save korbo and next index e jabo
// step3:list of token return korbo
function getListOfToken(expression){
listOfToken = [];
nextIndex = 0;
while(nextIndex < expression.length){
[token, nextIndex] = getNextToken(expression, nextIndex);
listOfToken.push(token)
}
return listOfToken
}
expression = "500+20-1";
console.log(getListOfToken(expression))
// 1.console.log(getListOfToken(expression)):
// 1.expression = "500+20-1"
// 2.listOfToken = [];
// 3.nextIndex = 0;
// 4. while(nextIndex < expression.length)
// 5. while(0 < 8)
// 6.while true{
// 1.[token, nextIndex] = getNextToken(expression, nextIndex);
// 2.[token, nextIndex] = getNextToken("500+20-1", 0);
// 3.[token, nextIndex] = [500, 3]
// 4.listOfToken.push(token)
// 5.listOfToken.push(500)
// 6.listOfToken = [500]
// }
// 7.while(nextIndex < expression.length)
// 8.while(3 < expression.length)
// 9.while(3 < 8)
// function = getNextToken
// input = expression , index
// output = a new token starting at index , next index
// step1:if expression[index] is a digit :
// return getNextNumber(index, expression)
// step2:else return expression[index], index++
function getNextToken(expression, index){
if (isNaN(expression[index]) == false){
return getNumber(expression, index)
}else {
return [expression[index], ++index]}
}
// 1.getNextToken(expression, 3):
// 1.expression = "500+20-1", index = 3
// 2.if (isNaN(expression[index]) == false)
// 3.if (isNaN(expression[3]) == false)
// 4.if ((isNaN("+") == false))
// 5.if (true == false)
// 6.if (false)
// 7.else{
// 1.return [expression[index], ++index]
// 2.return [expression[3], ++3]
// 3.return [+, 4]
// }
// test case 1: "5+6+7", index = 0
// test case 2:"50+6+7" , index = 0
// test case 3:"5+6+70" , index = 4
// test case 4:"", index = 0
// test case 5:"50", index = 2
// function = getNumber
// input = expression, index
// output = number, nextIndex
// step1: initialize number = 0
// step2:while expression[index] is a digit then number = number*10+ float(expression[index])
// step3:return number , index++
function getNumber(expression, index){
var number = 0;
while(isNaN(expression[index]) == false){
number =number*10 + parseFloat(expression[index]);
index++;
}
return [number, index];
}
// 1.getNumber(expression, index):
// expression = "500+20-1", index = 0;
// 2.var number = 0;
// 3.while (isNaN(expression[index]) == false)
// 4.while((isNaN(expression[0]) == false)
// 5.while(isNaN("5") == false)
// 6.while (false== false)
// 7.while(true)
// 1.number =number*10 + parseFloat(expression[index])
// 2.number =0*10 + parseFloat(expression[0])
// 3.number =0 + parseFloat("5")
// 4.number =5.00
// 5.index = 0+1
// 6.index = 1
// function = allClear
// input = nothing
// output = nothing
// step1: input string e ja ache ta clear korbo and screen e show korbo
function allClear(){
inputString = "";
document.getElementById("screen").innerHTML = inputString;
document.getElementById("showResult").innerHTML = "";
}
// function = clearLastCharacter
// input = nothing
// output = nothing
// step1: input string er last character tar ag porjonto slice kore inputString er moddhe rakhbo
function clearLastCharacter(){
inputString = inputString.slice(0, (inputString.length-1) );
document.getElementById("screen").innerHTML = inputString;
}