微信小程序实现一个小型计算器

这个博客详细介绍了如何使用微信小程序(WXML、WXSS、JS)开发一个简单的计算器应用。从页面布局到功能实现,包括点击数字按钮、运算符按钮的操作、删除功能以及等号按钮的计算逻辑,都进行了深入的代码分析。通过这个实例,读者可以学习到微信小程序的基本开发流程和计算表达式的处理方法。
摘要由CSDN通过智能技术生成
  • app.js
// app.js
App({
  onLaunch() {
  },
  REGEXP:/^[\+\-×÷]$/
})
  • app.wxss
/**app.wxss**/
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 
  • index.html
<!--index.wxml-->
<view class="result">
  <view class="result-express">{{express}}</view>
  <view class="result-value">{{result}}</view>
</view>
<view class="btns">
  <view class="item">
    <view class="reset" hover-class="bg" bindtap="reset">C</view>
    <view class="devide" hover-class="bg" bindtap="clickNumBtn" data-val="7">7</view>
    <view class="multiply" hover-class="bg" bindtap="clickNumBtn" data-val="4">4</view>
    <view class="delete" hover-class="bg" bindtap="clickNumBtn" data-val="1">1</view>
    <view class="percent" hover-class="bg" bindtap="clickNumBtn" data-val="%">%</view>
  </view>
  <view class="item">
    <view class="devide" hover-class="bg" bindtap="clickOptBtn" data-val="÷">÷</view>
    <view class="eight" hover-class="bg" bindtap="clickNumBtn" data-val="8">8</view>
    <view class="five" hover-class="bg" bindtap="clickNumBtn" data-val="5">5</view>
    <view class="two" hover-class="bg" bindtap="clickNumBtn" data-val="2">2</view>
    <view class="zero" hover-class="bg" bindtap="clickNumBtn" data-val="0">0</view>
  </view>
  <view class="item">
    <view class="multiply" hover-class="bg" bindtap="clickOptBtn" data-val="×">×</view>
    <view class="nine" hover-class="bg" bindtap="clickNumBtn" data-val="9">9</view>
    <view class="six" hover-class="bg" bindtap="clickNumBtn" data-val="6">6</view>
    <view class="three" hover-class="bg" bindtap="clickNumBtn" data-val="3">3</view>
    <view class="dot" hover-class="bg" bindtap="clickNumBtn" data-val=".">.</view>
  </view>
  <view class="item">
    <view class="delete" hover-class="bg" bindtap="delete">DEL</view>
    <view class="subtract" hover-class="bg" bindtap="clickOptBtn" data-val="-">-</view>
    <view class="add" hover-class="bg" bindtap="clickOptBtn" data-val="+">+</view>
    <view class="equal" hover-class="bg" bindtap="equal">=</view>
  </view>
</view>
  • index.wxss
/* index.wxss */
page{
  display: flex;
  flex-direction: column;
  height: 100%;
}
.result,.btns{
  flex: 1;
}
.result{
  background: #f3f6fe;
}
.btns{
  display: flex;
  flex-direction: row;
}
.item{
  flex-basis: 25%;
  display: flex;
  flex-direction: column;
}
.item view{
  flex-basis: 20%;
  border-bottom: 1rpx solid #eee;
  border-right: 1rpx solid #eee;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  font-size: 17pt;
}
.item view:first-child{
  color: rgb(41, 192, 252);
}
.item:last-child view{
  color: rgb(41, 192, 252);
}
.item view.equal{
  flex-basis:40%;
  background-color: rgb(41, 192, 252);
  color:#fff;
}
.bg{
  background-color: #eee;
}
.result{
  position: relative;
}
.result-express{
  position: absolute;
  bottom:5vh;
  right: 3vw;
  font-size: 27pt;
}
.result-value{
  position: absolute;
  bottom: 1vh;
  right: 3vw;
  font-size: 16pt;
  color: darkgray;
}
  • index.js
// index.js
const app = getApp();
const {REGEXP} = app;
Page({
  data:{
    express:"",
    result:"",
    isEqualBtnClicked:false
  },
  reset:function(e){
    this.setData({
      express:"",
      result:"",
      isEqualBtnClicked:false
    })
  },
  preProcess:function(express){
    if(REGEXP.test(express[express.length-1])){
      express = express.slice(0,express.length-1);
    }
    return express;
  },
  delete:function(){
    const {express} = this.data;
    let newExpress = express.slice(0,express.length-1);
    let expressForCalc = this.preProcess(newExpress);
    let temp = this.calculate(expressForCalc);
    let newResult = temp==="0"?"":temp;
    this.setData({
      express:newExpress,
      result:newResult
    })
  },
  equal:function(){
    const {express} = this.data;
    let expressForCalc = this.preProcess(express);
    this.setData({
      result:"",
      express:this.calculate(expressForCalc),
      isEqualBtnClicked:true
    })
  },
  clickNumBtn:function(e){
    const val = e.target.dataset.val;
    let {express} = this.data;
    express+=val;
    this.setData({
      express:express,
      result:this.calculate(express)
    },function(){
      console.log(`click numBtn,current express: ${this.data.express},it'time to get result:${this.calculate(express)}`);
  
    })
  },
  clickOptBtn:function(e){
    let {express} = this.data;
    if(express==="" || REGEXP.test(express[express.length-1])) return;
    const opt = e.target.dataset.val;
    express+=opt;
    this.setData({
      express:express
    },function(){
      console.log("click optBtn,current express:",this.data.express);
    })
  },
  getLeftAndRight:function(express,i){
    const REGEXP = /\d+(\.\d+)?/;
    const REGEXP2 = /[\+\-×÷]/;

    let front = express.slice(0,i);
    let back = express.slice(i+1);
  
    let temp = front.split(REGEXP2);
    let left = Number(temp[temp.length-1]);
  
    let right = Number(back.match(REGEXP)[0]);
  
    return {left,right};
  },
  calculate:function(express){
    const REGEXP1 = /[×÷]/;
    const REGEXP2 = /\+(\d+)(\.\d+)?/;//正数
    const REGEXP3 = /\-(\d+)(\.\d+)?/;//负数
    const REGEXP4 = /\d+(\.\d+)?/;  

    const {getLeftAndRight} = this;
  
    let temp;
    let result = 0;
  
    //先乘除
    while((temp = express.match(REGEXP1)) !== null){
      let opt = temp[0];
      let i = temp["index"];
      let {left,right} = getLeftAndRight(express,i);
      switch(opt){
        case "×":express = express.replace(left+opt+right,left*right);break;
        case "÷":express = express.replace(left+opt+right,left/right);break;
      }
    }
  
    //后加减
    let firstNum = 0,sum1=0,sum2=0;
  
    let res = express.match(REGEXP4);
    if(res!==null && res["index"]===0){
      let str = res[0];
      firstNum = Number(str);
      express = express.slice(str.length);
    }
  
    while((temp = express.match(REGEXP2)) != null){
      let str = temp[0];
      express = express.replace(str,"");
      sum1 += Number(str.slice(1));
    }
    while((temp = express.match(REGEXP3)) != null){
      let str = temp[0];
      express = express.replace(str,"");
      sum2 += Number(str.slice(1));
    }
    
    result += firstNum + sum1 - sum2;
    
    return result.toString();
  }
})
  • 测试验证
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值