package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 11:25
*/
public interface Expression {
long interpret();
}
package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 11:26
*/
public class NumberExpression implements Expression{
private long number;
public NumberExpression(long number){
this.number = number;
}
public NumberExpression(String number){
this.number = Long.parseLong(number);
}
@Override
public long interpret() {
return this.number;
}
}
package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 11:27
*/
public class AdditionExpression implements Expression{
private Expression exp1;
private Expression exp2;
public AdditionExpression(Expression exp1,Expression exp2){
this.exp1 = exp1;
this.exp2 = exp2;
}
@Override
public long interpret() {
return exp1.interpret()+exp2.interpret() ;
}
}
package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 12:22
*/
public class SubstractionExpression implements Expression{
private Expression exp1;
private Expression exp2;
public SubstractionExpression(Expression exp1, Expression exp2){
this.exp1 = exp1;
this.exp2 = exp2;
}
@Override
public long interpret() {
return exp1.interpret()-exp2.interpret() ;
}
}
package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 12:23
*/
public class MultiplicationExpression implements Expression{
private Expression exp1;
private Expression exp2;
public MultiplicationExpression(Expression exp1,Expression exp2){
this.exp1 = exp1;
this.exp2 = exp2;
}
@Override
public long interpret() {
return exp1.interpret()*exp2.interpret() ;
}
}
package com.example.designpattern.interpret.hard;
/**
* @Author: zhangQi
* @Date: 2021-02-10 12:23
*/
public class DivisionExpression implements Expression{
private Expression exp1;
private Expression exp2;
public DivisionExpression(Expression exp1,Expression exp2){
this.exp1 = exp1;
this.exp2 = exp2;
}
@Override
public long interpret() {
try {
return exp1.interpret()/exp2.interpret();
} catch (ArithmeticException e) {
if(exp2.interpret()==0){
System.out.print("除数不能为0,默认返回Long最大值:");
}
return Long.MAX_VALUE;
}
}
}
package com.example.designpattern.interpret.hard;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
* @Author: zhangQi
* @Date: 2021-02-10 12:26
*/
public class ExpressionInterpreter {
private Deque<Expression> numbers = new LinkedList<>();
public long interpret(String expression){
String[] elements = expression.split(" ");
int length = elements.length;
for(int i=0;i<(length+1)/2;++i){
numbers.addLast(new NumberExpression(elements[i]));
}
for(int i=(length+1)/2;i<length;++i){
String operator = elements[i];
boolean isValid = "+".equals(operator) || "-".equals(operator)
|| "*".equals(operator) || "/".equals(operator);
if(!isValid){
throw new RuntimeException("Expression is invalid: "+expression);
}
Expression exp1 = numbers.pollFirst();
Expression exp2 = numbers.pollFirst();
Expression combindExp = null;
if(operator.equals("+")){
combindExp = new AdditionExpression(exp1,exp2);
}else if(operator.equals("-")){
combindExp = new SubstractionExpression(exp1,exp2);
}else if(operator.equals("*")){
combindExp = new MultiplicationExpression(exp1,exp2);
}else if(operator.equals("/")){
combindExp = new DivisionExpression(exp1,exp2);
}
long result = combindExp.interpret();
numbers.addFirst(new NumberExpression(result));
}
if(numbers.size()!=1){
throw new RuntimeException("Expression is invalid: "+expression);
}
return numbers.pop().interpret();
}
public static void main(String[] args) {
ExpressionInterpreter cal = new ExpressionInterpreter();
String behindExpression1 = "3 3 +";
String behindExpression2 = "3 3 1 4 + - *";
String behindExpression3 = "5 3 1 4 0 - - * /";
String behindExpression4 = "2 3 1 4 0 9 * - * + +";
List<String> behindExpressionList = new ArrayList<>();
behindExpressionList.add(behindExpression1);
behindExpressionList.add(behindExpression2);
behindExpressionList.add(behindExpression3);
behindExpressionList.add(behindExpression4);
behindExpressionList.forEach(bhex-> System.out.println(cal.interpret(bhex)));
}
}