WeChat applet implements a simple calculator

WeChat applet implements a simple calculator

A simple calculator written in WeChat applet for your reference. The specific contents are as follows

jisaunqi.js

// pages/jisuanqi/jisuanqi.js
Page({

  /**
   * Initial data of the page */
  data: {
    result:"0",
    string:"",
    cal:"",
    num1:"",
    num2:""
  },
  btSubmit:function(e){
    console.log(e);
    var num1 = this.data.num1;
    var cal = this.data.cal;
    var num2 = this.data.num2;
    var char= e.target.id;
    var string ;
    if((char>="0"&&char<="9"||char=='.')&&cal==""){
      num1=num1+char;
      this.setData({
        num1:num1,
      })
    }
    else if((char>="0"&&char<="9"||char=='.')&&cal!=""){
      num2=num2+char;
      this.setData({
        num2:num2,
      })
    }
    else {
      cal=char;
      this.setData({
        cal:cal,
      })
    }
    this.setData({
     string:num1+cal+num2
    })
  },
  clean:function(e){
    this.setData({
      string:"",
      num1:"",
      num2:"",
      cal:""
    })
  },
  calculate:function(e){
    var num1 = this.data.num1;
    var num2 = this.data.num2;
    var cal = this.data.cal;
    var result;
    switch(cal){
      case '+':result=num1*1+num2*1;break;
      case '-':result=num1*1-num2*1;break;
      case '*':result=(num1*1)*(num2*1);break;
      case '/':result=(num1*1)/(num2*1);break;
    }
    num1=result;
    cal="";
    num2="";
    this.setData({
      result:result,
      num1:num1,
      cal:cal,
      num2:num2
    })
  },
  reverse:function(e){
    var cal = this.data.cal;
    var num1 = this.data.num1;
    var num2 = this.data.num2;
    if(cal==""){num1="-";}
    else if(cal!=""){num2="-"}
    this.setData({
      num1:num1,
      num2:num2
    })
  },
  lololo:function(e){
    console.log(123)
  },
  confirm:function(e){
    console.log(555);
    console.log(e)
  },
  event:function(e){
    wx.navigateTo({
      url: '/pages/event/event',
    })
  },
  bindinput:function(e){
    console.log(1)
  },
  jisuanqi:function(e){
    var n1=e.detail.value.num1;
    var n2=e.detail.value.num2;
    var result=n1*1+n2*1;
    console.log(n1);
    console.log(n2);
    console.log(result);
    this.setData({
      result:result
    })
  },
  tiaozhuan:function(e){
    wx.navigateTo({
      url: '/pages/9x9form/9x9form',
    })
  },
  /**
   * Life cycle function--listen for page loading*/
  onLoad: function (options) {

  },

  /**
   * Life cycle function - listen for the completion of the initial rendering of the page*/
  onReady: function () {

  },

  /**
   * Life cycle function--monitor page display*/
  onShow: function () {

  },

  /**
   * Life cycle function--listen for page hiding*/
  onHide: function () {

  },

  /**
   * Life cycle function--monitor page uninstallation*/
  onUnload: function () {

  },

  /**
   * Page related event processing function - listen to user pull-down action */
  onPullDownRefresh: function () {

  },

  /**
   * The function that handles the bottoming event on the page*/
  onReachBottom: function () {

  },

  /**
   * User clicks on the upper right corner to share*/
  onShareAppMessage: function () {

  }
})

jisuanqi.json

{
  "usingComponents": {},
  "navigationBarTitleText": "Calculator"
}

jisuanqi.wxml

<view class="container">
  <view class="view1">{{string}}</view>
  <view class="view2">{{result}}</view>
  <view class="button-group">
    <button class="button">History</button>
    <button class="button" bindtap="clean">C</button>
    <button class="button"></button>
    <button class="button" id="/" bindtap="btSubmit">/</button>
  </view>
  <view class="button-group">
    <button class="button" id="7" bindtap="btSubmit">7</button>
    <button class="button" id="8" bindtap="btSubmit">8</button>
    <button class="button" id="9" bindtap="btSubmit">9</button>
    <button class="button" id="*" bindtap="btSubmit">*</button>
  </view>
  <view class="button-group">
    <button class="button" id="4" bindtap="btSubmit">4</button>
    <button class="button" id="5" bindtap="btSubmit">5</button>
    <button class="button" id="6" bindtap="btSubmit">6</button>
    <button class="button" id="-" bindtap="btSubmit">-</button>
  </view>
  <view class="button-group">
    <button class="button" id="1" bindtap="btSubmit">1</button>
    <button class="button" id="2" bindtap="btSubmit">2</button>
    <button class="button" id="3" bindtap="btSubmit">3</button>
    <button class="button" id="+" bindtap="btSubmit">+</button>
  </view>
  <view class="button-group">
    <button class="button" bindtap="reverse">-(minus sign)</button>
    <button class="button" id="0" bindtap="btSubmit">0</button>
    <button class="button" id="." bindtap="btSubmit">.</button>
    <button class="button" bindtap="calculate">=</button>
  </view>
</view>
<navigator url="/pages/event/event">Jump to event</navigator>//

jisuanqi.wxss

.button{
  width: 160rpx;
  height: 100rpx;
  margin-left: 10rpx;
  padding-left: 10rpx;
  margin-top: 10rpx;
  text-align: center;
  line-height: 100rpx;
  padding: 5px;
  border-radius: 5px;
}
.button-group{
  display: flex;
  flex-direction: row;
  align-content: flex-start;
}
.container{
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  /* align-content: flex-end; */
}
.view1{
  height: 100rpx;
  background-color: #e4e4e4;
  line-height: 100rpx;
  font-size: 20px;
}
.view2{
  height: 100rpx;
  margin-top: 5px;
  background-color: #e4e4e4;
  line-height: 100rpx;
  font-size: 20px;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet implements a simple calculator
  • Implementing calculator functions with WeChat applet
  • WeChat applet implements simple calculator function
  • WeChat applet implements calculator function
  • WeChat applet calculator example
  • WeChat applet implements calculator function
  • WeChat applet implements simple calculator function
  • WeChat applet simple calculator implementation code example
  • WeChat applet calculator example

<<:  Detailed graphic explanation of Mysql5.7.18 installation and master-slave replication

>>:  How to configure two-way certificate verification on nginx proxy server

Recommend

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

What is a MySQL index? Ask if you don't understand

Table of contents Overview From Binary Tree to B+...

XHTML introductory tutorial: Application of table tags

<br />Table is an awkward tag in XHTML, so y...

Solution to the problem of MySQL thread in Opening tables

Problem Description Recently, there was a MySQL5....

Three networking methods and principles of VMware virtual machines (summary)

1. Brigde——Bridge: VMnet0 is used by default 1. P...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

How to implement MySQL master-slave replication based on Docker

Preface MySQL master-slave replication is the bas...

Use Typescript configuration steps in Vue

Table of contents 1. TypeScript is introduced int...

HTML+css to create a simple progress bar

1. HTML code Copy code The code is as follows: Ex...

Centos 7 64-bit desktop version installation graphic tutorial

If you think the system is slow and want to chang...

CentOS6 upgrade glibc operation steps

Table of contents background Compile glibc 2.14 M...

My CSS framework - base.css (reset browser default style)

Copy code The code is as follows: @charset "...