Spaces:
Running
Running
File size: 14,649 Bytes
e202a45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
<!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> |