public abstract class Expression {
public abstract int interpret();
@Override public abstract String toString(); }
public class PlusExpression extends Expression {
private Expression leftExpression; private Expression rightExpression;
public PlusExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; }
@Override public int interpret() { return leftExpression.interpret() + rightExpression.interpret(); }
@Override public String toString() { return "+"; } }
public class MinusExpression extends Expression {
private Expression leftExpression; private Expression rightExpression;
public MinusExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; }
@Override public int interpret() { return leftExpression.interpret() - rightExpression.interpret(); }
@Override public String toString() { return "-"; }
}
public class MultiplyExpression extends Expression {
private Expression leftExpression; private Expression rightExpression;
public MultiplyExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; }
@Override public int interpret() { return leftExpression.interpret() * rightExpression.interpret(); }
@Override public String toString() { return "*"; }
}
public class NumberExpression extends Expression {
private int number;
public NumberExpression(int number) { this.number = number; }
public NumberExpression(String s) { this.number = Integer.parseInt(s); }
@Override public int interpret() { return number; }
@Override public String toString() { return "number"; } }
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) { var tokenString = "4 3 2 - 1 + *"; var stack = new Stack<Expression>();
var tokenList = tokenString.split(" "); for (var s : tokenList) { if (isOperator(s)) { var rightExpression = stack.pop(); var leftExpression = stack.pop(); LOGGER.info("popped from stack left: {} right: {}", leftExpression.interpret(), rightExpression.interpret()); var operator = getOperatorInstance(s, leftExpression, rightExpression); LOGGER.info("operator: {}", operator); var result = operator.interpret(); var resultExpression = new NumberExpression(result); stack.push(resultExpression); LOGGER.info("push result to stack: {}", resultExpression.interpret()); } else { var i = new NumberExpression(s); stack.push(i); LOGGER.info("push to stack: {}", i.interpret()); } } LOGGER.info("result: {}", stack.pop().interpret()); }
public static boolean isOperator(String s) { return s.equals("+") || s.equals("-") || s.equals("*"); }
public static Expression getOperatorInstance(String s, Expression left, Expression right) { switch (s) { case "+": return new PlusExpression(left, right); case "-": return new MinusExpression(left, right); case "*": return new MultiplyExpression(left, right); default: return new MultiplyExpression(left, right); } } }
|