四则运算计算器的微信小程序_2 运算

js文件:

function isOperator(value) {
  var operatorString = '+-*/()×÷';
  return operatorString.indexOf(value) > -1;
}

function getPrioraty(value) {
  if(value == '-' || value == '+') {
    return 1;
  } else if(value == '*' || value == '/' || value == '×' || value == '÷' ) {
    return 2;
  } else{
    return 0;
  }
}

function prioraty(v1, v2) {
  return getPrioraty(v1) <= getPrioraty(v2);
}

function outputRpn(exp) {
  var inputStack = [];
  var outputStack = [];
  var outputQueue = [];
  var firstIsOperator = false;
  exp.replace(/\s/g,'');
  if(isOperator(exp[0])){
    exp = exp.substring(1);
    firstIsOperator = true;
  }
  for(var i = 0, max = exp.length; i < max; i++) {
    if(!isOperator(exp[i]) && !isOperator(exp[i-1]) && (i != 0)) {
      inputStack[inputStack.length-1] = inputStack[inputStack.length-1] + exp[i] + '';
    } else {
      inputStack.push(exp[i]);
    }
  }
  if(firstIsOperator) {
    inputStack[0] = -inputStack[0]
  }
  while(inputStack.length > 0) {
    var cur = inputStack.shift();
    if(isOperator(cur)) {
      if (cur == '(') {
        outputStack.push(cur);
      } else if (cur == ')') {
        var po = outputStack.pop();
        while(po != '(' && outputStack.length > 0) {
          outputQueue.push(po);
          po = outputStack.pop();
        }
      } else {
        while(prioraty(cur,outputStack[outputStack.length - 1]) && outputStack.length > 0) {
          outputQueue.push(outputStack.pop());
        }
        outputStack.push(cur)
      }
    } else {
      outputQueue.push(Number(cur));
    }
  }
  if(outputStack.length > 0){
    while (outputStack.length > 0) {
      outputQueue.push(outputStack.pop());
    }
  }
  return outputQueue;
}

function calRpnExp(rpnArr) {
  var stack = [];
  for(var i = 0, max = rpnArr.length; i < max; i++) {
    if(!isOperator(rpnArr[i])) {
      stack.push(rpnArr[i]);
    } else {
      var num1 = stack.pop();
      var num2 = stack.pop();
      if(rpnArr[i] == '-') {
        var num = num2 - num1;
      } else if(rpnArr[i] == '+') {
        var num = num2 + num1;
      } else if(rpnArr[i] == '*' || rpnArr[i] == '×') {
        var num = num2 * num1;
      } else if(rpnArr[i] == '/' || rpnArr[i] == '÷') {
        var num = num2/num1;
      }
      stack.push(num);
    }
  }
  return stack[0];
}

function calCommonExp(exp) {
  var rpnArr = outputRpn(exp);
  return calRpnExp(rpnArr)
}

module.exports = {
  isOperator: isOperator,
  getPrioraty: getPrioraty,
  outputRpn: outputRpn,
  calRpnExp: calRpnExp,
  calCommonExp: calCommonExp
}
 
 
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()

var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()


return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}

module.exports = {
formatTime: formatTime
}
 
//index.js
//获取应用实例
var rpn = require("../../utils/rpn.js");
var app = getApp()
Page({
data: {
id1:"back",
id2:"clear",
id3:"negative",
id4:"+",
id5:"9",
id6:"8",
id7:"7",
id8:"-",
id9:"6",
id10:"5",
id11:"4",
id12:"×",
id13:"3",
id14:"2",
id15:"1",
id16:"÷",
id17:"0",
id18:".",
id19:"history",
id20:"=",
screenData:"0",
lastIsOperator: false,
logs: []
},
//事件处理函数
 
onLoad: function () {
 
},
onReady: function () {

},
onShow: function () {

},
onHide: function () {

},
onUpload: function () {

},
history: function () {
wx.navigateTo({
url:'../list/list'
})
},
clickButton: function (event) {
console.log(event);
var data = this.data.screenData.toString();
var id = event.target.id;
if(id == this.data.id1) {
if(data == 0){
return;
}
console.log(data);
console.log("data.substring(0,data.length-1)"+data.substring(0,data.length-1));
var data = data.substring(0,data.length-1);
console.log(data);
} else if(id == this.data.id2) {
data = 0;
} else if (id == this.data.id3) {
var firstWord = data.substring(0,1);
if(firstWord != '-'){
data = '-' + data;
} else {
data = data.substring(1);
}
} else if (id == this.data.id20){
if(data == 0) {
return;
}
var lastWord = data.substring(data.length-1, data.length);
if(isNaN(lastWord)) {
return;
}
console.log("parseFloat(data)"+parseFloat(data));
console.log("data"+data)
if(parseFloat(data) == data){
return;
}
var log = data;
var data = rpn.calCommonExp(data);
log = log + '=' +data;
this.data.logs.push(log);
wx.setStorageSync('callogs',this.data.logs)
console.log(wx.getStorageSync('callogs'));
} else {
if(id == this.data.id4 || id == this.data.id8 || id == this.data.id12 || id == this.data.id16) {
if(this.data.lastIsOperator || data == 0) {
return;
}
}
if(data == 0) {
data = id;
} else {
data = data + id
}
if(id == this.data.id4 || id == this.data.id8 || id == this.data.id12 || id == this.data.id16) {
this.setData({lastIsOperator:true});
} else {
this.setData({lastIsOperator:false})
}
}

this.setData({
screenData:data
})
}
})
 
 
 

//获取应用实例
var app = getApp()
Page({
data: {
logs:[]
},
onLoad: function () {
var logs =wx.getStorageSync('callogs');
this.data.logs=logs;
}
})

(原项目作者:忽如寄 (简书)

原文链接:https://www.jianshu.com/p/47c1a65009a7)

posted @ 2018-02-22 20:04  肥鹅PU火  阅读(1645)  评论(0编辑  收藏  举报