我需要一些帮助,因为我无法在任何地方找到合乎逻辑的解释。也许这只是一个小错误,但我已经在代码中待了很长时间,以至于我无法看到它。我添加了注释,我试图在我的堆栈中推送元素。我有两个堆栈,一个用于所有运算符,一个用于操作数。运算符栈工作正常,但操作数堆栈表现得很奇怪。当我试图推送元素时,它会以某种方式用我推送的最新元素替换堆栈元素。当我弹出它时,它始终返回最后一个元素。我有两个不同的类作为运算符和操作数。如果您需要代码,请告诉我。编辑:其他类不做任何事情,虽然只是几个getter,setter和检查令牌的有效性。任何帮助表示赞赏。谢谢!
import java.util.*;
public class Evaluator {
private Stack<Operand> opdStack;
private Stack<Operator> oprStack;
public Evaluator() {
opdStack = new Stack<Operand>();
oprStack = new Stack<Operator>();
// HashMap<String, Operator> operators = new HashMap<String, Operator>();
Operator.operators.put("+",new AdditionOperator());
Operator.operators.put("-", new SubtractionOperator());
Operator.operators.put("*",new MultiplyOperator());
Operator.operators.put("/",new DivisionOperator());
Operator.operators.put("#",new BogusHash());
}
public int eval(String expr) {
String tok = "";
// init stack - necessary with operator priority schema;
// the priority of any operator in the operator stack other then
// the usual operators - "+-*/" - should be less than the priority
// of the usual operators
Operator newOpr = Operator.operators.get("#");
oprStack.push(newOpr);
String delimiters = "+-*/#! ";
StringTokenizer st = new StringTokenizer(expr,delimiters,true);
// the 3rd arg is true to indicate to use the delimiters as tokens, too
// but we'll filter out spaces
while (st.hasMoreTokens()) {
if ( !(tok = st.nextToken()).equals(" ")) { // filter out spaces
//System.out.println("equals");
if (Operand.check(tok)) { // check if tok is an operand
//System.out.println("tokens = "+tok);
//opdStack.push(new Operand ("0"));
opdStack.push(new Operand(tok));
System.out.println("stack peek "+ opdStack.peek().getValue());
//System.out.println(opdStack);
//System.out.println(oprStack);
} else {
//System.out.println("tokens = "+tok);
//System.out.println(newOpr);
if (!Operator.operators.containsKey(tok)) {
//System.out.println("tokens = "+tok);
//System.out.println(newOpr);
System.out.println("*****invalid token******"); System.exit(1);
}
Operator newOpr2 = Operator.operators.get(tok); // POINT 1
//System.out.println("Operator = "+oprStack.peek().priority());
while ( (oprStack.peek()).priority() >= newOpr2.priority()) {
//System.out.println("tokens while = "+tok);
System.out.println("tokens = "+tok);
Operator oldOpr = ((Operator)oprStack.pop());
//System.out.println("Opr"+oldOpr.priority());
Operand op2 = (Operand)opdStack.pop();
Operand op1 = (Operand)opdStack.pop();
System.out.println("run till here");
opdStack.push(oldOpr.execute(op1,op2));
System.out.println("Final res pushed opd= " + opdStack.peek().getValue());
}
oprStack.push(newOpr2);
//System.out.println("operand "+opdStack.toString());
}
}
}
//System.out.println("pop op2 = "+opdStack.pop().getValue()+" and pop op1 = "+opdStack.pop().getValue());
Operator newOp2 = ((Operator)oprStack.pop());
Operand op2 = (Operand)opdStack.get(0);
Operand op1 = (Operand)opdStack.get(1);
System.out.println("opd = "+ op1.getValue() + op2.getValue());
opdStack.push(newOp2.execute(op2, op1));
System.out.println("Full Stack opd size= "+ opdStack.size());
//System.out.println("Full Stack opd= "+ opdStack.toString());
Operand res = (Operand) opdStack.pop();
return res.getValue();
}
}
我的Operand.java类
public class Operand {
static int val=0;
private static String strval="";
public Operand(String tok) {
// TODO Auto-generated constructor stub
strval = tok;
val = Integer.parseInt(strval);
// System.out.println("value = "+val);
}
public Operand(int value){
val = value;
//System.out.println("value = "+val);
}
public static boolean check(String tok) {
// TODO Auto-generated method stub
boolean bool;
try {
Integer.parseInt(tok);
bool = true;
} catch (Exception e){
//System.out.println("Type conversion "+e);
bool = false;
}
return bool;
}
public int getValue(){
//int re = Integer.parseInt(this.toString());
System.out.println("return value = " + val);
return val;
}
}