/**!
 *
 * Copyright(c) boyce and other contributors.
 * MIT Licensed
 *
 * Authors:
 *  boyce <boyce.ywr@gmail.com> (http://www.jianshu.com/users/9b5b907d9bce)
 */
"use strict";
var e = 0,
  n = 1,
  t = 2,
  r = 3,
  u = 4,
  a = 5,
  c = e,
  s = 0,
  i = "0",
  o = "",
  f = "",
  l = i,
  b = "";
function d() {
  c = e, s = 0, i = "0", o = "", f = "", l = "0";
}
function p(e) {
  return "0" == e;
}
function h(e) {
  return e >= "0" && e <= "9";
}
function k(e) {
  return "+" == e || "-" == e || "x" == e || "/" == e || "%" == e;
}
function g(e) {
  return "." == e;
}
function N(e) {
  return "=" == e;
}
function x(e) {
  return "d" == e;
}
function m(e, n) {
  return e.length < 15 && (e += n), e;
}
function v() {
  return c == a || c == u ? (o.length > 0 && (o = o.substr(0, o.length - 1)), void ("" == o && (o = "0"))) : (i.length > 0 && "0" != i && (i = i.substr(0, i.length - 1)), void ("" == i && (i = "0")));
}
function O() {
  var e,
    n = parseFloat(i),
    t = parseFloat(o);
  switch (f) {
    case "+":
      s = n + t;
      break;
    case "-":
      s = n - t;
      break;
    case "x":
      s = n * t;
      break;
    case "/":
      0 == t ? (d(), s = "NaN", b = "") : s = n / t;
      break;
    case "%":
      0 == t ? (d(), s = "NaN", b = "") : s = n % t;
  }
  (e = "" + s).length > 15 && (e = e.substr(0, 15)), s = e;
}
d(), module.exports = {
  reset: d,
  addOp: function (w) {
    var shouldCompute = false; // 新增标志位控制计算结果显示

    switch (c) {
      case n:
      case e:
        if (h(w) && !p(w)) {
          c = t, i = w;
          l = i; // 确保显示当前输入
        } else if (g(w)) {
          c = r, i = "0.";
          l = i; // 确保显示当前输入
        } else if (k(w)) {
          c = u, i = "0", o = "", f = w;
          l = i; // 确保显示当前输入
        } else if (N(w)) {
          // 修复问题：在初始状态下按等号直接显示结果
          l = s.toString();
        }
        b = "";
        break;
      case t:
        b = "";
        if (h(w)) {
          i = p(i) ? w : m(i, w);
          l = i;
        } else if (g(w)) {
          c = r, i = "" == i ? "0" : m(i, ".");
          l = i;
        } else if (x(w)) {
          v(), i.indexOf(".") < 0 && (c = t);
          l = i; // 显示删除后的数字
        } else if (k(w)) {
          c = u, o = "", b = f = w;
          l = i; // 保持显示第一个数字
        } else if (N(w)) {
          // 修复问题：在数字后直接按等号
          s = parseFloat(i);
          l = s.toString();
          c = n;
        }
        break;
      case r:
        b = "";
        if (h(w)) {
          i = m(i, w);
          l = i;
        } else if (x(w)) {
          v(), i.indexOf(".") < 0 && (c = t);
          l = i;
        } else if (k(w)) {
          c = u, o = "", b = f = w;
          l = i; // 保持显示第一个数字
        }
        break;
      case u:
        b = "";
        if (h(w)) {
          o = p(o) ? w : m(o, w);
          l = o;
        } else if (g(w)) {
          c = a, o = "" == o ? "0" : m(o, ".");
          l = o;
        } else if (x(w)) {
          v(), o.indexOf(".") < 0 && (c = u);
          l = o;
        } else if (k(w)) {
          if ("" != o) {
            O();
            c = u;
            i = s;
            o = "";
            l = s;
          }
          b = f = w;
        } else if (N(w)) {
          // 修复问题：正确处理等号操作符
          if (o === "") o = i; // 当第二个操作数为空时使用第一个操作数
          if (o !== "") O();
          l = s.toString();
          c = n;
        }
        break;
      case a:
        b = "";
        if (h(w)) {
          o = m(o, w);
          l = o;
        } else if (x(w)) {
          v(), o.indexOf(".") < 0 && (c = u);
          l = o;
        } else if (k(w)) {
          if ("" != o) {
            O();
            c = u;
            i = s;
            o = "";
            l = s;
          }
          b = f = w;
        } else if (N(w)) {
          // 修复问题：正确处理等号操作符
          if (o === "") o = i; // 当第二个操作数为空时使用第一个操作数
          if (o !== "") O();
          l = s.toString();
          c = n;
        }
    }
    
    // =号操作符的特殊显示处理
    if (N(w)) {
      b = function (e) {
        return "/" == e ? "÷" : "x" == e ? "×" : e;
      }(f) + " =";
    } else if ("c" == w) {
      d();
    } else {
      b = function (e) {
        return "/" == e ? "÷" : "x" == e ? "×" : e;
      }(b);
    }
  },
  getVars: function () {
    return {
      curState: c,
      curResult: s,
      opNum1: i,
      opNum2: o,
      op: f,
      displayNum: l,
      displayOp: b
    };
  }
};