Código para Práctica6.html
NOTA: PARA ÉSTA PRÁCTICA SE UTILIZAN DOS COMPLEMENTOS
Busca el código
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora Mejorada</title>
<link rel="stylesheet" href="styless.css">
</head>
<body>
<div class="calculator">
<h3>Calculadora Mejorada</h3>
<input type="text" id="display" disabled placeholder="0">
<div class="buttons">
<!-- Fila 1 -->
<button class="btn" onclick="clearDisplay()">C</button>
<button class="btn" onclick="deleteLast()">←</button>
<button class="btn" onclick="appendOperator('/')">/</button>
<button class="btn" onclick="appendOperator('*')">×</button>
<!-- Fila 2 -->
<button class="btn" onclick="appendNumber('7')">7</button>
<button class="btn" onclick="appendNumber('8')">8</button>
<button class="btn" onclick="appendNumber('9')">9</button>
<button class="btn" onclick="appendOperator('-')">−</button>
<!-- Fila 3 -->
<button class="btn" onclick="appendNumber('4')">4</button>
<button class="btn" onclick="appendNumber('5')">5</button>
<button class="btn" onclick="appendNumber('6')">6</button>
<button class="btn" onclick="appendOperator('+')">+</button>
<!-- Fila 4 -->
<button class="btn" onclick="appendNumber('1')">1</button>
<button class="btn" onclick="appendNumber('2')">2</button>
<button class="btn" onclick="appendNumber('3')">3</button>
<button class="btn equals" onclick="calculate()">=</button>
<!-- Fila 5 -->
<button class="btn zero" onclick="appendNumber('0')">0</button>
<button class="btn" onclick="appendOperator('.')">.</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Código para el Complemento Styless.css
body {
font-family: Arial, sans-serif;
background-color: #f3f4f6;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background: #ffffff;
border-radius: 10px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
width: 300px;
padding: 20px;
}
#display {
width: 100%;
height: 50px;
border: none;
background: #f3f4f6;
border-radius: 5px;
text-align: right;
font-size: 1.5rem;
padding: 10px;
margin-bottom: 20px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.btn {
background: #e0e0e0;
border: none;
border-radius: 5px;
padding: 15px;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}
.btn:hover {
background: #d1d1d1;
}
.btn.zero {
grid-column: span 2;
}
.equals {
background: #4caf50;
color: white;
}
.equals:hover {
background: #45a049;
}
Código Complemento para script.js
const display = document.getElementById('display');
function appendNumber(number) {
if (display.value === '0') {
display.value = number;
} else {
display.value += number;
}
}
function appendOperator(operator) {
const lastChar = display.value.slice(-1);
if (['+', '-', '*', '/'].includes(lastChar)) {
display.value = display.value.slice(0, -1) + operator;
} else {
display.value += operator;
}
}
function clearDisplay() {
display.value = '0';
}
function deleteLast() {
if (display.value.length === 1 || display.value === '0') {
display.value = '0';
} else {
display.value = display.value.slice(0, -1);
}
}
function calculate() {
try {
display.value = eval(display.value) || '0';
} catch (e) {
display.value = 'Error';
}
}
No hay comentarios.:
Publicar un comentario