|
1 | | -package com.iluwatar.interpreter; |
2 | | - |
3 | | -import java.util.Stack; |
4 | | - |
5 | | -/** |
6 | | - * |
7 | | - * Interpreter pattern breaks sentences into expressions (Expression) that can |
8 | | - * be evaluated and as a whole form the result. |
9 | | - * |
10 | | - */ |
11 | | -public class App { |
12 | | - |
13 | | - /** |
14 | | - * |
15 | | - * Expressions can be evaluated using prefix, infix or postfix notations |
16 | | - * This sample uses postfix, where operator comes after the operands |
17 | | - * |
18 | | - */ |
19 | | - public static void main(String[] args) { |
20 | | - String tokenString = "4 3 2 - 1 + *"; |
21 | | - Stack<Expression> stack = new Stack<>(); |
22 | | - |
23 | | - String[] tokenList = tokenString.split(" "); |
24 | | - for (String s : tokenList) { |
25 | | - if (isOperator(s)) { |
26 | | - Expression rightExpression = stack.pop(); |
27 | | - Expression leftExpression = stack.pop(); |
28 | | - System.out |
29 | | - .println(String.format( |
30 | | - "popped from stack left: %d right: %d", |
31 | | - leftExpression.interpret(), |
32 | | - rightExpression.interpret())); |
33 | | - Expression operator = getOperatorInstance(s, leftExpression, |
34 | | - rightExpression); |
35 | | - System.out.println(String.format("operator: %s", operator)); |
36 | | - int result = operator.interpret(); |
37 | | - NumberExpression resultExpression = new NumberExpression(result); |
38 | | - stack.push(resultExpression); |
39 | | - System.out.println(String.format("push result to stack: %d", |
40 | | - resultExpression.interpret())); |
41 | | - } else { |
42 | | - Expression i = new NumberExpression(s); |
43 | | - stack.push(i); |
44 | | - System.out.println(String.format("push to stack: %d", |
45 | | - i.interpret())); |
46 | | - } |
47 | | - } |
48 | | - System.out |
49 | | - .println(String.format("result: %d", stack.pop().interpret())); |
50 | | - } |
51 | | - |
52 | | - public static boolean isOperator(String s) { |
53 | | - return s.equals("+") || s.equals("-") || s.equals("*"); |
54 | | - } |
55 | | - |
56 | | - public static Expression getOperatorInstance(String s, Expression left, |
57 | | - Expression right) { |
58 | | - switch (s) { |
59 | | - case "+": |
60 | | - return new PlusExpression(left, right); |
61 | | - case "-": |
62 | | - return new MinusExpression(left, right); |
63 | | - case "*": |
64 | | - return new MultiplyExpression(left, right); |
65 | | - } |
66 | | - return null; |
67 | | - } |
68 | | -} |
| 1 | +package com.iluwatar.interpreter; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * Interpreter pattern breaks sentences into expressions ({@link Expression}) that can |
| 8 | + * be evaluated and as a whole form the result. |
| 9 | + * |
| 10 | + */ |
| 11 | +public class App { |
| 12 | + |
| 13 | + /** |
| 14 | + * |
| 15 | + * Program entry point. |
| 16 | + * <p> |
| 17 | + * Expressions can be evaluated using prefix, infix or postfix notations |
| 18 | + * This sample uses postfix, where operator comes after the operands |
| 19 | + * |
| 20 | + * @param args command line args |
| 21 | + * |
| 22 | + */ |
| 23 | + public static void main(String[] args) { |
| 24 | + String tokenString = "4 3 2 - 1 + *"; |
| 25 | + Stack<Expression> stack = new Stack<>(); |
| 26 | + |
| 27 | + String[] tokenList = tokenString.split(" "); |
| 28 | + for (String s : tokenList) { |
| 29 | + if (isOperator(s)) { |
| 30 | + Expression rightExpression = stack.pop(); |
| 31 | + Expression leftExpression = stack.pop(); |
| 32 | + System.out |
| 33 | + .println(String.format( |
| 34 | + "popped from stack left: %d right: %d", |
| 35 | + leftExpression.interpret(), |
| 36 | + rightExpression.interpret())); |
| 37 | + Expression operator = getOperatorInstance(s, leftExpression, |
| 38 | + rightExpression); |
| 39 | + System.out.println(String.format("operator: %s", operator)); |
| 40 | + int result = operator.interpret(); |
| 41 | + NumberExpression resultExpression = new NumberExpression(result); |
| 42 | + stack.push(resultExpression); |
| 43 | + System.out.println(String.format("push result to stack: %d", |
| 44 | + resultExpression.interpret())); |
| 45 | + } else { |
| 46 | + Expression i = new NumberExpression(s); |
| 47 | + stack.push(i); |
| 48 | + System.out.println(String.format("push to stack: %d", |
| 49 | + i.interpret())); |
| 50 | + } |
| 51 | + } |
| 52 | + System.out |
| 53 | + .println(String.format("result: %d", stack.pop().interpret())); |
| 54 | + } |
| 55 | + |
| 56 | + public static boolean isOperator(String s) { |
| 57 | + return s.equals("+") || s.equals("-") || s.equals("*"); |
| 58 | + } |
| 59 | + |
| 60 | + public static Expression getOperatorInstance(String s, Expression left, |
| 61 | + Expression right) { |
| 62 | + switch (s) { |
| 63 | + case "+": |
| 64 | + return new PlusExpression(left, right); |
| 65 | + case "-": |
| 66 | + return new MinusExpression(left, right); |
| 67 | + case "*": |
| 68 | + return new MultiplyExpression(left, right); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | +} |
0 commit comments