calculator / index.html
Mishak's picture
Add 2 files
e202a45 verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scientific 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 10px 30px rgba(0, 0, 0, 0.2);
transition: all 0.3s ease;
}
.calculator:hover {
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.25);
}
.btn {
transition: all 0.2s ease;
transform: translateY(0);
}
.btn:active {
transform: translateY(2px);
}
.display {
min-height: 80px;
word-wrap: break-word;
word-break: break-all;
}
.scientific-btn {
transition: all 0.3s ease;
opacity: 0;
max-height: 0;
overflow: hidden;
}
.show-scientific .scientific-btn {
opacity: 1;
max-height: 100px;
}
.history-panel {
transition: all 0.3s ease;
transform: translateX(100%);
}
.show-history .history-panel {
transform: translateX(0);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<div class="calculator bg-gray-800 rounded-2xl p-4 w-full max-w-md relative overflow-hidden">
<!-- Display Area -->
<div class="display bg-gray-900 text-right p-4 rounded-lg mb-4 text-white font-mono">
<div id="history" class="text-gray-400 text-sm min-h-4"></div>
<div id="current" class="text-3xl font-semibold">0</div>
</div>
<!-- History Toggle -->
<button id="historyToggle" class="absolute top-4 left-4 text-gray-400 hover:text-white">
<i class="fas fa-history"></i>
</button>
<!-- History Panel -->
<div class="history-panel absolute top-0 left-0 w-full h-full bg-gray-700 p-4 z-10 overflow-y-auto">
<div class="flex justify-between items-center mb-4">
<h3 class="text-white font-bold">Calculation History</h3>
<button id="closeHistory" class="text-gray-400 hover:text-white">
<i class="fas fa-times"></i>
</button>
</div>
<div id="historyList" class="text-gray-300 space-y-2"></div>
</div>
<!-- Mode Toggle -->
<div class="flex justify-between mb-4">
<button id="modeToggle" class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-1 rounded-lg text-sm">
<i class="fas fa-flask"></i> Scientific
</button>
<div class="flex space-x-2">
<button id="clear" class="bg-red-600 hover:bg-red-700 text-white px-3 py-1 rounded-lg text-sm">
AC
</button>
<button id="backspace" class="bg-gray-600 hover:bg-gray-700 text-white px-3 py-1 rounded-lg text-sm">
<i class="fas fa-backspace"></i>
</button>
</div>
</div>
<!-- Calculator Buttons -->
<div class="grid grid-cols-5 gap-2">
<!-- Scientific Buttons (First Row) -->
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="sin(">sin</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="cos(">cos</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="tan(">tan</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="log(">log</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="ln(">ln</button>
<!-- Scientific Buttons (Second Row) -->
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="asin(">sin⁻¹</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="acos(">cos⁻¹</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="atan(">tan⁻¹</button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="√("></button>
<button class="scientific-btn btn bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-lg" data-value="^">^</button>
<!-- Standard Buttons -->
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="7">7</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="8">8</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="9">9</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="/">/</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="%">%</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="4">4</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="5">5</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="6">6</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="*">×</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="π">π</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="1">1</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="2">2</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="3">3</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="-">-</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="(">(</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value="0">0</button>
<button class="btn bg-gray-600 hover:bg-gray-700 text-white p-2 rounded-lg" data-value=".">.</button>
<button class="btn bg-green-600 hover:bg-green-700 text-white p-2 rounded-lg col-span-2" id="equals">=</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value="+">+</button>
<button class="btn bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg" data-value=")">)</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Calculator state
let currentInput = '0';
let previousInput = '';
let operation = null;
let resetScreen = false;
const calculationHistory = [];
// DOM elements
const currentDisplay = document.getElementById('current');
const historyDisplay = document.getElementById('history');
const historyList = document.getElementById('historyList');
const historyToggle = document.getElementById('historyToggle');
const closeHistory = document.getElementById('closeHistory');
const clearButton = document.getElementById('clear');
const backspaceButton = document.getElementById('backspace');
const equalsButton = document.getElementById('equals');
const modeToggle = document.getElementById('modeToggle');
const numberButtons = document.querySelectorAll('.btn[data-value]');
const calculator = document.querySelector('.calculator');
// Initialize
updateDisplay();
// Event listeners
numberButtons.forEach(button => {
button.addEventListener('click', () => {
appendNumber(button.dataset.value);
});
});
equalsButton.addEventListener('click', evaluate);
clearButton.addEventListener('click', clear);
backspaceButton.addEventListener('click', deleteNumber);
modeToggle.addEventListener('click', toggleScientificMode);
historyToggle.addEventListener('click', showHistory);
closeHistory.addEventListener('click', hideHistory);
// Keyboard support
document.addEventListener('keydown', (e) => {
if (e.key >= '0' && e.key <= '9') appendNumber(e.key);
else if (e.key === '.') appendNumber('.');
else if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') {
appendNumber(e.key);
}
else if (e.key === 'Enter' || e.key === '=') evaluate();
else if (e.key === 'Escape') clear();
else if (e.key === 'Backspace') deleteNumber();
else if (e.key === '(' || e.key === ')') appendNumber(e.key);
else if (e.key === '^') appendNumber('^');
});
// Functions
function appendNumber(number) {
if (currentInput === '0' || resetScreen) {
currentInput = '';
resetScreen = false;
}
// Handle special constants
if (number === 'π') {
currentInput += Math.PI.toString();
} else {
currentInput += number;
}
updateDisplay();
}
function clear() {
currentInput = '0';
previousInput = '';
operation = null;
updateDisplay();
}
function deleteNumber() {
if (currentInput.length === 1 || (currentInput.length === 2 && currentInput.startsWith('-'))) {
currentInput = '0';
} else {
currentInput = currentInput.slice(0, -1);
}
updateDisplay();
}
function evaluate() {
if (resetScreen) return;
try {
// Replace special functions and constants
let expression = currentInput
.replace(/sin\(/g, 'Math.sin(')
.replace(/cos\(/g, 'Math.cos(')
.replace(/tan\(/g, 'Math.tan(')
.replace(/asin\(/g, 'Math.asin(')
.replace(/acos\(/g, 'Math.acos(')
.replace(/atan\(/g, 'Math.atan(')
.replace(/log\(/g, 'Math.log10(')
.replace(/ln\(/g, 'Math.log(')
.replace(/√\(/g, 'Math.sqrt(')
.replace(/\^/g, '**')
.replace(/π/g, 'Math.PI');
// Evaluate the expression
const result = eval(expression);
// Add to history
calculationHistory.push(`${currentInput} = ${result}`);
updateHistory();
// Update display
previousInput = currentInput;
currentInput = result.toString();
operation = null;
resetScreen = true;
updateDisplay();
} catch (error) {
currentInput = 'Error';
updateDisplay();
setTimeout(clear, 1500);
}
}
function updateDisplay() {
currentDisplay.textContent = currentInput;
historyDisplay.textContent = previousInput;
}
function updateHistory() {
historyList.innerHTML = '';
calculationHistory.slice().reverse().forEach(item => {
const historyItem = document.createElement('div');
historyItem.textContent = item;
historyItem.classList.add('cursor-pointer', 'hover:text-white');
historyItem.addEventListener('click', () => {
currentInput = item.split(' = ')[0];
updateDisplay();
hideHistory();
});
historyList.appendChild(historyItem);
});
}
function toggleScientificMode() {
calculator.classList.toggle('show-scientific');
if (calculator.classList.contains('show-scientific')) {
modeToggle.innerHTML = '<i class="fas fa-calculator"></i> Standard';
} else {
modeToggle.innerHTML = '<i class="fas fa-flask"></i> Scientific';
}
}
function showHistory() {
calculator.classList.add('show-history');
updateHistory();
}
function hideHistory() {
calculator.classList.remove('show-history');
}
});
</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=Mishak/calculator" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
</html>