From f8c3052705c7ff644cd77173c646131a506e79da Mon Sep 17 00:00:00 2001 From: xyj <1173560674@qq.com> Date: Wed, 16 Oct 2024 13:05:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calculator/src/components/Calculator.vue | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/calculator/src/components/Calculator.vue b/calculator/src/components/Calculator.vue index 5befdbe..9080828 100644 --- a/calculator/src/components/Calculator.vue +++ b/calculator/src/components/Calculator.vue @@ -6,7 +6,9 @@ + + @@ -29,6 +31,8 @@ const operators = [ { operator: '%' }, ]; + + const appendNumber = (number) => { if (displayValue.value === '0' || displayValue.value === '表达式错误') { displayValue.value = number; @@ -47,6 +51,16 @@ const appendOperator = (op) => { const calculate = () => { displayValue.value = eval(displayValue.value); + try { + const result = Function('"use strict"; return (' + displayValue.value + ')')(); + if (!isFinite(result)) { + displayValue.value = '错误'; + } else { + displayValue.value = result; + } + } catch (error) { + displayValue.value = '错误'; + } }; const clearDisplay = () => { @@ -55,6 +69,12 @@ const clearDisplay = () => { secondNumber.value = null; operator.value = null; }; + +const deleteLastChar = () => { + if (displayValue.value.length > 0) { + displayValue.value = displayValue.value.slice(0, -1); // 删除最后一个字符 + } +};