Calculator App
Calculator App
```html
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<input type="text" id="display">
<div class="buttons">
<button class="operator" value="+">+</button>
<button class="operator" value="-">-</button>
<button class="operator" value="*">*</button>
<button class="operator" value="/">/</button>
<button class="operand" value="7">7</button>
<button class="operand" value="8">8</button>
<button class="operand" value="9">9</button>
<button class="operand" value="4">4</button>
<button class="operand" value="5">5</button>
<button class="operand" value="6">6</button>
<button class="operand" value="1">1</button>
<button class="operand" value="2">2</button>
<button class="operand" value="3">3</button>
<button class="operand" value="0">0</button>
<button class="decimal" value=".">.</button>
<button class="equals" value="=">=</button>
<button class="clear" value="C">C</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
```css
/* CSS for the calculator */
.calculator {
width: 300px;
margin: 0 auto;
padding: 20px;
background-color: #f2f2f2;
border: 1px solid #ccc;
border-radius: 5px;
}
.display {
width: 280px;
height: 50px;
margin-bottom: 20px;
padding: 10px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 24px;
text-align: right;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.button {
width: 70px;
height: 70px;
margin: 0;
padding: 0;
background-color: #eee;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 24px;
cursor: pointer;
}
.operator {
background-color: #ff9900;
color: #fff;
}
.operand {
background-color: #fff;
color: #000;
}
.decimal {
background-color: #eee;
color: #000;
}
.equals {
background-color: #009900;
color: #fff;
}
.clear {
background-color: #ff0000;
color: #fff;
}
```
```javascript
// JavaScript for the calculator
buttons.forEach((button) => {
button.addEventListener('click', () => {
const value = button.value;
if (/\d/.test(value)) {
// Handle operand input
if (firstOperand === null) {
firstOperand = value;
} else if (secondOperand === null) {
secondOperand = value;
} else {
// Ignore additional operand input
}
updateDisplay();
} else if (value === '.') {
// Handle decimal point input
if (firstOperand && !firstOperand.includes('.')) {
firstOperand += value;
} else if (secondOperand && !secondOperand.includes('.')) {
secondOperand += value;
}
updateDisplay();
} else if (value === 'C') {
// Handle clear input
firstOperand = null;
secondOperand = null;
operator = null;
result = null;
updateDisplay();
} else if (value === '=') {
// Handle equals input
if (firstOperand && secondOperand && operator) {
result = operate(operator, firstOperand, secondOperand);
updateDisplay(result);
firstOperand = null;
secondOperand = null;
operator = null;
}
} else {
// Handle operator input
if (firstOperand && operator === null) {
operator = value;
}
updateDisplay();
}
});
});
function operate(operator, a, b) {
a = Number(a);
b = Number(b);
switch (operator) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return null;
}
}
```input