Expression.java
public interface Expression {
// 解析公式和数值,其中var 中的key 值是公式中的参数,value 值是具体的数字.
public int interpreter(final Map<String, Integer> var);
}
VarExpression.java
public class VarExpression implements Expression {
private String key;
public VarExpression(final String key) {
this.key = key;
}
/**
* 从map中取值.
*/
@Override
public int interpreter(Map<String, Integer> var) {
return var.get(this.key);
}
}
SymbolExpression.java
public abstract class SymbolExpression implements Expression {
protected Expression left;
protected Expression right;
public SymbolExpression(final Expression left, final Expression right) {
this.left = left;
this.right = right;
}
@Override
public abstract int interpreter(Map<String, Integer> var);
}
AddExpression.java
public class AddExpression extends SymbolExpression {
/**
* @param newLeft
* @param newRight
*/
public AddExpression(Expression left, Expression right) {
super(left, right);
}
/**
* {@inheritDoc}
*/
@Override
public int interpreter(Map<String, Integer> var) {
return left.interpreter(var) + right.interpreter(var);
}
}
SubExpression.java
public class SubExpression extends SymbolExpression {
/**
* @param newLeft
* @param newRight
*/
public SubExpression(Expression left, Expression right) {
super(left, right);
}
/**
* {@inheritDoc}
*/
@Override
public int interpreter(Map<String, Integer> var) {
return left.interpreter(var) - right.interpreter(var);
}
}
Calculator.java
public class Calculator {
// 定义表达式.
private Expression expression;
public Calculator(final String expr) {
// 定义一个栈,安排运算的先后顺序.
final Stack<Expression> stack = new Stack<>();
char[] charArray = expr.toCharArray();
// 运算.
Expression left = null;
Expression right = null;
for (int i=0; i<charArray.length; i++) {
switch(charArray[i]) {
case '+':
left = stack.pop();
right = new VarExpression(String.valueOf(charArray[++i]));
stack.push(new AddExpression(left, right));
break;
case '-':
left = stack.pop();
right = new VarExpression(String.valueOf(charArray[++i]));
stack.push(new SubExpression(left, right));
break;
default:
stack.push(new VarExpression(String.valueOf(charArray[i])));
}
}
// 把运算结果抛出来.
this.expression = stack.pop();
}
/**
* 开始运算.
* @param var
* @return
*/
public int run(Map<String, Integer> var) {
return this.expression.interpreter(var);
}
}
Client.java
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
String expr = readExpressionString();
Map<String, Integer> var = getValue(expr);
Calculator cal = new Calculator(expr);
System.out.println("运算结果为:" + expr + "=" + cal.run(var));
}
/**
* 获得表达式.
* @return
*/
public static String readExpressionString() {
System.out.println("请输入表达式:");
return readLine();
}
private static String readLine() {
try {
final BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static Map<String, Integer> getValue(final String expr) {
Map<String, Integer> map = new HashMap<>();
for(char c : expr.toCharArray()) {
if (c != '+' && c != '-') {
if (!map.containsKey(String.valueOf(c))) {
String in = readLine();
map.put(String.valueOf(c), Integer.valueOf(in));
}
}
}
return map;
}
}