在调车场处理链式一元运算符

时间:2018-10-31 02:13:10

标签: algorithm shunting-yard

我查看了wiki page for shunting yard,因为在调车场中处理链式一元负号运算符时遇到问题,但是wiki算法似乎无法处理它。

Wiki算法供参考:

$(document).ready(function() {
  var getLogin_hr;
  var getLogin_min;
  var getLogin_sec;
// clock in and out
  $clockIn = true;
  $(".buttons").click(function() {
    // get date
    var d = new Date();
    var cal = new Date();

    function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
    }

    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var day = d.getDay();
    var month = months[d.getMonth()];
    var date = d.getDate();
    var year = d.getFullYear();
    var hoursIn = addZero(d.getHours());
    var minutesIn = addZero(d.getMinutes());
    var secondsIn = addZero(d.getSeconds());
    var hoursOut = addZero(cal.getHours());
    var minutesOut = addZero(cal.getMinutes());
    var secondsOut = addZero(cal.getSeconds());

    var hoursInNum = d.getHours();
    var minutesInNum = d.getMinutes();
    var secondsInNum = d.getSeconds();
    var hoursOutNum = cal.getHours();
    var minutesOutNum = cal.getMinutes();
    var secondsOutNum = cal.getSeconds();

    var timeIn = hoursIn + ":" + minutesIn + ":" + secondsIn;
    var timeOut = hoursOut + ":" + minutesOut + ":" + secondsOut;

    switch (day) {
      case 0:


  day = "Sunday";
      break;
  case 1:
      day = "Monday";
      break;
  case 2:
      day = "Tuesday";
      break;
  case 3:
      day = "Wednesday";
      break;
  case 4:
      day = "Thursday";
      break;
  case 5:
      day = "Friday";
      break;
  case 6:
      day = "Saturday";
  }


var output = day + ", " + month + " " + date + ", " + year + " || " + timeIn;
  var output1 = day + ", " + month + " " + date + ", " + year + " || " + timeOut;

  // to clock in
if ($(this).hasClass("clockIn") && $clockIn == true) {
  $(this).removeClass("clockIn");
  $(this).addClass("clockOut");
  $(this).text("Clock Out");

  $("#logWrapper").append("<p class='logText'><span class='clockInTextColor'>Logged-In. </span>" + output + "</p><hr />");
  var getLogin_hr = hoursInNum;
  var getLogin_min = minutesInNum;
  var getLogin_sec = secondsInNum;
  var getLoginTime = getLogin_hr + ":" + getLogin_min + ":" + getLogin_sec;
  console.log("Log In: " + getLoginTime);
  var startDate = new Date();
  $clockIn = false;
  // to clock out
} else if ($(this).hasClass("clockOut") && $clockIn == false) {
  $(this).removeClass("clockOut");
  $(this).addClass("clockIn");
  $(this).text("Clock In");

  $("#logWrapper").append("<p class='logText'><span class='clockOutTextColor'>Logged-Out. </span>" + output1 + "</p><hr />");
  var getLogout_hr = hoursOutNum;
  var getLogout_min = minutesOutNum;
  var getLogout_sec = secondsOutNum;
  var getLogoutTime = getLogout_hr + ":" + getLogout_min + ":" + getLogout_sec;
  console.log("Log Out: " + getLogoutTime);
  var endDate = new Date();
  $clockIn = true;

  var diff = new Date(endDate) - new Date(startDate);
  diff_time = diff/(60*60*1000);
  console.log(diff_time);
}
  });
});

假设我具有以下表达式/* This implementation does not implement composite functions,functions with variable number of arguments, and unary operators. */ while there are tokens to be read: read a token. if the token is a number, then: push it to the output queue. if the token is a function then: push it onto the operator stack if the token is an operator, then: while ((there is a function at the top of the operator stack) or (there is an operator at the top of the operator stack with greater precedence) or (the operator at the top of the operator stack has equal precedence and is left associative)) and (the operator at the top of the operator stack is not a left bracket): pop operators from the operator stack onto the output queue. push it onto the operator stack. if the token is a left bracket (i.e. "("), then: push it onto the operator stack. if the token is a right bracket (i.e. ")"), then: while the operator at the top of the operator stack is not a left bracket: pop the operator from the operator stack onto the output queue. pop the left bracket from the stack. /* if the stack runs out without finding a left bracket, then there are mismatched parentheses. */ if there are no more tokens to read: while there are still operator tokens on the stack: /* if the operator token on the top of the stack is a bracket, then there are mismatched parentheses. */ pop the operator from the operator stack onto the output queue. exit. ,我希望rpn输出为---1,但是实际发生的情况如下:


迭代1: 1--- operator stack: []

要读取的令牌是output queue: [],因此我们将其压入运算符堆栈


迭代2: - operator stack: [-]

要读取的令牌是output queue: []。操作符堆栈上有一个-,我们应该将其弹出到输出队列中。然后,我们将令牌推入运算符堆栈。


迭代3: - operator stack: [-]

错误-输出队列中还应该没有output queue: [-](由于毫无意义,因此不会继续进行算法)

0 个答案:

没有答案