|
| 1 | +<template> |
| 2 | + <div style="padding: 5px;"> |
| 3 | + <text class="result">{{result}}</text> |
| 4 | + <div class="row"> |
| 5 | + <text class="btn" @click="input">1</text> |
| 6 | + <text class="btn" @click="input">2</text> |
| 7 | + <text class="btn" @click="input">3</text> |
| 8 | + <text class="btn btn-operator" @click="input">+</text> |
| 9 | + </div> |
| 10 | + <div class="row"> |
| 11 | + <text class="btn" @click="input">4</text> |
| 12 | + <text class="btn" @click="input">5</text> |
| 13 | + <text class="btn" @click="input">6</text> |
| 14 | + <text class="btn btn-operator" @click="input">-</text> |
| 15 | + </div> |
| 16 | + <div class="row"> |
| 17 | + <text class="btn" @click="input">7</text> |
| 18 | + <text class="btn" @click="input">8</text> |
| 19 | + <text class="btn" @click="input">9</text> |
| 20 | + <text class="btn btn-operator" @click="input">*</text> |
| 21 | + </div> |
| 22 | + <div class="row"> |
| 23 | + <text class="btn" @click="input">0</text> |
| 24 | + <text class="btn" @click="input">.</text> |
| 25 | + <text class="btn" @click="clear">AC</text> |
| 26 | + <text class="btn btn-operator" @click="calculate">=</text> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | +</template> |
| 30 | + |
| 31 | +<style> |
| 32 | + .row { |
| 33 | + flex-direction: row; |
| 34 | + } |
| 35 | +
|
| 36 | + .result { |
| 37 | + text-align: right; |
| 38 | + background-color: #666; |
| 39 | + font-size: 40px; |
| 40 | + color: white; |
| 41 | + } |
| 42 | +
|
| 43 | + .btn { |
| 44 | + flex: 1; |
| 45 | + text-align: center; |
| 46 | + background-color: #eee; |
| 47 | + font-size: 36px; |
| 48 | + } |
| 49 | +
|
| 50 | + .btn, .result { |
| 51 | + height: 100px; |
| 52 | + padding: 30px; |
| 53 | + margin: 5px; |
| 54 | + } |
| 55 | +
|
| 56 | + .btn-operator { |
| 57 | + background-color: #669; |
| 58 | + font-size: 40px; |
| 59 | + color: white; |
| 60 | + } |
| 61 | +</style> |
| 62 | + |
| 63 | +<script> |
| 64 | + var OP = ['+', '-', '*', '/']; |
| 65 | + module.exports = { |
| 66 | + data: { |
| 67 | + result: '', |
| 68 | + inputs: [] |
| 69 | + }, |
| 70 | + methods: { |
| 71 | + input: function(e) { |
| 72 | + var value = e.target.attr['value']; |
| 73 | + var inputs = this.inputs; |
| 74 | + var lastOne = inputs.length ? inputs[inputs.length - 1] : ''; |
| 75 | + if (OP.indexOf(lastOne) > -1 && OP.indexOf(value) > -1) { |
| 76 | + return; |
| 77 | + } |
| 78 | + inputs.push(value); |
| 79 | + var buf = [], char; |
| 80 | + for (var i = 0; i < inputs.length; i++) { |
| 81 | + char = inputs[i]; |
| 82 | + if (OP.indexOf(char) > -1) { |
| 83 | + char = ' ' + char + ' '; |
| 84 | + } |
| 85 | + buf.push(char); |
| 86 | + } |
| 87 | + this.result = buf.join(''); |
| 88 | + }, |
| 89 | + calculate: function() { |
| 90 | + var result = eval(this.result); |
| 91 | + this.inputs = [result]; |
| 92 | + this.result = result; |
| 93 | + }, |
| 94 | + clear: function() { |
| 95 | + this.inputs = []; |
| 96 | + this.result = ''; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +</script> |
0 commit comments