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); // 删除最后一个字符 + } +};