This article example shares the specific code of the mini program to implement the calculator function for your reference. The specific content is as follows Simulate the calculator on your phone and calculate by input This page is for calculating payment. If you don't need the following content, you can delete it. wxml <view class="calculate-box"> <view class="calculate-txt">{{express}}</view> <view class="result-num">={{result}}</view> </view> <view class="fixation-box"> <view class="calculator-box"> <view class="calculator-line"> <view data-con='c' class='boxtn btn1 clear' catchtap="clickKeyBoard">AC</view> <view data-con='←' class='boxtn btn1' catchtap="clickKeyBoard"> <image src="../../../images/clear-icon.png" class="clear-icon"></image> </view> <view data-con='÷100' class='boxtn btn1 percent' catchtap="clickKeyBoard">%</view> <view data-con='÷' class='boxtn num' catchtap="clickKeyBoard">÷</view> </view> <view class="calculator-line"> <view data-con='7' class='boxtn btn1 num' catchtap="clickKeyBoard">7</view> <view data-con='8' class='boxtn btn1 num' catchtap="clickKeyBoard">8</view> <view data-con='9' class='boxtn btn1 num' catchtap="clickKeyBoard">9</view> <view data-con='×' class='boxtn num' catchtap="clickKeyBoard">×</view> </view> <view class="calculator-line"> <view data-con='4' class='boxtn btn1 num' catchtap="clickKeyBoard">4</view> <view data-con='5' class='boxtn btn1 num' catchtap="clickKeyBoard">5</view> <view data-con='6' class='boxtn btn1 num' catchtap="clickKeyBoard">6</view> <view data-con='-' class='boxtn num' catchtap="clickKeyBoard">-</view> </view> <view class="calculator-line"> <view data-con='1' class='boxtn btn1 num' catchtap="clickKeyBoard">1</view> <view data-con='2' class='boxtn btn1 num' catchtap="clickKeyBoard">2</view> <view data-con='3' class='boxtn btn1 num' catchtap="clickKeyBoard">3</view> <view data-con='+' class='boxtn num' catchtap="clickKeyBoard">+</view> </view> <view class="calculator-line"> <view data-con='0' class='boxtn btn2 num' catchtap="clickKeyBoard">0</view> <view data-con='.' class='boxtn btn1 num' catchtap="clickKeyBoard">.</view> <view data-con='=' class='boxtn equal' catchtap="result">=</view> </view> </view> <view class="bottom-handle"> <!-- <view class="sweep-code-verification" bindtap="sweepCodeVerification"> <image src="../../../images/sweep-code-verification.png"></image> <text>Scan the code to verify</text> </view> --> <view style="flex: 1;font-size: 34rpx;" class="artificial-collection" bindtap="artificial_collection"> <!--<image src="../../../images/artificial-collection.png"></image> --> <text>Manual collection</text> </view> <view class="payment-code" bindtap="qrcode_collection">Payment code collection</view> </view> </view> js data: { express: '', //The expression of the first line result: '0', //The result of the second line calc2: { str: '', //temporary string strList: [], //infix expression storage (queue first-in, first-out) strListP: [], // suffix expression (queue first-in, first-out) list: [], //Store the stack of operators (first in, last out) count: [], //Calculate expression stack (first in, last out) flag: 0 // indicates whether the last digit of the string is an operator digit}, isqr: false, }, //Bind this event to all text or view, and add the corresponding custom attribute value clickKeyBoard(e) { let that = this; let input = e.currentTarget.dataset.con //Get the content of each input if (input == "c") { that.handleClear(); } else if (input == "←") { that.handleDelete(); } else { //Call processing string that.handleInfo(input); } }, //Process the local user's input operation handleInfo(input) { if (this.data.calc2.str.length == 0) { //First clickif (input == "-" || this.checkShuZi(input)) { //For minus signthis.addStr(input); } else { return; } } else { if (this.data.calc2.flag == 1) { //Indicates that the last digit is a symbol if (this.checkFuHao(input)) { this.data.calc2.str = this.data.calc2.str.substring(0, this.data.calc2.str.length - 1); //Remove the last symbol and add the latest symbol this.addStr(input); } else { this.addStr(input); } console.log(); } else { this.addStr(input); this.result(); } } this.result(); }, //The customer clicks the equal sign result() { //Each time you click the equal sign, empty the list again this.data.calc2.strList.length = 0; this.data.calc2.strListP.length = 0; this.data.calc2.list.length = 0; this.data.calc2.count.length = 0; //Convert the expression into an infix expression queue this.expressToStrList(this.data.express); console.log(this.data.calc2.strList); //Assign the infix expression set to the temporary variable let tempList = this.data.calc2.strList; this.expressToStrListP(tempList); console.log(this.data.calc2.strListP); //Final calculation let tempP = this.data.calc2.strListP for (let m in tempP) { if (this.checkFuHao2(tempP[m])) { //Symbol method judgment without dot let m1 = this.data.calc2.count[0]; //Take out the first data this.data.calc2.count.shift(); //Delete the data after taking it out let m2 = this.data.calc2.count[0]; this.data.calc2.count.shift(); // console.log('m1 is ' + m1); // console.log('m2 is' + m2); // console.log('The operator is ' + tempP[m]); // console.log('The calculation result is: ' + this.countDetail(m2, tempP[m], m1)); this.data.calc2.count.unshift(this.countDetail(m2, tempP[m], m1)); //Put the calculation result into count} else { this.data.calc2.count.unshift(tempP[m]) //Push the number in} } console.log('The final calculation result is' + parseFloat(this.data.calc2.count[0]).toFixed(2)); this.setData({ result: this.data.calc2.count[0] }); }, //Actual calculation countDetail(e1, e2, e3) { let result = 0.0; console.log(e1); console.log(e2); console.log(e3); try { if (e2 == "×") { if (typeof(e1) != "undefined") { result = parseFloat(e1) * parseFloat(e3); } else { result = parseFloat(e3); } } else if (e2 == "÷") { if (typeof(e1) != "undefined") { result = parseFloat(e1) / parseFloat(e3); } else { result = parseFloat(e3); } } else if (e2 == "%") { if (typeof(e1) != "undefined") { result = parseFloat(e1) / parseFloat(e3); } else { result = parseFloat(e3); } } else if (e2 == "+") { if (typeof(e1) != "undefined") { result = parseFloat(e1) + parseFloat(e3); } else { result = parseFloat(e3); } } else { if (typeof (e1) != "undefined") { result = parseFloat(e1) - parseFloat(e3); } else { result = parseFloat(e3); } } } catch (error) { } return result; }, //Convert the infix expression set to a postfix expression set expressToStrListP(tempList) { for (let item in tempList) { if (this.checkFuHao2(tempList[item])) { //Symbol method judgment without period if (this.data.calc2.list.length == 0) { this.data.calc2.list.unshift(tempList[item]); //Add the addition operator directly} else { if (this.checkFuHaoDX(this.data.calc2.list[0], tempList[item])) { for (let x in this.data.calc2.list) { this.data.calc2.strListP.push(this.data.calc2.list[x]); //Put all operators in listP} this.data.calc2.list.length = 0; //After looping, empty the list this.data.calc2.list.unshift(tempList[item]); //Add new elements } else { this.data.calc2.list.unshift(tempList[item]); //Add the addition operator directly} } } else { this.data.calc2.strListP.push(tempList[item]); //Add the number directly to the suffix collection} } //After the loop is finished, the last one may be a number, and the one obtained is not a character, so the remaining ones in the operator should be added to listP if (this.data.calc2.list.length > 0) { for (let x in this.data.calc2.list) { this.data.calc2.strListP.push(this.data.calc2.list[x]); //Put all operators in listP} this.data.calc2.list.length = 0; //After looping, empty the list} }, //Judge the priority of the two operators (m1 is the last element added to the list set, and compares it with the element that is about to come in. If m1 is larger than m2, pop the list set into listp, and then add m2 to the list, otherwise add m2 directly to the list) checkFuHaoDX(m1, m2) { if ((m1 == "%" || m1 == "×" || m1 == "÷") && (m2 == "-" || m2 == "+")) { return true; } else { return false; } }, //Convert the string expression into an infix queue expressToStrList(express) { let temp = ''; //define temporary variables //Change the expression to infix queue for (let i = 0; i < express.length; i++) { if (i == 0 && express[i] == "-") { temp = temp + express[i]; } else { if (this.checkShuZi2(express[i])) { //If i is a number temp = temp + express[i]; } else { if (temp.length > 0) { if (express[i] == ".") { temp = temp + express[i]; } else { this.data.calc2.strList.push(parseFloat(temp)); temp = ''; this.data.calc2.strList.push(express[i]); } } else { temp = temp + express[i]; } } } } // Loop to the end and check if there are any numbers in temp, if so add them if (temp.length > 0 && this.checkShuZi(temp.substring(temp.length - 1))) { this.data.calc2.strList.push(parseFloat(temp)); temp = ''; } }, //Handle the clear key input by the customer handleClear() { this.data.calc2.str = ''; this.data.calc2.strList.length = 0; this.data.calc2.strListP.length = 0; this.data.calc2.list.length = 0; this.data.calc2.count.length = 0; this.data.calc2.minusFlag = 0; this.setData({ express: '', result: '' }); }, //Handle the customer input back key handleDelete() { let that = this; let str = that.data.calc2.str; if (str.length > 0) { str = str.substring(0, str.length - 1); that.data.calc2.str = str; that.setData({ express: str, }); } else { return; } }, //Judge whether it is an operator (including dot, used when organizing expressions. Cannot be entered repeatedly) checkFuHao(input) { if (input == "-" || input == "+" || input == "÷" || input == "%" || input == "×" || input == ".") { return true; } else { return false; } }, //Judge whether it is an operator (without period) checkFuHao2(input) { if (input == "-" || input == "+" || input == "÷" || input == "%" || input == "×") { return true; } else { return false; } }, //Judge whether it is a number checkShuZi(input) { if (input == "0" || input == "1" || input == "2" || input == "3" || input == "4" || input == "5" || input == "6" || input == "7" || input == "8" || input == "9") { return true; } else { return false; } }, //Judge whether it is a number (including the . sign, used in the expression conversion infix method) checkShuZi2(input) { if (input == "0" || input == "1" || input == "2" || input == "3" || input == "4" || input == "5" || input == "6" || input == "7" || input == "8" || input == "9" || input == ".") { return true; } else { return false; } }, //Add new characters to the string (append directly to determine whether to change the variable flag) addStr(input) { this.data.calc2.str = this.data.calc2.str + input; if (this.checkFuHao(input)) { this.data.calc2.flag = 1; //Set the flag bit to 1 } else { this.data.calc2.flag = 0; } this.setData({ express: this.data.calc2.str }); }, wxss /* pages/index/collect-money/collect-money.wxss */ page { background-color: #f8f8f8; } .bottom-handle { height: 100rpx; width: 100%; display: flex; align-items: center; } .fixation-box { position: fixed; bottom: 0; width: 750rpx; } .sweep-code-verification { background: #fff; border-top: 1rpx solid #e3e3e3; width: 220rpx; color: #333; } .artificial-collection, .sweep-code-verification { height: 100%; display: flex; flex-direction: column; align-items: center; font-size: 24rpx; justify-content: center; } .artificial-collection { background: #f3b055; width: 248rpx; color: #fff; } .payment-code { background-image: linear-gradient(203deg, #6f6f6f 0%, #3c3c3c 96%); flex: 1; font-size: 34rpx; color: #fff; text-align: center; line-height: 100rpx; } .sweep-code-verification image { width: 40rpx; height: 40rpx; } .artificial-collection image { width: 40rpx; height: 40rpx; } .calculator-box { background-color: #fff; } .calculator-line { display: flex; align-items: center; } .boxtn { width: 25%; height: 154rpx; border-left: none; display: flex; align-items: center; justify-content: center; } .calculator-box .calculator-line:last-child { border-bottom: none; } .calculator-line { border-bottom: 1rpx solid #dedede; } .btn1, .btn2 { border-right: 1rpx solid #dedede; } .btn2 { width: 50%; } .equal { background: #f3b055; font-size: 61rpx; color: #fff; } .num { font-size: 60rpx; color: #000; } .clear { font-size: 48rpx; color: #f3b055; } .percent { font-size: 60rpx; color: #000; } .charge-content { background: #fff; border-radius: 24rpx; width: 540rpx; display: flex; flex-direction: column; align-items: center; } .charge-title { background: #f3b055; border-radius: 12px 12px 0 0; width: 100%; height: 92rpx; font-size: 34rpx; line-height: 92rpx; text-align: center; color: #fff; } .charge-money { font-size: 60rpx; color: #333; line-height: 84rpx; margin-top: 35rpx; } .charge-propmt { font-size: 28rpx; color: #999; line-height: 42rpx; padding-bottom: 40rpx; } .charge-btn { display: flex; align-items: center; height: 100rpx; border-top: 1rpx solid #ddd; width: 100%; } .charge-cancel, .charge-confirm { flex: 1; text-align: center; line-height: 100rpx; font-size: 34rpx; } .charge-cancel { color: #999; border-right: 1rpx solid #ddd; } .charge-confirm { color: #f3b055; } .successful-content { background: #fff; border-radius: 24rpx; width: 540rpx; display: flex; flex-direction: column; align-items: center; } .success-icon { width: 120rpx; margin-top: 45rpx; height: 120rpx; } .successful-title { font-size: 34rpx; color: #333; line-height: 42rpx; padding: 30rpx 0; font-weight: 600; } .clear-icon { width: 80rpx; height: 80rpx; } .calculate-box { position: fixed; top: 0; bottom: 875rpx; width: 100%; display: flex; flex-direction: column; align-items: flex-end; padding: 0 50rpx; } .result-num { font-size: 88rpx; color: #333; line-height: 124rpx; } .calculate-txt { font-size: 60rpx; color: #333; line-height: 84rpx; margin-top: auto; word-wrap: break-word; text-align: right; word-break: break-all; text-overflow: -o-ellipsis-lastline; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .suspend-box{ height: 64rpx; background-color: rgba(0, 0, 0, .5); position: fixed; top: 70rpx; left: 0; color: #fff; display: flex; align-items: center; font-size: 24rpx; padding: 0 20rpx; border-radius: 0 100rpx 100rpx 0; z-index: 9; } .close-suspend{ width: 30rpx; height: 30rpx; } 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:
|
<<: Implementation of debugging code through nginx reverse proxy
>>: Detailed explanation of the basic implementation principle of MySQL DISTINCT
Table of contents Preface 1. 404 Page 1. Causes 2...
First, let's look at an example of memory rel...
Table of contents 1. Component Registration 1.1 G...
0x00 Introduction A few months ago, I found a vul...
Table of contents Preface 1. Environment Configur...
What is CSS# CSS (abbreviation of Cascading Style...
This article shares the specific code of JavaScri...
Knowing the IP address of a device is important w...
1. Log in to MySQL database mysql -u root -p View...
Preface I have an old laptop with Win7. In order ...
CSS3 Patterns Gallery This CSS3 pattern library s...
The img tag in XHTML is so-called self-closing, w...
There are two ways to create a primary key: creat...
How to center your HTML button itself? This is ea...
This article example shares the specific code of ...