使用微信开发者工具制作计算器

wxml部分

<view class='screen'>{{result}}</view>
<view class='test-bg'>
  <view class='btnGroup'>
    <view class='item green' bindtap='clickButton' id="{{C}}" style="color: red; font-size: 20px;">C</view>
    <view class='item green' bindtap='clickButton' id="{{back}}" style="font-size: 20px;">DEL</view>
    <view class='item green' bindtap='clickButton' id="{{addSub}}">+/-</view>
    <view class='item green' bindtap='clickButton' id="{{div}}" style="color: orange;">÷</view>
  </view>
  <view class='btnGroup'>
    <view class='item blue' bindtap='clickButton' id="{{id7}}">7</view>
    <view class='item blue' bindtap='clickButton' id="{{id8}}">8</view>
    <view class='item blue' bindtap='clickButton' id="{{id9}}">9</view>
    <view class='item green' bindtap='clickButton' id="{{mut}}" style="color: orange;">×</view>
  </view>
  <view class='btnGroup'>
    <view class='item blue' bindtap='clickButton' id="{{id4}}">4</view>
    <view class='item blue' bindtap='clickButton' id="{{id5}}">5</view>
    <view class='item blue' bindtap='clickButton' id="{{id6}}">6</view>
    <view class='item green' bindtap='clickButton' id="{{sub}}" style="color: orange;">-</view>
  </view>
  <view class='btnGroup'>
    <view class='item blue' bindtap='clickButton' id="{{id1}}">1</view>
    <view class='item blue' bindtap='clickButton' id="{{id2}}">2</view>
    <view class='item blue' bindtap='clickButton' id="{{id3}}">3</view>
    <view class='item green' bindtap='clickButton' id="{{add}}" style="color: orange;">+</view>
  </view>
  <view class='btnGroup'>
    <view class='item blue' bindtap='clickButton' id="{{id0}}" style="width: 371rpx;">0</view>
    <view class='item blue' bindtap='clickButton' id="{{dot}}">.</view>
    <view class='item green' bindtap='clickButton' id="{{equ}}" style="color: orange;">=</view>
  </view>
</view>

wxss部分

page {
  width: 100%;
  height: 100%;
}
.test-bg {
  position: fixed;
  bottom: 0;
}
.screen {
  width: 100%;
  height: 180px;
  position: fixed;
  line-height: 300px;
  background-color: rgb(228, 255, 255);
  color: #000000;
  font-size: 30px;
  bottom: 780rpx;
  text-align: right;
}
.btnGroup {
  display: flex; 
  flex-direction: row;
}
.item {
  width: 183rpx;
  line-height: 148rpx;
  border-radius: 2px;
  text-align: center;
  font-size: 30px;
}
.green {
  color: #000000;
  border: solid 1rpx rgba(83, 83, 83, 0.329);
}
.blue {
  color: #000000;
  border: solid 1rpx rgba(83, 83, 83, 0.329);
}

js部分

Page({
  data: {
    back: 'back',
    C: 'C',
    addSub: 'addSub',
    add: '+',
    sub: '-',
    mut: '×',
    div: '÷',
    equ: '=',
    percent: '%',
    dot: '.',
    id0: '0',
    id1: '1',
    id2: '2',
    id3: '3',
    id4: '4',
    id5: '5',
    id6: '6',
    id7: '7',
    id8: '8',
    id9: '9',
    result: '0',
    valiBackOfArray: ['+', '-', '×', '÷', '.'],
    completeCalculate: false
  },
  calculate: function (str) {
    var isNagativeNum = false;
    if (str.charAt(0) == '-') {
      str = str.replace('-', '').replace('(', '').replace(')', '');
      isNagativeNum = true;
    }
    var addArray = str.split('+');
    var sum = 0.0;
    for (var i = 0; i < addArray.length; i++) {
      if (addArray[i].indexOf('-') == -1) {
        if (addArray[i].indexOf('×') != -1 || addArray[i].indexOf('÷') != -1)
          sum += this.calculateMutDiv(addArray[i]);
        else sum += Number(addArray[i]);
      }
      else {
        var subArray = addArray[i].split('-');
        var subSum = 0;
        if (subArray[0].indexOf('×') != -1 || subArray[0].indexOf('÷') != -1) subSum = this.calculateMutDiv(subArray[0]);
        else subSum = Number(subArray[0]);
        for (var j = 1; j < subArray.length; j++) {
          if (subArray[i].indexOf('×') != -1 || subArray[i].indexOf('÷') != -1)
            subSum -= this.calculateMutDiv(subArray[j]);
          else subSum -= Number(subArray[j]);
        }
        sum += subSum;
      }
    }
    if (isNagativeNum) return (-sum).toString();
    else return sum.toString();
  },
  calculateMutDiv: function (str) {
    var addArray = str.split('×');
    var sum = 1.0;
    for (var i = 0; i < addArray.length; i++) {
      if (addArray[i].indexOf('÷') == -1) {
        sum *= Number(addArray[i]);
      }
      else {
        var subArray = addArray[i].split('÷');
        var subSum = Number(subArray[0]);
        for (var j = 1; j < subArray.length; j++) {
          subSum /= Number(subArray[j]);
        }
        sum *= subSum;
      }
    }
    return sum;
  },
  isOperatorEnd: function (str) {
    for (var i = 0; i < this.data.valiBackOfArray.length; i++) {
      if (str.charAt(str.length - 1) == this.data.valiBackOfArray[i]) return true;
    }
    return false;
  },
  clickButton: function (event) {
    if (this.data.result == 0) {
      if (event.target.id == 'back' || event.target.id == 'C' || event.target.id == 'addSub' || event.target.id == '%' || event.target.id == '+' || event.target.id == '-' || event.target.id == '×' || event.target.id == '÷' || event.target.id == '=') return;
      this.setData({
        result: event.target.id
      });
    }
    else if (event.target.id == 'back') {
      this.setData({
        result: this.data.result.length == 1 ? '0' : this.data.result.substring(0, this.data.result.length - 1)
      });
    }
    else if (event.target.id == 'C') {
      this.setData({
        result: '0'
      });
    }
    else if (event.target.id == 'addSub') {
      var r = this.data.result;
      if (this.isOperatorEnd(r)) return;
      if (r.charAt(0) == '-') this.setData({ result: r.replace('-', '').replace('(', '').replace(')', '') });
      else this.setData({
        result: '-(' + r + ')'
      });
    }
    else if (event.target.id == '%') {
    }
    else if (event.target.id == '=') {
      if (this.isOperatorEnd(this.data.result)) return;
      this.setData({
        result: this.calculate(this.data.result)
      });
      this.setData({
        completeCalculate: true
      });
    }
    else {
      if (this.isOperatorEnd(this.data.result) && this.isOperatorEnd(event.target.id)) return;

      if (this.data.completeCalculate && this.data.result.indexOf('.') != -1 && event.target.id == '.') return;
      for (var i = 0; i < this.data.valiBackOfArray.length - 1; i++) {
        if (this.data.valiBackOfArray[i] == event.target.id) {
          this.setData({
            completeCalculate: false
          });
          break;
        }
      }
      this.setData({
        result: this.data.result + event.target.id
      });
    }
  }
})

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值