~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package pattern;
import java.util.HashMap;
public abstract class Expression {
// 解析公式和数值, 其中var中的key值是是公式中的参数, value值是具体的数字
public abstract int interpreter(HashMap var);
}
package pattern;
import java.util.HashMap;
public class VarExpression extends Expression {
private String key;
public VarExpression(String _key){
this.key = _key;
}
@Override
public int interpreter(HashMap var) {
// 从map中取值
return var.get(this.key);
}
}
package pattern;
public abstract class SymbolExpression extends Expression {
protected Expression left;
protected Expression right;
// 所有的解析公式都应只关心自己左右两个表达式的结果
public SymbolExpression(Expression _left, Expression _right){
this.left = _left;
this.right = _right;
}
}
package pattern;
import java.util.HashMap;
public class AddExpression extends SymbolExpression {
public AddExpression(Expression _left, Expression _right){
super(_left, _right);
}
// 把左右两个表达式运算的结果加起来
@Override
public int interpreter(HashMap var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
package pattern;
import java.util.HashMap;
public class SubExpression extends SymbolExpression {
public SubExpression(Expression _left, Expression _right){
super(_left, _right);
}
// 把左右两个表达式运算的结果相减
@Override
public int interpreter(HashMap var) {
return super.left.interpreter(var) - super.right.interpreter(var);
}
}
package pattern;
import java.util.HashMap;
import java.util.Stack;
public class Calculator {
// 定义的表达式
private Expression expression;
// 构造函数传参,并解析
public Calculator(String expStr){
// 定义一个堆栈,安排运算的先后顺序
Stack stack = new Stack();
// 表达式拆分为字符数组
char[] charArray = expStr.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();
}
// 开始运算
public int run(HashMap var){
return this.expression.interpreter(var);
}
}
package user;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import pattern.Calculator;
public class Client {
// 进行四则运算
public static void main(String[] args) throws IOException {
String expStr = getExpStr();
// 赋值
HashMap var = getValue(expStr);
Calculator cal = new Calculator(expStr);
System.out.println("运算结果为:" + expStr + "=" + cal.run(var));
}
// 获得表达式
public static String getExpStr() throws IOException{
System.out.print("请输入表达式:");
return (new BufferedReader(new InputStreamReader(System.in))).readLine();
}
// 获得值映射
public static HashMap getValue(String exprStr) throws IOException{
HashMap map = new HashMap();
// 解析有几个参数要传递
for(char ch : exprStr.toCharArray()){
if(ch != '+' && ch != '-'){
if(!map.containsKey(String.valueOf(ch))){ // 解决重复参数的问题
System.out.print("请输入" + ch + "的值:");
String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
map.put(String.valueOf(ch), Integer.valueOf(in));
}
}
}
return map;
}
}