Compare commits

..

2 Commits

@ -6,7 +6,9 @@
<OperatorButton v-for="op in operators" :key="op.operator" :operator="op.operator" @click="appendOperator(op.operator)" /> <OperatorButton v-for="op in operators" :key="op.operator" :operator="op.operator" @click="appendOperator(op.operator)" />
<button @click="appendDecimal('.')">.</button> <button @click="appendDecimal('.')">.</button>
<button @click="calculate">=</button> <button @click="calculate">=</button>
<button @click="deleteLastChar">del</button>
<button class="ac-button" @click="clearDisplay">AC</button> <button class="ac-button" @click="clearDisplay">AC</button>
</div> </div>
</div> </div>
</template> </template>
@ -29,6 +31,8 @@ const operators = [
{ operator: '%' }, { operator: '%' },
]; ];
const appendNumber = (number) => { const appendNumber = (number) => {
if (displayValue.value === '0' || displayValue.value === '表达式错误') { if (displayValue.value === '0' || displayValue.value === '表达式错误') {
displayValue.value = number; displayValue.value = number;
@ -47,6 +51,16 @@ const appendOperator = (op) => {
const calculate = () => { const calculate = () => {
displayValue.value = eval(displayValue.value); 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 = () => { const clearDisplay = () => {
@ -55,6 +69,12 @@ const clearDisplay = () => {
secondNumber.value = null; secondNumber.value = null;
operator.value = null; operator.value = null;
}; };
const deleteLastChar = () => {
if (displayValue.value.length > 0) {
displayValue.value = displayValue.value.slice(0, -1); //
}
};
</script> </script>
<style scoped> <style scoped>

Loading…
Cancel
Save