Oto kompletny kalkulator w jednym pliku HTML, z całą logiką i strukturą zawartą w jednym znaczniku <script>:
html
<!DOCTYPE html>
<html>
<head>
<title>Prosty Kalkulator</title>
</head>
<body>
<script>
// Dodanie stylów CSS
const style = document.createElement('style');
style.textContent = `
#calculator {
width: 250px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
font-family: Arial, sans-serif;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
#display {
width: 100%;
height: 40px;
font-size: 18px;
margin-bottom: 10px;
text-align: right;
padding-right: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.row {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
}
.button {
width: 56px;
height: 48px;
font-size: 18px;
margin: 2px;
cursor: pointer;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
transition: background-color 0.2s;
}
.button:hover {
background-color: #e0e0e0;
}
.button.operator {
background-color: #ff9800;
color: white;
}
.button.operator:hover {
background-color: #fb8c00;
}
.button.equal {
background-color: #4caf50;
color: white;
}
.button.equal:hover {
background-color: #43a047;
}
.button.clear {
background-color: #f44336;
color: white;
}
.button.clear:hover {
background-color: #e53935;
}
`;
document.head.appendChild(style);
// Tworzenie kontenera kalkulatora
const calc = document.createElement('div');
calc.id = 'calculator';
// Tworzenie wyświetlacza
const display = document.createElement('input');
display.type = 'text';
display.id = 'display';
display.disabled = true;
calc.appendChild(display);
// Przyciski kalkulatora
const buttons = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['C', '0', '.', '+'],
['=', '=', '=', '='] // Rząd z przyciskiem równości
];
// Dodawanie przycisków
buttons.forEach(row => {
const rowDiv = document.createElement('div');
rowDiv.className = 'row';
row.forEach(btnText => {
const btn = document.createElement('button');
btn.className = 'button';
btn.textContent = btnText;
// Dodanie klas specjalnych dla operatorów, C i =
if (btnText === 'C') btn.classList.add('clear');
else if (btnText === '=') btn.classList.add('equal');
else if (['+', '-', '*', '/'].includes(btnText)) btn.classList.add('operator');
btn.addEventListener('click', () => handleButton(btnText));
rowDiv.appendChild(btn);
});
calc.appendChild(rowDiv);
});
document.body.appendChild(calc);
// Logika kalkulatora
let currentInput = '';
let previousInput = '';
let operator = null;
function handleButton(value) {
if (value === 'C') {
currentInput = '';
previousInput = '';
operator = null;
} else if (value === '=') {
if (currentInput && previousInput && operator) {
if (operator === '/' && currentInput === '0') {
currentInput = 'Błąd: Dzielenie przez zero';
} else {
try {
const result = eval(`${previousInput}${operator}${currentInput}`);
currentInput = result.toString();
} catch (e) {
currentInput = 'Błąd';
}
previousInput = '';
operator = null;
}
}
} else if (['+', '-', '*', '/'].includes(value)) {
if (currentInput) {
previousInput = currentInput;
operator = value;
currentInput = '';
}
} else if (value === '.') {
if (!currentInput.includes('.')) {
currentInput += value;
}
} else if (!isNaN(value) || value === '.') {
currentInput += value;
}
updateDisplay();
}
function updateDisplay() {
document.getElementById('display').value = currentInput;
}
</script>
</body>
</html>