| <!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> |
| <script src="https://cdn.tailwindcss.com"></script> |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> |
| <style> |
| .calculator { |
| box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2); |
| transition: all 0.3s ease; |
| } |
| .calculator:hover { |
| box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3); |
| } |
| .btn { |
| transition: all 0.2s ease; |
| transform: scale(1); |
| } |
| .btn:active { |
| transform: scale(0.95); |
| } |
| .display { |
| min-height: 100px; |
| word-wrap: break-word; |
| word-break: break-all; |
| } |
| .theme-toggle { |
| transition: all 0.3s ease; |
| } |
| </style> |
| </head> |
| <body class="bg-gray-100 min-h-screen flex items-center justify-center p-4"> |
| <div class="calculator bg-white rounded-3xl overflow-hidden w-full max-w-md"> |
| |
| <div class="flex justify-between items-center p-4 bg-gradient-to-r from-blue-500 to-purple-600"> |
| <h1 class="text-white font-bold text-xl">CalcPro</h1> |
| <button id="themeToggle" class="theme-toggle bg-white bg-opacity-20 rounded-full p-2 text-white"> |
| <i class="fas fa-moon"></i> |
| </button> |
| </div> |
| |
| |
| <div class="display p-4 bg-gray-50 text-right"> |
| <div id="previousOperand" class="text-gray-500 text-lg h-6"></div> |
| <div id="currentOperand" class="text-3xl font-semibold"></div> |
| </div> |
| |
| |
| <div class="grid grid-cols-4 gap-2 p-4 bg-white"> |
| |
| <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="clear">AC</button> |
| <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="delete"> |
| <i class="fas fa-backspace"></i> |
| </button> |
| <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="percentage">%</button> |
| <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="divide">÷</button> |
| |
| |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="7">7</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="8">8</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="9">9</button> |
| <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="multiply">×</button> |
| |
| |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="4">4</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="5">5</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="6">6</button> |
| <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="subtract">-</button> |
| |
| |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="1">1</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="2">2</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="3">3</button> |
| <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="add">+</button> |
| |
| |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium col-span-2" data-number="0">0</button> |
| <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number=".">.</button> |
| <button class="btn bg-purple-500 hover:bg-purple-600 text-white rounded-lg p-4 text-xl font-medium" data-action="equals">=</button> |
| </div> |
| </div> |
|
|
| <script> |
| document.addEventListener('DOMContentLoaded', () => { |
| |
| class Calculator { |
| constructor(previousOperandElement, currentOperandElement) { |
| this.previousOperandElement = previousOperandElement; |
| this.currentOperandElement = currentOperandElement; |
| this.clear(); |
| } |
| |
| clear() { |
| this.currentOperand = '0'; |
| this.previousOperand = ''; |
| this.operation = undefined; |
| } |
| |
| delete() { |
| this.currentOperand = this.currentOperand.toString().slice(0, -1); |
| if (this.currentOperand === '') { |
| this.currentOperand = '0'; |
| } |
| } |
| |
| appendNumber(number) { |
| if (number === '.' && this.currentOperand.includes('.')) return; |
| if (this.currentOperand === '0' && number !== '.') { |
| this.currentOperand = number.toString(); |
| } else { |
| this.currentOperand = this.currentOperand.toString() + number.toString(); |
| } |
| } |
| |
| 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; |
| case '%': |
| computation = prev % current; |
| break; |
| default: |
| return; |
| } |
| |
| this.currentOperand = computation.toString(); |
| this.operation = undefined; |
| this.previousOperand = ''; |
| } |
| |
| getPercentage() { |
| this.currentOperand = (parseFloat(this.currentOperand) / 100).toString(); |
| } |
| |
| updateDisplay() { |
| this.currentOperandElement.innerText = this.currentOperand; |
| if (this.operation != null) { |
| this.previousOperandElement.innerText = |
| `${this.previousOperand} ${this.operation}`; |
| } else { |
| this.previousOperandElement.innerText = ''; |
| } |
| } |
| } |
| |
| |
| const numberButtons = document.querySelectorAll('[data-number]'); |
| const operationButtons = document.querySelectorAll('[data-action]'); |
| const equalsButton = document.querySelector('[data-action="equals"]'); |
| const deleteButton = document.querySelector('[data-action="delete"]'); |
| const clearButton = document.querySelector('[data-action="clear"]'); |
| const percentageButton = document.querySelector('[data-action="percentage"]'); |
| const previousOperandElement = document.getElementById('previousOperand'); |
| const currentOperandElement = document.getElementById('currentOperand'); |
| const themeToggle = document.getElementById('themeToggle'); |
| |
| const calculator = new Calculator(previousOperandElement, currentOperandElement); |
| |
| |
| numberButtons.forEach(button => { |
| button.addEventListener('click', () => { |
| calculator.appendNumber(button.innerText); |
| calculator.updateDisplay(); |
| }); |
| }); |
| |
| operationButtons.forEach(button => { |
| button.addEventListener('click', () => { |
| const action = button.getAttribute('data-action'); |
| |
| if (action === 'clear') { |
| calculator.clear(); |
| } else if (action === 'delete') { |
| calculator.delete(); |
| } else if (action === 'percentage') { |
| calculator.getPercentage(); |
| } else if (action === 'equals') { |
| calculator.compute(); |
| } else { |
| calculator.chooseOperation(button.innerText); |
| } |
| |
| calculator.updateDisplay(); |
| }); |
| }); |
| |
| |
| document.addEventListener('keydown', (e) => { |
| if (e.key >= 0 && e.key <= 9) { |
| calculator.appendNumber(e.key); |
| calculator.updateDisplay(); |
| } else if (e.key === '.') { |
| calculator.appendNumber(e.key); |
| calculator.updateDisplay(); |
| } else if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') { |
| calculator.chooseOperation( |
| e.key === '*' ? '×' : |
| e.key === '/' ? '÷' : e.key |
| ); |
| calculator.updateDisplay(); |
| } else if (e.key === '%') { |
| calculator.getPercentage(); |
| 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') { |
| calculator.clear(); |
| calculator.updateDisplay(); |
| } |
| }); |
| |
| |
| let darkMode = false; |
| themeToggle.addEventListener('click', () => { |
| darkMode = !darkMode; |
| if (darkMode) { |
| document.body.classList.add('bg-gray-900'); |
| document.body.classList.remove('bg-gray-100'); |
| document.querySelector('.calculator').classList.add('bg-gray-800'); |
| document.querySelector('.calculator').classList.remove('bg-white'); |
| document.querySelector('.display').classList.add('bg-gray-700', 'text-white'); |
| document.querySelector('.display').classList.remove('bg-gray-50'); |
| document.querySelectorAll('.btn').forEach(btn => { |
| if (!btn.classList.contains('bg-blue-500') && !btn.classList.contains('bg-purple-500')) { |
| btn.classList.add('bg-gray-600', 'text-white'); |
| btn.classList.remove('bg-gray-100', 'bg-gray-200', 'bg-gray-300'); |
| } |
| }); |
| themeToggle.innerHTML = '<i class="fas fa-sun"></i>'; |
| } else { |
| document.body.classList.add('bg-gray-100'); |
| document.body.classList.remove('bg-gray-900'); |
| document.querySelector('.calculator').classList.add('bg-white'); |
| document.querySelector('.calculator').classList.remove('bg-gray-800'); |
| document.querySelector('.display').classList.add('bg-gray-50'); |
| document.querySelector('.display').classList.remove('bg-gray-700', 'text-white'); |
| document.querySelectorAll('.btn').forEach(btn => { |
| if (!btn.classList.contains('bg-blue-500') && !btn.classList.contains('bg-purple-500')) { |
| btn.classList.remove('bg-gray-600', 'text-white'); |
| if (btn.getAttribute('data-action') === 'clear' || |
| btn.getAttribute('data-action') === 'delete' || |
| btn.getAttribute('data-action') === 'percentage') { |
| btn.classList.add('bg-gray-200'); |
| } else { |
| btn.classList.add('bg-gray-100'); |
| } |
| } |
| }); |
| themeToggle.innerHTML = '<i class="fas fa-moon"></i>'; |
| } |
| }); |
| }); |
| </script> |
| <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=sparkhousecreative/calculator" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body> |
| </html> |