calculator-anycoder / index.html
akhaliq's picture
akhaliq HF Staff
Upload index.html with huggingface_hub
7a03de1 verified
Raw
History Blame Contribute Delete
11.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Calculator</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
display: grid;
place-items: center;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
padding: 20px;
}
.calculator {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 30px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
border: 1px solid rgba(255, 255, 255, 0.18);
max-width: 350px;
width: 100%;
}
.display {
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 20px;
margin-bottom: 20px;
text-align: right;
min-height: 80px;
display: flex;
flex-direction: column;
justify-content: center;
box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
}
.previous-operand {
color: rgba(0, 0, 0, 0.5);
font-size: 1.2rem;
min-height: 1.5rem;
overflow: hidden;
}
.current-operand {
color: #000;
font-size: 2rem;
font-weight: 500;
word-wrap: break-word;
overflow: hidden;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
border: none;
outline: none;
background: rgba(255, 255, 255, 0.2);
color: white;
font-size: 1.5rem;
padding: 25px;
border-radius: 15px;
cursor: pointer;
transition: all 0.3s ease;
backdrop-filter: blur(5px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
button:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
button:active {
transform: translateY(0);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
.operator {
background: rgba(255, 149, 0, 0.8);
font-weight: bold;
}
.operator:hover {
background: rgba(255, 149, 0, 0.9);
}
.equals {
background: rgba(0, 255, 127, 0.8);
font-weight: bold;
}
.equals:hover {
background: rgba(0, 255, 127, 0.9);
}
.clear, .delete {
background: rgba(255, 59, 48, 0.8);
font-weight: bold;
}
.clear:hover, .delete:hover {
background: rgba(255, 59, 48, 0.9);
}
.span-2 {
grid-column: span 2;
}
@media (max-width: 400px) {
.calculator {
padding: 20px;
}
button {
padding: 20px;
font-size: 1.3rem;
}
}
</style>
</head>
<body>
<div class="calculator">
<div class="display">
<div class="previous-operand" data-previous-operand></div>
<div class="current-operand" data-current-operand>0</div>
</div>
<div class="buttons">
<button class="clear span-2" data-clear>C</button>
<button class="delete" data-delete></button>
<button class="operator" data-operation>÷</button>
<button data-number>7</button>
<button data-number>8</button>
<button data-number>9</button>
<button class="operator" data-operation>×</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button class="operator" data-operation>-</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button class="operator" data-operation>+</button>
<button data-number>0</button>
<button data-number>.</button>
<button class="equals span-2" data-equals>=</button>
</div>
</div>
<script>
class Calculator {
constructor(previousOperandElement, currentOperandElement) {
this.previousOperandElement = previousOperandElement;
this.currentOperandElement = currentOperandElement;
this.clear();
}
clear() {
this.currentOperand = '0';
this.previousOperand = '';
this.operation = undefined;
}
delete() {
if (this.currentOperand === '0') return;
if (this.currentOperand.length === 1) {
this.currentOperand = '0';
} else {
this.currentOperand = this.currentOperand.slice(0, -1);
}
}
appendNumber(number) {
if (number === '.' && this.currentOperand.includes('.')) return;
if (this.currentOperand === '0' && number !== '.') {
this.currentOperand = number;
} else {
this.currentOperand = this.currentOperand + number;
}
}
chooseOperation(operation) {
if (this.currentOperand === '') return;
if (this.previousOperand !== '') {
this.compute();
}
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = '';
}
compute() {
let computation;
const prev = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (this.operation) {
case '+':
computation = prev + current;
break;
case '-':
computation = prev - current;
break;
case '×':
computation = prev * current;
break;
case '÷':
computation = prev / current;
break;
default:
return;
}
this.currentOperand = computation.toString();
this.operation = undefined;
this.previousOperand = '';
}
getDisplayNumber(number) {
const stringNumber = number.toString();
const integerDigits = parseFloat(stringNumber.split('.')[0]);
const decimalDigits = stringNumber.split('.')[1];
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = '';
} else {
integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 });
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`;
} else {
return integerDisplay;
}
}
updateDisplay() {
this.currentOperandElement.innerText = this.getDisplayNumber(this.currentOperand);
if (this.operation != null) {
this.previousOperandElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
} else {
this.previousOperandElement.innerText = '';
}
}
}
const previousOperandElement = document.querySelector('[data-previous-operand]');
const currentOperandElement = document.querySelector('[data-current-operand]');
const numberButtons = document.querySelectorAll('[data-number]');
const operationButtons = document.querySelectorAll('[data-operation]');
const equalsButton = document.querySelector('[data-equals]');
const deleteButton = document.querySelector('[data-delete]');
const clearButton = document.querySelector('[data-clear]');
const calculator = new Calculator(previousOperandElement, currentOperandElement);
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.innerText);
calculator.updateDisplay();
});
});
operationButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.chooseOperation(button.innerText);
calculator.updateDisplay();
});
});
equalsButton.addEventListener('click', () => {
calculator.compute();
calculator.updateDisplay();
});
clearButton.addEventListener('click', () => {
calculator.clear();
calculator.updateDisplay();
});
deleteButton.addEventListener('click', () => {
calculator.delete();
calculator.updateDisplay();
});
// Keyboard support
document.addEventListener('keydown', e => {
if (e.key >= '0' && e.key <= '9') {
calculator.appendNumber(e.key);
calculator.updateDisplay();
} else if (e.key === '.') {
calculator.appendNumber('.');
calculator.updateDisplay();
} else if (e.key === '+' || e.key === '-') {
calculator.chooseOperation(e.key);
calculator.updateDisplay();
} else if (e.key === '*') {
calculator.chooseOperation('×');
calculator.updateDisplay();
} else if (e.key === '/') {
e.preventDefault();
calculator.chooseOperation('÷');
calculator.updateDisplay();
} else if (e.key === 'Enter' || e.key === '=') {
calculator.compute();
calculator.updateDisplay();
} else if (e.key === 'Backspace') {
calculator.delete();
calculator.updateDisplay();
} else if (e.key === 'Escape' || e.key === 'c' || e.key === 'C') {
calculator.clear();
calculator.updateDisplay();
}
});
</script>
</body>
</html>