Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project #565

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 128 additions & 0 deletions Awesome Javascript Projects_Scripts/Calculator/Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}

.container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 20%;
display: flex;
justify-content: center;
align-items: center;
background-color: black;
padding: 20px 40px;
height: auto;


}

.calculator {
display: grid;
justify-content: center;
align-items: center;
grid-template-columns: 60px 60px 60px 90px;
margin: 15px auto;
font-size: 30px;
}

.numbers {
width: 50px;
height: 50px;
background-color: rgb(152, 152, 245);
margin: 10px 15px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;

}

.operator {
width: 50px;
height: 50px;
background-color: rgb(245, 152, 152);
margin: 10px 15px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
}

.screen {
width: 300px;
height: 70px;
font-size: 30px;
text-align: right;
display: flex;
align-items: center;
background-color: white
}

.ac {
/* text-align: left; */
width: 50px;
height: 50px;
display: flex;
justify-content: left;
background-color: rgb(245, 0, 0);
color: white;
font-size: 23px;
border-radius: 50%;
margin: 10px 15px;
align-items: center;
justify-content: center;
align-self: flex-start;
margin: 20px 27px;
margin-bottom: -3px;
}
</style>

<title>Calculator</title>
</head>

<body>

<h1>Calculator Using Javascript</h1>
<div class="container">
<div>
<input type="text" class="screen" id="screen">
</div>
<div class="ac" id="ac">AC</div>
<div class="calculator">
<div class="numbers" id="number1" onkeypress="keypress()">1</div>
<div class="numbers" id="number2" onkeypress="keypress()">2</div>
<div class="numbers" id="number3" onkeypress="keypress()">3</div>
<div class="operator" id="+">+</div>
<div class="numbers" id="number4" onkeypress="keypress()">4</div>
<div class="numbers" id="number5" onkeypress="keypress()">5</div>
<div class="numbers" id="number6" onkeypress="keypress()">6</div>
<div class="operator" id="-">-</div>
<div class="numbers" id="number7" onkeypress="keypress()">7</div>
<div class="numbers" id="number8" onkeypress="keypress()">8</div>
<div class="numbers" id="number9" onkeypress="keypress()">9</div>
<div class="operator" id="*">*</div>
<div class="numbers" id="number.">.</div>
<div class="numbers" id="number0" onkeypress="keypress()">0</div>
<div class="operator" id="=">=</div>
<div class="operator" id="/">/</div>


</div>
</div>
<script src="index.js"></script>
</body>

</html>
175 changes: 175 additions & 0 deletions Awesome Javascript Projects_Scripts/Calculator/Calculator/index.js
Original file line number Diff line number Diff line change
@@ -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 = ''
})

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is calclator which create using html and javascript logic