vue中实现简易计算器

演示

代码

<template>
  <div style="width: 260px; height: 75px; border: silver 1px solid">
    <div style="float: right;" :class="this.eq === '' ? 'eqBeforeCompute' : 'eqCompute'">{{ this.value }}</div><br>
    <div style="float: right; font-size: x-large; font-weight: bold">{{ this.eq }}</div>
  </div>
  <div style="width: 250px;">
    <el-button-group style="width: 250px; display: flex; align-items: center">
      <el-button style="height: 50px" class="calculator-button" text @click="compute('(')">(</el-button>
      <el-button style="height: 50px" class="calculator-button" text @click="compute(')')">)</el-button>
      <el-button style="height: 50px" class="calculator-button" text @click="compute('C')">C</el-button>
      <el-button style="height: 50px" class="calculator-button" text @click="compute('Del')">
        <el-icon><CircleClose /></el-icon>
      </el-button>
    </el-button-group>
    <el-button-group style="width: 250px; display: flex; align-items: center">
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('7')">7</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('8')">8</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('9')">9</el-button>
      <el-button type="warning" style="height: 50px; font-size: x-large" class="calculator-button" text @click="compute('÷')">÷</el-button>
    </el-button-group>
    <el-button-group style="width: 250px; display: flex; align-items: center">
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('4')">4</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('5')">5</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('6')">6</el-button>
      <el-button type="warning" style="height: 50px; font-size: x-large" class="calculator-button" text @click="compute('×')">×</el-button>
    </el-button-group>
    <el-button-group style="width: 250px; display: flex; align-items: center">
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('1')">1</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('2')">2</el-button>
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('3')">3</el-button>
      <el-button type="warning" style="height: 50px; font-size: x-large" class="calculator-button" text @click="compute('-')">-</el-button>
    </el-button-group>
    <el-button-group style="width: 250px; display: flex; align-items: center">
      <el-button style="height: 50px" class="calculator-button-num" @click="compute('0')">0</el-button>
      <el-button style="height: 50px; font-size: x-large" class="calculator-button-num" @click="compute('.')">.</el-button>
      <el-button type="warning" style="height: 50px; font-size: x-large" class="calculator-button" text @click="compute('+')">+</el-button>
      <el-button style="background: #1c84c6; height: 50px; font-size: x-large" class="calculator-button" text @click="compute('=')">=</el-button>
    </el-button-group>
    <el-button-group style="width: 250px; display: flex; align-items: center">

    </el-button-group>
  </div>
</template>
<script>
export default {
  name: "index",
  data() {
    return {
      value: '',
      eq: ''
    }
  },
  methods: {
    compute(num) {
      if (this.eq !== '') {
        this.eq = '';
        this.value = '';
      }
      if (this.isOperator(this.value.slice(-1)) && this.isOperator(num)) {
        return;
      }
      if (num === 'C') {
        this.eq = '';
        this.value = '';
      } else if (num === 'Del') {
        this.value = this.value.slice(0, -1);
      } else if (num === '=') {
        this.isBrackets(this.value)
        try {
          this.eq = eval(this.value
            .replaceAll('×', '*')
            .replaceAll('÷', '/'));
        } catch (e) {
          this.eq = '出错';
        }
        this.value = this.value + num;
      } else {
        if (this.value === '' && this.isOperator(num)) {
          return;
        }
        this.value = this.value + num
      }
    },
    isOperator(str) {
      const operators = ['+', '-', '×', '÷', '%', '.'];
      return operators.includes(str);
    },
    isBrackets(str) {
      const leftBrackets = str.split('(').length - 1;
      const rightBrackets = str.split(')').length - 1;
      if (leftBrackets !== 0 && leftBrackets !== rightBrackets) {
        for (let i = 0; i < leftBrackets; i++) {
          this.value = this.value + ')';
        }
      } else if (rightBrackets !== 0 && leftBrackets !== rightBrackets) {
        for (let i = 0; i < rightBrackets; i++) {
          this.value = '(' + this.value;
        }
      }
    }
  }
}
</script>

<style scoped>
.calculator-button {
  text-align: center;
  font-size: larger;
  font-weight: bold;
  width: 65px;
  height: 50px;
}

.calculator-button-num {
  text-align: center;
  font-size: larger;
  width: 65px;
  height: 50px;
  font-weight: bold;
}
.eqBeforeCompute {
  font-size: x-large;
}
.eqCompute {
  font-size: small;
  color: #5a5e66;
}
</style>

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值