norCalc.vue 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<template>
  <div id="app">
    <div class="calculator" style="margin-top:0px;margin-bottom:0px;">
      <div class="results input">{{equation}}</div>
      <div class="mode">
        <button type="button" class="button" @click="append(7)">7</button>
        <button type="button" class="button" @click="append(8)">8</button>
        <button type="button" class="button" @click="append(9)">9</button>
        <button type="button" class="button" @click="append('×')">*</button>
        <button type="button" class="button" @click="clear">AC</button>

        <button type="button" class="button" @click="append(4)">4</button>
        <button type="button" class="button" @click="append(5)">5</button>
        <button type="button" class="button" @click="append(6)">6</button>
        <button type="button" class="button" @click="append('÷')">/</button>
        <button type="button" class="button" @click="calculatePercent">%</button>

        <button type="button"  class="button" @click="append(1)">1</button>
        <button type="button" class="button" @click="append(2)">2</button>
        <button type="button" class="button" @click="append(3)">3</button>
        <button type="button" class="button" @click="append('+')">+</button>
        <button type="button" class="button" @click="append('-')">-</button>

        <button type="button" class="button" @click="append(0)">0</button>
        <button type="button" class="button" @click="append('.')">.</button>

        <button type="button" class="button" @click="calculateToggle">±</button>

        <button type="button" class="button calculate" @click="calculate">=</button>

        <!-- <button class="button" @click="press">(</button>
        <button class="button" @click="press">)</button>-->

        <!-- <button class="button" @click="press">x 2</button> -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      equation: "0",
      isDecimalAdded: false, //只能输入一个小数位
      isOperatorAdded: false, //只能写一个等号
      isStarted: false //判断是否已经开始输入数据
    };
  },

  methods: {
    //判断是否是增删改查
    isOperator(character) {
      return ["+", "-", "×", "÷"].indexOf(character) > -1;
    },
    //追加数字
    append(character) {
      if (this.equation === "0" && !this.isOperator(character)) {
        if (character === ".") {
          this.equation += "" + character;
          this.isDecimalAdded = true;
        } else {
          this.equation = "" + character;
        }
        this.isStarted = true;
        return;
      }
      if (!this.isOperator(character)) {
        if (character === "." && this.isDecimalAdded) {
          return;
        }
        if (character === ".") {
          this.isDecimalAdded = true;
          this.isOperatorAdded = true;
        } else {
          this.isOperatorAdded = false;
        }
        this.equation += "" + character;
      }
      if (this.isOperator(character) && !this.isOperatorAdded) {
        this.equation += "" + character;
        this.isDecimalAdded = false;
        this.isOperatorAdded = false;
      }
    },
    //点击等于号
    calculate() {
      let result = this.equation
        .replace(new RegExp("×", "g"), "*")
        .replace(new RegExp("÷", "g"), "/");
      this.equation = parseFloat(eval(result).toFixed(9)).toString();
      this.isDecimalAdded = false;
      this.isOperatorAdded = false;
    },

    //点击AC
    clear() {
      this.equation = "0";
      this.isDecimalAdded = false;
      this.isOperatorAdded = false;
      this.isStarted = false;
    },

    //点击正负号
    calculateToggle() {
      if (this.isOperatorAdded || !this.isStarted) {
        true;
      }
      this.equation = this.equation + "*(-1))";
      this.calculate();
    },

    //点击百分比
    calculatePercent() {
      if (this.isOperatorAdded || !this.isStarted) {
        true;
      }
      this.equation = this.equation + "*0.01";
      this.calculate();
    }
  }
};
</script>
liuxin committed
124
<style scoped>
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
.calculator {
  width: 500px;
  padding: 20px;
  border-radius: 5px;
  margin: 20px auto;
  font-size: 16px;
  background-color: #d9d9d9;
}

.button {
  margin: 3px;
  width:80px;
  border: 1px solid #0d0d0d;
  height: 30px;
  border-radius: 4px;
  columns: #d9d9d9;
  background-color: white;
  cursor: pointer;
  outline: none;
}
.calculate {
  width: 175px;
}
.mode {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
}
.input {
  width: 480px;
  height: 50px;
  border-radius: 0px;
  border: 1px solid black;
  background-color: white;
  color: black;
  padding: 0 5px 0 5px;
  margin: 0 0px 10px 0px;
}
</style>