当前位置:首页 > JavaScript

js实现计算器连续加减

2026-03-02 02:46:26JavaScript

实现连续加减的计算器功能

在JavaScript中实现一个能够处理连续加减的计算器,需要关注表达式解析和运算顺序。以下是实现方法:

基础实现方法

使用eval()函数直接计算字符串表达式:

let expression = '';
function handleButtonClick(value) {
  expression += value;
  document.getElementById('display').value = expression;
}

function calculate() {
  try {
    const result = eval(expression);
    document.getElementById('display').value = result;
    expression = result.toString();
  } catch (error) {
    document.getElementById('display').value = 'Error';
    expression = '';
  }
}

function clearDisplay() {
  expression = '';
  document.getElementById('display').value = '';
}

不使用eval的安全实现

通过解析表达式逐步计算:

let currentValue = 0;
let pendingOperation = null;
let displayValue = '0';

function inputDigit(digit) {
  if (displayValue === '0') {
    displayValue = digit;
  } else {
    displayValue += digit;
  }
  updateDisplay();
}

function handleOperation(nextOperator) {
  const inputValue = parseFloat(displayValue);

  if (pendingOperation) {
    currentValue = performCalculation(currentValue, inputValue, pendingOperation);
  } else {
    currentValue = inputValue;
  }

  displayValue = '0';
  pendingOperation = nextOperator;
  updateDisplay();
}

function performCalculation(firstOperand, secondOperand, operation) {
  switch(operation) {
    case '+':
      return firstOperand + secondOperand;
    case '-':
      return firstOperand - secondOperand;
    default:
      return secondOperand;
  }
}

function updateDisplay() {
  document.getElementById('display').value = displayValue;
}

function clearAll() {
  currentValue = 0;
  pendingOperation = null;
  displayValue = '0';
  updateDisplay();
}

处理连续运算的改进方案

维护一个运算队列来支持更复杂的连续运算:

const calculator = {
  displayValue: '0',
  firstOperand: null,
  waitingForSecondOperand: false,
  operator: null,

  inputDigit(digit) {
    const { displayValue, waitingForSecondOperand } = this;

    if (waitingForSecondOperand) {
      this.displayValue = digit;
      this.waitingForSecondOperand = false;
    } else {
      this.displayValue = displayValue === '0' ? digit : displayValue + digit;
    }
  },

  handleOperator(nextOperator) {
    const { firstOperand, displayValue, operator } = this;
    const inputValue = parseFloat(displayValue);

    if (operator && this.waitingForSecondOperand) {
      this.operator = nextOperator;
      return;
    }

    if (firstOperand === null) {
      this.firstOperand = inputValue;
    } else if (operator) {
      const result = this.calculate(firstOperand, inputValue, operator);
      this.displayValue = String(result);
      this.firstOperand = result;
    }

    this.waitingForSecondOperand = true;
    this.operator = nextOperator;
  },

  calculate(firstOperand, secondOperand, operator) {
    switch(operator) {
      case '+': return firstOperand + secondOperand;
      case '-': return firstOperand - secondOperand;
      default: return secondOperand;
    }
  },

  reset() {
    this.displayValue = '0';
    this.firstOperand = null;
    this.waitingForSecondOperand = false;
    this.operator = null;
  }
};

注意事项

  • 使用eval()虽然简单但有安全风险,不建议在生产环境使用
  • 处理浮点数运算时可能出现精度问题,考虑使用toFixed()限制小数位数
  • 对于更复杂的表达式,可以考虑使用逆波兰算法或构建语法树
  • 添加输入验证防止非法字符输入

以上实现方案可以根据具体需求进行调整,第一种方法适合简单场景,后两种方案更适合需要严格控制的商业应用。

js实现计算器连续加减

标签: 计算器加减
分享给朋友:

相关文章

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculat…

vue实现数字加减

vue实现数字加减

Vue 实现数字加减功能 在 Vue 中实现数字加减功能可以通过数据绑定、方法调用和事件处理来实现。以下是几种常见的实现方式: 使用 data 和 methods 在 Vue 实例中定义一个数字变…

vue实现自动加减

vue实现自动加减

Vue实现自动加减功能 自动加减功能通常用于计数器、购物车数量选择等场景。以下是几种常见的实现方式: 基础实现(使用v-model和methods) <template> <d…

vue实现计算器

vue实现计算器

Vue 实现计算器的方法 使用 Vue 实现计算器可以充分利用其响应式数据和组件化特性。以下是一个简单的实现方案: 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目: npm…

vue实现加减按钮

vue实现加减按钮

实现加减按钮的基本思路 在Vue中实现加减按钮功能,通常涉及数据绑定和事件处理。通过v-model绑定数值,使用v-on或@监听按钮点击事件,修改绑定的数据值。 基础实现代码示例 <temp…

css制作计算器界面

css制作计算器界面

CSS制作计算器界面 基本结构 使用HTML创建计算器的基本结构,包含显示屏和按钮区域。显示屏用于显示输入和结果,按钮区域包含数字、运算符和功能键。 <div class="calculat…