From 08e5c1cc9b5fc32ddc45aa9b608ca7fd87c24e3e Mon Sep 17 00:00:00 2001 From: Jayendra Parmar <88284160+Jp88Programmer@users.noreply.github.com> Date: Tue, 16 May 2023 23:03:18 +0530 Subject: [PATCH 1/2] Create calculator.txt --- Awesome Javascript Projects_Scripts/Calculator/calculator.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Awesome Javascript Projects_Scripts/Calculator/calculator.txt diff --git a/Awesome Javascript Projects_Scripts/Calculator/calculator.txt b/Awesome Javascript Projects_Scripts/Calculator/calculator.txt new file mode 100644 index 000000000..71edc4549 --- /dev/null +++ b/Awesome Javascript Projects_Scripts/Calculator/calculator.txt @@ -0,0 +1 @@ +this is calclator which create using html and javascript logic From 31d2698a0daf344823550fe9ea2d8e989d9d3277 Mon Sep 17 00:00:00 2001 From: Jayendra Parmar <88284160+Jp88Programmer@users.noreply.github.com> Date: Tue, 16 May 2023 23:04:07 +0530 Subject: [PATCH 2/2] Add calculator application --- .../Calculator/Calculator/index.html | 128 +++++++++++++ .../Calculator/Calculator/index.js | 175 ++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100644 Awesome Javascript Projects_Scripts/Calculator/Calculator/index.html create mode 100644 Awesome Javascript Projects_Scripts/Calculator/Calculator/index.js diff --git a/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.html b/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.html new file mode 100644 index 000000000..b8a3c9b12 --- /dev/null +++ b/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.html @@ -0,0 +1,128 @@ + + + + + + + + + + Calculator + + + + +

Calculator Using Javascript

+
+
+ +
+
AC
+
+
1
+
2
+
3
+
+
+
4
+
5
+
6
+
-
+
7
+
8
+
9
+
*
+
.
+
0
+
=
+
/
+ + +
+
+ + + + \ No newline at end of file diff --git a/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.js b/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.js new file mode 100644 index 000000000..71a295ac5 --- /dev/null +++ b/Awesome Javascript Projects_Scripts/Calculator/Calculator/index.js @@ -0,0 +1,175 @@ +// the Calculator application to use most of devices + +console.log('calculator') + +// to scan all numbers and notation +let numbers = document.getElementsByClassName('numbers') +let operators = document.getElementsByClassName('operator') +let screen = document.getElementById('screen') + +// any numbers click than stor numbers as string +// any numbers click than show the screen of numbers +for (const number of numbers) { + + number.addEventListener('click', () => { + + let num = parseInt(number.innerText) + // console.log(num, typeof (num)) + screen.value += num + }) + + // number.addEventListener('keypress', (e) => { + + // let num = parseInt(e.key) + // console.log(e.key) + // screen.value += num + +// }) +} + +// when click operator than show operator to the screen +for (const operator of operators) { + + operator.addEventListener('click', () => { + + let sign = operator.innerText + + if (sign === '=') { + }else { + screen.value += sign + } + // console.log(sign, typeof (sign)) + }) +} + +let equals = document.getElementById('=') + +// when click equals than event occur and opration perform +equals.addEventListener('click', () => { + + let postfixStr = infixToPost(screen.value) + console.log(postfixStr) + + let answer = reversePolishSolution(postfixStr) + console.log(answer, typeof (answer)) + screen.value = answer +}) + +// precedance of operator +function priorityOfOperator (operatorSign) { + if (operatorSign === '-') { + return 1 + } + else if (operatorSign === '+') { + return 1 + } + else if (operatorSign === '/') { + return 3 + } + else if (operatorSign === '*') { + return 4 + } +} + +// infix to postfix conversaion +function infixToPost (infixString) { + let stack = [] + let postfixString = '' + for (let index = 0; index < infixString.length; index++) { + const element = infixString[index] + + if ((element === '+') || (element === '-') || (element === '*') || (element === '/')) { + if (stack[0] === undefined) { + stack.push(element) + }else { + if (priorityOfOperator(element) > priorityOfOperator(stack[stack.length - 1])) { + stack.push(element) + }else { + while ((stack.length > 0) && (priorityOfOperator(element) <= priorityOfOperator(stack[stack.length - 1]))) { + postfixString += stack.pop() + } + stack.push(element) + } + } + }else { + postfixString += element + } + } + + for (let index = stack.length - 1; index >= 0; index--) { + const element = stack[index] + postfixString += element + } + + console.log(infixString, postfixString) + + return postfixString +} + +function reversePolishSolution (postfixString) { + stack = [] + ans = 0 + for (let index = 0; index < postfixString.length; index++) { + const element = postfixString[index] + + if (!((element === '+') || (element === '-') || (element === '*') || (element === '/'))) { + stack.push(parseInt(element)) + }else { + let num2 = stack.pop() + let num1 = stack.pop() + + switch (element) { + case '+': + ans = num1 + num2 + break + case '-': + ans = num1 - num2 + break + case '*': + ans = num1 * num2 + break + case '/': + ans = num1 / num2 + break + + default: + break + } + stack.push(ans) + } + } + + ans = stack.pop() + console.log(ans) + return ans +} + + +function keypress () { + for (const number of numbers) { + + // number.addEventListener('click', () => { + + let num = parseInt(number.target.innerText) + // console.log(num, typeof (num)) + screen.value += num + + // }) + + } +} + + document.addEventListener('keypress', (event) => { + + let num = event.key + console.log(num,event.key) + screen.value += num + + }, false); + +let ac = document.getElementById('ac') + +ac.addEventListener('click',()=>{ + screen.value = '' +}) +