From 3206079cd7419645fa6dabb99cbd55a3bf628aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=9D=B0?= <3522851519@qq.com> Date: Wed, 9 Oct 2024 11:44:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 script.js diff --git a/script.js b/script.js new file mode 100644 index 0000000..f637294 --- /dev/null +++ b/script.js @@ -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 \ No newline at end of file