微信小程序?qū)崿F(xiàn)簡單的計算器功能
更新時間:2021年07月19日 09:03:17 作者:聰明可愛小軒軒
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序?qū)崿F(xiàn)計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
wxml
<view class='content'>
<input value='{{calculation}}'></input>
<view class='box'>
<button class='yellow-color'>退格</button>
<button class='yellow-color' bindtap='empty'>清屏</button>
<button class='yellow-color'>❤</button>
<button bindtap='add' data-text='+' class='yellow-color'>+</button>
</view>
<view class='box'>
<button bindtap='add' data-text='9'>9</button>
<button bindtap='add' data-text='8'>8</button>
<button bindtap='add' data-text='7'>7</button>
<button bindtap='add' class='yellow-color' data-text='-'>-</button>
</view>
<view class='box'>
<button bindtap='add' data-text='6'>6</button>
<button bindtap='add' data-text='5'>5</button>
<button bindtap='add' data-text='4'>4</button>
<button bindtap='add' class='yellow-color' data-text='*'>*</button>
</view>
<view class='box'>
<button bindtap='add' data-text='3'>3</button>
<button bindtap='add' data-text='2'>2</button>
<button bindtap='add' data-text='1'>1</button>
<button bindtap='add' data-text='/' class='yellow-color'>÷</button>
</view>
<view class='box'>
<button bindtap='add' data-text='0'>0</button>
<button bindtap='add' data-text='.'>.</button>
<button>歷史</button>
<button class='yellow-color' bindtap='res'>=</button>
</view>
</view>
wxss
input {
width: 95%;
height: 250rpx;
margin: 0 auto;
margin-bottom: 20rpx;
border-bottom: 1rpx solid #ccc;
}
.box {
display: flex;
}
button {
width: 20%;
height: 150rpx;
margin-bottom: 20rpx;
line-height: 150rpx;
background-color:rgb(0, 150, 250);
color: white;
}
.yellow-color {
background-color: rgb(247, 142, 24)
}
JS
//index.js
//獲取應(yīng)用實例
const app = getApp()
Page({
data: {
calculation:"",
result:0,
character:[], // 運算符號
operand: [], // 數(shù)字
temp:false
},
// 輸入框輸入數(shù)據(jù)
add:function(e) {
let input = e.currentTarget.dataset.text;
var that = this;
if (input == '+' || input == '-' || input == '*' || input == '/') {
this.data.temp = false; // 用于記錄上一次是否是操作符
var item = 'character[' + this.data.character.length+ ']';
this.setData({
[item] :input
})
} else {
var item = 'operand['+this.data.operand.length+']';
if(that.data.temp) {
// 拿到前一個的值
var res = 'operand[' + (this.data.operand.length-1) + ']'
var ress= that.data.operand.length-1;
var xyz = that.data.operand[ress] * 10 + parseInt(input);
that.setData({
[res]:xyz
})
} else {
input = parseInt(input);
that.data.temp = true;
that.setData({
[item]: input
})
}
}
// 將所有的內(nèi)容放到顯示框中
this.setData({
calculation:this.data.calculation+input
})
},
// 計算答案
res:function() {
console.log(this.data.character.length);
console.log(this.data.operand.length)
var character_len = this.data.character.length ;
var operand_len = this.data.operand.length;
console.log(operand_len - character_len)
if(operand_len - character_len == 1) {
this.data.result = this.data.operand[0];
console.log("初始值"+this.data.result);
for(var i=0;i<character_len;i++) {
if(this.data.character[i] == '+') {
this.data.result = this.data.result + this.data.operand[i + 1];
}
if (this.data.character[i] == '-') {
this.data.result = this.data.result - this.data.operand[i + 1];
}
if (this.data.character[i] == '*') {
this.data.result = this.data.result * this.data.operand[i + 1];
}
if (this.data.character[i] == '/') {
this.data.result = this.data.result / this.data.operand[i + 1];
}
}
} else {
this.setData({
result:'輸入有誤,清空數(shù)據(jù),重新輸入'
})
}
this.setData({
calculation:this.data.result
})
},
// 清空屏幕
empty:function() {
this.setData({
calculation: "",
result: 0,
character: [], // 運算符號
operand: [], // 數(shù)字
temp: false
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript位移運算符(無符號) >>> 三個大于號 的使用方法詳解
這篇文章主要介紹了JavaScript位移運算符(無符號) >>> 三個大于號 的使用方法詳解的相關(guān)資料,需要的朋友可以參考下2016-03-03
詳解JavaScript的內(nèi)存空間、賦值和深淺拷貝
這篇文章主要介紹了JavaScript的內(nèi)存空間、賦值和深淺拷貝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
cordova入門基礎(chǔ)教程及使用中遇到的一些問題總結(jié)
這篇文章主要給大家介紹了關(guān)于cordova的入門基礎(chǔ)教程以及在使用中遇到的一些問題,文中通過示例代碼一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
cropper js基于vue的圖片裁剪上傳功能的實現(xiàn)代碼
這篇文章主要介紹了cropper js基于vue的圖片裁剪上傳功能的相關(guān)資料,需要的朋友可以參考下2018-03-03
讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計)
JavaScript支持函數(shù)重載嗎?可以說不支持,也可以說支持。說不支持,是因為JavaScript不能好像其它原生支持函數(shù)重載的語言一樣,直接寫多個同名函數(shù),讓編譯器來判斷某個調(diào)用對應(yīng)的是哪一個重載。2009-08-08

