簡易計算器全代碼

效果圖:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>mini計算器</title>

<style type="text/css">

body {

margin: 100px;

}

#app {

border: 1px solid #ccc;

width: 175px;

height: 285px;

padding: 10px;

border-radius: 4px;

}

#app .btn {

width: 40px;

height: 40px;

border: 1px solid #ccc;

border-radius: 2px;

background-color: white;

margin-bottom: 10px;

cursor: pointer;

color: #666;

font-weight: bold;

}

#app .btn:hover {

background-color: #333;

color: white;

}

#input-box {

width: 161px;

text-align: right;

height: 30px;

border: 1px solid #ccc;

border-radius: 2px;

margin-bottom: 10px;

background-color: white;

padding: 0 5px;

}

#app .btn-double {

width: 84px;

}

</style>

</head>

<body>

<div id="app">

<result-box :get-value="resultvalue"></result-box>

<calc-box>

<button class="btn" @click="clear">C</button>

<button class="btn" @click="sign">+/-</button>

<button class="btn" @click="sambol('*')">*</button>

<button class="btn" @click="sambol('/')">/</button>

<button class="btn" @click="input('1')">1</button>

<button class="btn" @click="input('2')">2</button>

<button class="btn" @click="input('3')">3</button>

<button class="btn" @click="sambol('-')">-</button>

<button class="btn" @click="input('4')">4</button>

<button class="btn" @click="input('5')">5</button>

<button class="btn" @click="input('6')">6</button>

<button class="btn" @click="sambol('+')">+</button>

<button class="btn" @click="input('7')">7</button>

<button class="btn" @click="input('8')">8</button>

<button class="btn" @click="input('9')">9</button>

<button class="btn" @click="sambol('%')">%</button>

<button class="btn" @click="input('0')">0</button>

<button class="btn" @click="point">.</button>

<button class="btn btn-double" @click="calculate">=</button>

</calc-box>

</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<script>

// 去掉提示

// Vue.config.productionTip=false

const resultBox = {

// 父子通信

props: ["getValue"],

// 計算屬性

computed: {

value() {

return this.getValue

}

},

template: `<input id="input-box" type="text" v-model="value" readonly />`

}

const calcBox = {

template: `<div id="calc-box">

<slot></slot>

</div>

`

}

const app = new Vue({

el: "#app",

data: {

resultvalue: 0

},

// 組件

components: {

// 計算器結果框

'result-box': resultBox,

// 計算器輸入面板組件

'calc-box': calcBox

},

methods: {

// 輸入數(shù)值? 累加

input(param) {

// 拒絕開始0和反復0? 防止0.被和諧

if (parseInt(this.resultvalue) === 0 && !/0[\.|\s]/.test(this.resultvalue)) {

this.resultvalue = param

} else {

this.resultvalue += param

}

},

// 加減乘除

sambol(param) {

// 有空格顯得更加好看

this.resultvalue += '? ' + param + '? '

},

// 處理小數(shù)點

point() {

const strValue = this.toStr()

// 得到最后一位數(shù)值

const lastNumber = strValue.substring(strValue.lastIndexOf(' '))

// 判斷是否已經(jīng)存在小數(shù)點

// 存在

if (lastNumber.indexOf('.') !== -1) {

return

} else {

this.resultvalue += '.'

}

},

// 轉換成字符串

toStr() {

return this.resultvalue.toString()

},

clear(){

this.resultvalue=0

},

// 正負號設置

sign(){

const strValue=this.toStr()

let lastNumber = strValue.substring(strValue.lastIndexOf(' '))

// 得到之前的數(shù)值+符號

let prevNumber=strValue.substr(0,strValue.lastIndexOf(' '))

// 判斷當前是否有正負號

if(lastNumber.indexOf('-')===-1){

lastNumber=' -'+lastNumber.trim()

}

else{

lastNumber=' '+lastNumber.trim().substr(1)

}

this.resultvalue=prevNumber+lastNumber;

},

calculate(){

try{

this.resultvalue=eval(this.resultvalue)

}catch(e){

//TODO handle the exception

alert('no')

}

}

}

})

</script>

</body>

</html>

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容