标签:new input ade NPU break lin case ack xtend
// Expression.java
import java.util.HashMap;
public abstract class Expression {
public abstract int interpreter(HashMap<String , Integer> var);
}
// VarExpression.java
import java.util.HashMap;
public class VarExpression extends Expression {
private String key;
public VarExpression(String key){
this.key = key;
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return var.get(this.key);
}
}
// SymbolExpression.java
import java.util.HashMap;
public class SymbolExpression extends Expression {
protected Expression left;
protected Expression right;
public SymbolExpression(Expression left, Expression right){
this.left = left;
this.right = right;
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return 0;
}
}
// AddExpression.java
import java.util.HashMap;
public class AddExpression extends SymbolExpression {
public AddExpression(Expression left, Expression right){
super(left, right);
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
// SubExpression.java
import java.util.HashMap;
public class SubExpression extends SymbolExpression {
public SubExpression(Expression left, Expression right){
super(left, right);
}
@Override
public int interpreter(HashMap<String, Integer> var) {
return super.left.interpreter(var) + super.right.interpreter(var);
}
}
// Calculator.java
import java.util.HashMap;
import java.util.Stack;
public class Calculator {
private Expression expression;
public Calculator(String expStr){
Stack<Expression> stack = new Stack<Expression>();
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<String, Integer> var){
return this.expression.interpreter(var);
}
}
// Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Client {
public static void main(String[] args) {
String expStr = getExpStr();
HashMap<String, Integer> var = getValue(expStr);
Calculator calculator = new Calculator(expStr);
System.out.println("Result: " + expStr + " = "+ calculator.run(var));
}
public static String getExpStr(){
System.out.println("Please Input Expressions : ");
try {
return (new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static HashMap<String, Integer> getValue(String expStr){
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(char ch: expStr.toCharArray()){
if(ch != ‘+‘ && ch != ‘-‘){
if(!map.containsKey(String.valueOf(ch))){
System.out.println("Please input the value of : " + String.valueOf(ch));
try {
String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
map.put(String.valueOf(ch), Integer.valueOf(in));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return map;
}
}
标签:new input ade NPU break lin case ack xtend
原文地址:https://www.cnblogs.com/nedrain/p/13209377.html