最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)崿F(xiàn)簡易的計(jì)算器功能

 更新時(shí)間:2022年09月09日 10:29:34   作者:SepbreeZe  
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡易的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一個(gè)初入IC的硅農(nóng),硬件編程經(jīng)驗(yàn)3個(gè)月。偶然接觸了微信小程序編程,然后自己寫了一個(gè)計(jì)算器,希望得到改進(jìn)意見。

功能:

1、計(jì)算 + - * /和%;

2、主要是當(dāng)?shù)贸鼋Y(jié)果的時(shí)候,可以顯示原來的數(shù)都是什么。用手機(jī)上的計(jì)算器都是一按等于號(hào),我的兩個(gè)操作數(shù)也沒了,很煩。所以自己寫的時(shí)候就在得到結(jié)果的時(shí)候操作數(shù)不變。

計(jì)算器樣子就是下面這樣子:

樣子不太會(huì)設(shè)計(jì),主要是js部分啦。

WXML代碼

由于wxml的很多方法和屬性我都還不太了解,所以有的設(shè)置方法可能顯得繁瑣。

<!--pages/caculator/caculator.wxml-->
<!-- 計(jì)算區(qū) -->
<view class="caculatorArea">
? <!-- 顯示數(shù)據(jù)與運(yùn)算符 -->
? <view class="dataArea1">{{firstNum}}</view>
? <view class="dataArea2">{{operatorShow}}</view>
? <view class="dataArea3">{{secondNum}}</view>
? <!-- 顯示結(jié)果 -->
? <view class="resultArea">result: ? {{result}}</view>
</view>
<!-- 輸入?yún)^(qū) -->
<view class="inputArea">
<!-- 按鍵第一排 -->
? <view class="fstRaw"> ?
? ? <button class="clear" bindtap="clearEvent" data-value="C" >C</button>
? ? <button class="divide" bindtap="opEvent" ?data-value="/">/</button>
? ? <button class="mutiply" bindtap="opEvent" data-value="*">*</button>
? ? <button class="back" bindtap="backEvent" data-value="←">←</button>
? </view>
? <!-- 按鍵第二排 -->
? <view class="secRaw"> ?
? ? <button class="seven" bindtap="numEvent" data-value="7">7</button>
? ? <button class="eight" bindtap="numEvent" data-value="8" >8</button>
? ? <button class="nine" bindtap="numEvent" data-value="9">9</button>
? ? <button class="minus" bindtap="opEvent" data-value="-">-</button>
? </view>
? ?<!-- 按鍵第三排 -->
? ?<view class="thdRaw"> ?
? ? <button class="four" bindtap="numEvent" data-value="4">4</button>
? ? <button class="five" bindtap="numEvent" data-value="5">5</button>
? ? <button class="six" bindtap="numEvent" data-value="6">6</button>
? ? <button class="plus" bindtap="opEvent" data-value="+">+</button>
? </view>
? ? ?<!-- 按鍵第四排 -->
? <view class="forRaw"> ??
? ? <button class="one" bindtap="numEvent" data-value="1">1</button>
? ? <button class="two" bindtap="numEvent" ?data-value="2">2</button>
? ? <button class="three" bindtap="numEvent" data-value="3">3</button>
? ? <button class="equal" bindtap="opEvent" data-value="=">=</button>
? </view>
? ? ?<!-- 按鍵第五排 -->
? <view class="fifRaw"> ?
? ? <button class="abs" bindtap="opEvent" data-value="%">%</button>
? ? <button class="zero" bindtap="numEvent" data-value="0">0</button>
? ? <button class="dot" bindtap="dotEvent" data-value=".">.</button>
? </view>
</view>

js代碼 

這個(gè)代碼是真的讓我快崩了,最后達(dá)到的效果,能跑就行。。由于軟件的代碼寫的很少,所以在一些判斷的時(shí)候顯得很繁瑣。比如如何區(qū)分目前輸入的數(shù)字是屬于第一個(gè)操作數(shù)還是第二個(gè)操作數(shù)(因?yàn)槲蚁朐趦尚酗@示數(shù)字,因此這就成了一個(gè)問題),我的辦法是設(shè)置了一個(gè)flag,請(qǐng)問有沒有知道更簡單的方法的?

Page({
? ? data:{
? ? ? result:"", ? ?
? ? ? firstNum:"",?
? ? ? secondNum:"",
? ? ? operatorUse:"",
? ? ? operatorShow:"",
? ? },
? ? var rst=""
? ? numFlag:true,
? ? optContinue:false,
? ? resultFlag:false, ?// ?結(jié)果標(biāo)志位 ? ? 1代表有結(jié)果,0代表沒結(jié)果
? ? fstIsClear:true, ? ? ?// ?第一個(gè)數(shù)字是否清除標(biāo)志位 ? 1代表前面沒數(shù),0代表有數(shù)
? ? secIsClear:true, ? ? ?//第二個(gè)數(shù)字是否清除的標(biāo)志位 ? 1代表前面沒數(shù),0代表有數(shù)
? ? numEvent(n){
? ? ? ? let numValue=n.currentTarget.dataset.value;
? ? ? ? if(this.resultFlag==true){ ? ? ? ? //如果得出了結(jié)果,并且按數(shù)字鍵的話,那么直接進(jìn)行下一個(gè)計(jì)算,當(dāng)前計(jì)算結(jié)束
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? firstNum:numValue,
? ? ? ? ? ? ? ? secondNum:"",
? ? ? ? ? ? ? ? secIsClear:true,
? ? ? ? ? ? ? ? operatorShow:"",
? ? ? ? ? ? ? ? operatorUse:""?
? ? ? ? ? ? })
? ? ? ? ? ? this.resultFlag=false
? ? ? ? }else if(this.numFlag==true){ ? ? //如果此時(shí)沒有按操作符,則歸為第一個(gè)數(shù) ??
? ? ? ? ? ? if(this.data.firstNum=="0"){ ?//假如第一個(gè)數(shù)現(xiàn)在是0,那么0不進(jìn)行保存
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? firstNum:numValue ? ? ?
? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? //如果第一個(gè)數(shù)不是0,那么進(jìn)行拼接 ? ? ? ? ? ?
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? firstNum:this.data.firstNum+numValue ? ? ?
? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? this.fstIsClear=false;
? ? ? ? ? ? }}else{ ? ? ? ? ? ? ? ? ? ? ? ? ?//如果此時(shí)已經(jīng)按過操作符,則歸為第二個(gè)數(shù)
? ? ? ? ? ? ? ?if(this.data.secondNum=="0"){//假如第二個(gè)數(shù)現(xiàn)在是0,那么0不進(jìn)行保存
? ? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? secondNum:numValue ? ? ??
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }else{ ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? secondNum:this.data.secondNum+numValue ? ? ??
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? this.secIsClear=false;
? ? ? ? ? ? }
? ? ? ? }
? ? },
? ? opEvent(o){
? ? ? ? let optLast=this.data.operatorUse;
? ? ? ? let optCurrent=o.currentTarget.dataset.value;
? ? ? ? if(optCurrent!=null && this.data.secondNum=="" && this.data.fisrtdNum==""){
? ? ? ? ? ? this.numFlag=true; //都為空時(shí),連續(xù)點(diǎn)擊操作符不更換數(shù)字順序
? ? ? ? }else if(optCurrent!=null && this.data.secondNum=="" && this.data.fisrtdNum!=""){
? ? ? ? ? ? if(optCurrent=="="){
? ? ? ? ? ? ? ? this.numFlag=true;
? ? ? ? ? ? }else{?
? ? ? ? ? ? this.numFlag=false;//1有數(shù)2沒數(shù)時(shí),連續(xù)點(diǎn)擊操作符,再次點(diǎn)擊數(shù)字歸為第二個(gè)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else if(optCurrent!=null && this.data.secondNum!=""&& this.data.fisrtdNum!=""){?
? ? ? ? ? ? ?if(optCurrent=="="){ ? ? ??
? ? ? ? ? ? ? ? ?this.numFlag=!this.numFlag; //都不為空時(shí),點(diǎn)擊操作符后,數(shù)字綁定的順序改變
? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? this.numFlag=false;
? ? ? ? ? ? ?}
? ? ? ? }
? ? ? ? if(optLast=="="){
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? firstNum:rst,?
? ? ? ? ? ? ? ? secondNum:"",
? ? ? ? ? ? ? ? secIsClear:true,
? ? ? ? ? ? ? ? result:"" ??
? ? ? ? ? ? ? });
? ? ? ? ? ? ? this.resultFlag=false;
? ? ? ? }
? ? ? ? rst=Number(this.data.firstNum) ? ? //將第一個(gè)數(shù)轉(zhuǎn)換為數(shù)字類型
? ? ? ? var secNum=Number(this.data.secondNum) ? ?//將第二個(gè)數(shù)轉(zhuǎn)換為數(shù)字類型
?
? ? ? ? this.setData({
? ? ? ? ? ? operatorUse:optCurrent ? ? ? ? //得到操作符,用于js判斷
? ? ? ? });
? ? ? ? if(optCurrent=="="){ ? ? ? ? ? ? ? //得到操作符,用于wxml顯示?
? ? ? ? ? if(optLast!="="){
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? operatorShow:optLast
? ? ? ? ? ? }); ?
? ? ? ? }else{
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? operatorShow:""
? ? ? ? ? ? }); ?
? ? ? ? } ? ? ? ? ? ? ? ??
? ? ? ? }else{
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? operatorShow:optCurrent
? ? ? ? ? ? }) ? ? ? ??
? ? ? ? }
? ? ? ? if(optLast=="+"){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? //依據(jù)操作符進(jìn)行操作
? ? ? ? ? ? rst=rst+secNum;
? ? ? ? }else if(optLast=="-"){
? ? ? ? ? ? rst=rst-secNum ; ??
? ? ? ? }else if(optLast=="*"){
? ? ? ? ? ? rst=rst*secNum;
? ? ? ? }else if(optLast=="/"){
? ? ? ? ? ? rst=rst/secNum;
? ? ? ? }else if(optLast=="%"){
? ? ? ? ? ? rst=rst%secNum;
? ? ? ? } ? ? ?
? ? ? ? if(optCurrent=="="){ ? ? ? ? ?//當(dāng)輸入的操作符為=時(shí),進(jìn)行結(jié)果顯示
? ? ? ? ? ? this.setData({
? ? ? ? ? ? result:rst
? ? ? ? ? ? })
? ? ? ? ? ? this.resultFlag=true
? ? ? ? }
? ? ? ? if(optCurrent!="=" && optLast!=""){
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? firstNum:rst,?
? ? ? ? ? ? ? ? secondNum:"",
? ? ? ? ? ? ? ? secIsClear:true,
? ? ? ? ? ? ? ? result:"" ??
? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? this.resultFlag=false; ?//表明此時(shí)有結(jié)果了
? ? ? ? ? ? ? ? this.optContinue=true; ?//表明要進(jìn)行連續(xù)計(jì)算了
? ? ? ? }
? ? },
? ? dotEvent(){
? ? ? ? ? if(this.numFlag==true){
? ? ? ? ? ? ? if(this.fstIsClear==true){
? ? ? ? ? ? ? ? this.fstIsClear==false?
? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? firstNum:"0."
? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? //如果此前有數(shù),則輸入.為.
? ? ? ? ? ? ? ? if(this.data.firstNum.indexOf(".")>0){
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }else{ ? ??
? ? ? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? ? firstNum:this.data.firstNum+"."
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? ? ? } ? ? ? ? ? ?
? ? ? ? ? }else{
? ? ? ? ? ? ?if(this.secIsClear==true){?
? ? ? ? ? ? ? ? this.secIsClear==false?
? ? ? ? ? ? ? ?this.setData({
? ? ? ? ? ? ? ? ? secondNum:"0."
? ? ? ? ? ? ? })
? ? ? ? ? }else{
? ? ? ? ? ? ? if(this.data.secondNum.indexOf(".")>0){
? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? ? secondNum:this.data.secondNum+"."
? ? ? ? ? ? })
? ? ? ? }
? ? ? ? }
? ? }
},
? ? backEvent(){
? ? ? ? var fstNumDel=0;
? ? ? ? var secNumDel=0; ?
? ? if(this.resultFlag==true){
? ? ? ? return;
? ? }else{
? ? ? if(this.numFlag==true){
? ? ? ? fstNumDel=this.data.firstNum.substr(0,this.data.firstNum.length-1);
? ? ? ? this.setData({
? ? ? ? ? ? firstNum:fstNumDel
? ? ? ? });
? ? ? ? if(this.data.firstNum==""){
? ? ? ? ? ? this.fstIsClear=true;
? ? ? ? }
? ? }else{
? ? ? ? secNumDel=this.data.secondNum.substr(0,this.data.secondNum.length-1);
? ? ? ? this.setData({
? ? ? ? ? ? secondNum:secNumDel
? ? ? ? });
? ? ? ? if(this.data.secondNum==""){
? ? ? ? ? ? this.secIsClear=true;
? ? ? ? }
? ? }
? ? }
?
},
? ? clearEvent(){
? ? ? this.setData({
? ? ? ? firstNum:"",
? ? ? ? secondNum:"",
? ? ? ? operatorUse:"",
? ? ? ? operatorShow:"",
? ? ? ? result:""
? ? ? })
? ? ? this.numFlag=true;
? ? ? this.resultFlag=false;
? ? ? this.isClear=true;
? ? ? this.fstIsClear=true;
? ? ? this.secIsClear=true;
}
?
})

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)簡易計(jì)算器

    微信小程序?qū)崿F(xiàn)簡易計(jì)算器

    這篇文章介紹了微信小程序?qū)崿F(xiàn)簡易計(jì)算器的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • js和jquery分別驗(yàn)證單選框、復(fù)選框、下拉框

    js和jquery分別驗(yàn)證單選框、復(fù)選框、下拉框

    這篇文章主要為大家詳細(xì)介紹了js和jquery分別驗(yàn)證單選框、復(fù)選框、下拉框的具體代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • js實(shí)現(xiàn)動(dòng)態(tài)添加上傳文件頁面

    js實(shí)現(xiàn)動(dòng)態(tài)添加上傳文件頁面

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)動(dòng)態(tài)添加上傳文件頁面,如何動(dòng)態(tài)創(chuàng)建一個(gè)input標(biāo)簽示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • JavaScript setTimeout和setInterval的使用方法 說明

    JavaScript setTimeout和setInterval的使用方法 說明

    兩個(gè)函數(shù)都是可以用來實(shí)現(xiàn)一段時(shí)間后執(zhí)行一段javascript代碼的效果。兩個(gè)函數(shù)都有兩個(gè)參數(shù),前面的都是執(zhí)行表達(dá)式,后面的是隔的秒數(shù)。
    2010-03-03
  • javascript 中的 delete及delete運(yùn)算符

    javascript 中的 delete及delete運(yùn)算符

    這篇文章主要介紹了javascript 中的 delete及delete運(yùn)算符的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 解決input輸入框僅支持輸入數(shù)字及兩位小數(shù)點(diǎn)的限制

    解決input輸入框僅支持輸入數(shù)字及兩位小數(shù)點(diǎn)的限制

    這篇文章主要為大家介紹了解決input輸入框僅支持輸入數(shù)字及兩位小數(shù)點(diǎn)的限制技巧示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • JavaScript canvas基于數(shù)組生成柱狀圖代碼實(shí)例

    JavaScript canvas基于數(shù)組生成柱狀圖代碼實(shí)例

    這篇文章主要介紹了JavaScript canvas基于數(shù)組生成柱狀圖代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • BootstrapTable refresh 方法使用實(shí)例簡單介紹

    BootstrapTable refresh 方法使用實(shí)例簡單介紹

    本文就bootstrapTable refresh 方法如何傳遞參數(shù)做簡單舉例說明,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-02-02
  • javascript IFrame 強(qiáng)制刷新代碼

    javascript IFrame 強(qiáng)制刷新代碼

    經(jīng)常會(huì)使用多個(gè)iframe來展示領(lǐng)域模型主子關(guān)系(主/子單),測試發(fā)現(xiàn)iframe是有cache功能的
    2009-07-07
  • 微信小程序之多列表的顯示和隱藏功能【附源碼】

    微信小程序之多列表的顯示和隱藏功能【附源碼】

    今天在項(xiàng)目碰到一個(gè)問題,之前在項(xiàng)目首頁實(shí)現(xiàn)單列表的顯示和隱藏,通過wx:if判斷就可實(shí)現(xiàn),現(xiàn)在要實(shí)現(xiàn)多列表的單項(xiàng)顯示和隱藏功能應(yīng)該如何實(shí)現(xiàn)呢?下面小編給大家?guī)砹宋⑿判〕绦蛑嗔斜淼娘@示和隱藏功能,感興趣的朋友一起看看吧
    2018-08-08

最新評(píng)論

宁南县| 东阳市| 利辛县| 怀仁县| 洪雅县| 湾仔区| 万全县| 丽水市| 宁南县| 闻喜县| 来宾市| 海阳市| 兰西县| 湟源县| 鄄城县| 东安县| 肥乡县| 凤冈县| 都安| 十堰市| 望奎县| 锦州市| 汕尾市| 清徐县| 柳河县| 台北市| 荃湾区| 潮州市| 金湖县| 安乡县| 青龙| 紫金县| 定安县| 永川市| 平舆县| 加查县| 马鞍山市| 昭苏县| 德惠市| 阜新市| 乐至县|