余杰 1 month ago
parent 2a4fa8e9ae
commit 3206079cd7

@ -0,0 +1,57 @@
let currentInput = '';
let previousInput = '';
let operation = null;
function appendNumber(number) {
currentInput += number.toString();
document.getElementById('display').value = currentInput;
}
function appendDecimal(dot) {
if (!currentInput.includes(dot)) {
currentInput += dot;
document.getElementById('display').value = currentInput;
}
}
function chooseOperation(op) {
if (currentInput !== '') {
previousInput = currentInput;
operation = op;
currentInput = '';
}
}
function calculateResult(equals) {
let result;
const prev = parseFloat(previousInput);
const curr = parseFloat(currentInput);
if (isNaN(prev) || isNaN(curr)) return;
switch (operation) {
case '+':
result = prev + curr;
break;
case '-':
result = prev - curr;
break;
case '*':
result = prev * curr;
break;
case '/':
result = prev / curr;
break;
default:
return;
}
currentInput = result.toString();
document.getElementById('display').value = currentInput;
previousInput = '';
operation = null;
}
function clearDisplay() {
currentInput = '';
previousInput = '';
operation = null;
document.getElementById('display').value = '';
}s
Loading…
Cancel
Save