Formatted all files to the same standard
This commit is contained in:
@ -3,64 +3,70 @@ package com.iluwatar;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Interpreter pattern breaks sentences into expressions (Expression) that can
|
||||
* be evaluated and as a whole form the result.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
*
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
/**
|
||||
*
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(), rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out
|
||||
.println(String.format(
|
||||
"popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(),
|
||||
rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d",
|
||||
resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d",
|
||||
i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out
|
||||
.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
|
||||
public static boolean isOperator(String s) {
|
||||
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static boolean isOperator(String s) {
|
||||
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
||||
public abstract class Expression {
|
||||
|
||||
public abstract int interpret();
|
||||
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ public class MinusExpression extends Expression {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() - rightExpression.interpret();
|
||||
|
@ -5,11 +5,12 @@ public class MultiplyExpression extends Expression {
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
||||
public MultiplyExpression(Expression leftExpression,
|
||||
Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() * rightExpression.interpret();
|
||||
|
@ -7,11 +7,11 @@ public class NumberExpression extends Expression {
|
||||
public NumberExpression(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
public NumberExpression(String s) {
|
||||
this.number = Integer.parseInt(s);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return number;
|
||||
|
@ -9,7 +9,7 @@ public class PlusExpression extends Expression {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() + rightExpression.interpret();
|
||||
|
Reference in New Issue
Block a user