用Java语言实现的算术表达式计算器

利用两个堆栈,一个保存操作数,一个保存操作符,一边解析输入,一边计算

# Calculator.java 类

import java.util.Stack;

public class Calculator {

  public String eval(String expr) {
    return _eval(expr);
  }

  public static class Lexer {

    private Lexer(String expr) {
      this.input = expr;
      this.seq = expr.toCharArray();
      this.pos = 0;
    }

    public static Lexer create(String expr) {
      return new Lexer(expr);
    }

    public String input;
    public char[] seq;
    int pos;

    public String getWord() {
      StringBuffer ret = new StringBuffer();
      boolean end = false;
      TYPE current = TYPE.NUL;
      while (!end && pos < seq.length) {
        char c = seq[pos];
        switch (c) {
          case '+':
            end = true;
            if (current == TYPE.FLOAT) {
              break;
            }
            current = TYPE.