Aquí tienes el código para una calculadora científica moderna y compacta:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora Científica</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.calculator {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
padding: 20px;
max-width: 400px;
width: 100%;
}
.display {
background: rgba(0, 0, 0, 0.3);
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
text-align: right;
color: white;
font-size: 24px;
font-weight: bold;
min-height: 60px;
word-wrap: break-word;
overflow-wrap: break-word;
}
.buttons {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 10px;
}
button {
padding: 15px 8px;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: all 0.2s ease;
color: white;
background: rgba(255, 255, 255, 0.2);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
button:active {
transform: translateY(0);
}
.operator {
background: rgba(255, 107, 107, 0.8);
}
.function {
background: rgba(54, 162, 235, 0.8);
font-size: 14px;
}
.number {
background: rgba(255, 255, 255, 0.3);
}
.equals {
background: rgba(46, 204, 113, 0.8);
grid-column: span 2;
}
.clear {
background: rgba(231, 76, 60, 0.8);
}
.zero {
grid-column: span 2;
}
</style>
</head>
<body>
<div class="calculator">
<div class="display" id="display">0</div>
<div class="buttons">
<button class="clear" onclick="clearDisplay()">C</button>
<button class="clear" onclick="clearEntry()">CE</button>
<button class="function" onclick="appendToDisplay('Math.sqrt(')">√</button>
<button class="function" onclick="appendToDisplay('^')">^</button>
<button class="operator" onclick="appendToDisplay('/')">/</button>
<button class="function" onclick="appendToDisplay('Math.sin(')">sin</button>
<button class="function" onclick="appendToDisplay('Math.cos(')">cos</button>
<button class="function" onclick="appendToDisplay('Math.tan(')">tan</button>
<button class="function" onclick="appendToDisplay('(')">(</button>
<button class="function" onclick="appendToDisplay(')')">)</button>
<button class="function" onclick="appendToDisplay('Math.log10(')">log</button>
<button class="function" onclick="appendToDisplay('Math.log(')">ln</button>
<button class="function" onclick="appendToDisplay('Math.PI')">π</button>
<button class="function" onclick="appendToDisplay('Math.E')">e</button>
<button class="operator" onclick="appendToDisplay('*')">×</button>
<button class="number" onclick="appendToDisplay('7')">7</button>
<button class="number" onclick="appendToDisplay('8')">8</button>
<button class="number" onclick="appendToDisplay('9')">9</button>
<button class="operator" onclick="appendToDisplay('-')">-</button>
<button class="function" onclick="factorial()">n!</button>
<button class="number" onclick="appendToDisplay('4')">4</button>
<button class="number" onclick="appendToDisplay('5')">5</button>
<button class="number" onclick="appendToDisplay('6')">6</button>
<button class="operator" onclick="appendToDisplay('+')">+</button>
<button class="function" onclick="percentage()">%</button>
<button class="number" onclick="appendToDisplay('1')">1</button>
<button class="number" onclick="appendToDisplay('2')">2</button>
<button class="number" onclick="appendToDisplay('3')">3</button>
<button class="number" onclick="appendToDisplay('.')">.</button>
<button class="function" onclick="toggleSign()">±</button>
<button class="number zero" onclick="appendToDisplay('0')">0</button>
<button class="equals" onclick="calculate()">=</button>
</div>
</div>
<script>
let display = document.getElementById('display');
let currentInput = '0';
let shouldResetDisplay = false;
function updateDisplay() {
display.textContent = currentInput;
}
function appendToDisplay(value) {
if (shouldResetDisplay) {
currentInput = '';
shouldResetDisplay = false;
}
if (currentInput === '0' && !isNaN(value)) {
currentInput = value;
} else {
currentInput += value;
}
updateDisplay();
}
function clearDisplay() {
currentInput = '0';
updateDisplay();
}
function clearEntry() {
if (currentInput.length > 1) {
currentInput = currentInput.slice(0, -1);
} else {
currentInput = '0';
}
updateDisplay();
}
function calculate() {
try {
// Reemplazar ^ con Math.pow para potencias
let expression = currentInput.replace(/\^/g, '**');
// Evaluar la expresión
let result = eval(expression);
// Redondear para evitar problemas de precisión
result = Math.round(result * 1000000000000) / 1000000000000;
currentInput = result.toString();
shouldResetDisplay = true;
updateDisplay();
} catch (error) {
currentInput = 'Error';
shouldResetDisplay = true;
updateDisplay();
}
}
function factorial() {
try {
let num = parseInt(currentInput);
if (num < 0) {
currentInput = 'Error';
} else {
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
currentInput = result.toString();
}
shouldResetDisplay = true;
updateDisplay();
} catch (error) {
currentInput = 'Error';
shouldResetDisplay = true;
updateDisplay();
}
}
function percentage() {
try {
let result = parseFloat(currentInput) / 100;
currentInput = result.toString();
shouldResetDisplay = true;
updateDisplay();
} catch (error) {
currentInput = 'Error';
shouldResetDisplay = true;
updateDisplay();
}
}
function toggleSign() {
if (currentInput !== '0') {
if (currentInput.startsWith('-')) {
currentInput = currentInput.substring(1);
} else {
currentInput = '-' + currentInput;
}
updateDisplay();
}
}
// Soporte para teclado
document.addEventListener('keydown', function(event) {
const key = event.key;
if (!isNaN(key) || key === '.') {
appendToDisplay(key);
} else if (['+', '-', '*', '/'].includes(key)) {
appendToDisplay(key === '*' ? '*' : key);
} else if (key === 'Enter' || key === '=') {
calculate();
} else if (key === 'Escape' || key === 'c' || key === 'C') {
clearDisplay();
} else if (key === 'Backspace') {
clearEntry();
}
});
</script>
</body>
</html>
Esta calculadora científica incluye:
Características principales:
- Operaciones básicas (suma, resta, multiplicación, división)
- Funciones trigonométricas (sin, cos, tan)
- Funciones logarítmicas (log, ln)
- Constantes matemáticas (π, e)
- Raíz cuadrada y potencias
- Factorial y porcentajes
- Paréntesis para agrupación
- Cambio de signo
Diseño:
- Interfaz moderna con efecto glassmorphism
- Gradiente de fondo atractivo
- Botones con efectos hover y animaciones
- Layout responsivo que se adapta a diferentes pantallas
- Colores diferenciados por tipo de función
Funcionalidad:
- Soporte completo para teclado
- Manejo de errores
- Precisión numérica mejorada
- Interfaz intuitiva y fácil de usar
La calculadora es completamente funcional y tiene un diseño moderno y compacto que se ve bien en cualquier dispositivo.