前缀表达式同时评估多个表达式 [英] Prefix expression to evaluate multiple expressions simultaneously
问题描述
private class InputListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Stack<Integer> operandStack = new Stack<Integer>();
Stack<Character> operatorStack = new Stack<Character>();
String input = inputTextField.getText();
StringTokenizer strToken = new StringTokenizer(input, " ", false);
while (strToken.hasMoreTokens())
{
String i = strToken.nextToken();
int operand;
char operator;
try
{
operand = Integer.parseInt(i);
operandStack.push(operand);
}
catch (NumberFormatException nfe)
{
operator = i.charAt(0);
operatorStack.push(operator);
}
}
int result = sum (operandStack, operatorStack);
resultTextField.setText(Integer.toString(result));
}
我的前缀表达式代码只会一次评估一个表达式(即+ 3 1)。我希望它以一个用户输入表达式(即* + 16 4 + 3 1)来评估多个表达式。如何编辑提供的代码,使其评估多个表达式?感谢您的帮助。
My prefix expression code will only evaluate one expression at a time (i.e. + 3 1). I want it to evaluate multiple expressions in one user-input expression (i.e. * + 16 4 + 3 1). How can I edit the code provided to make it evaluate multiple expressions? Thank you for your help.
推荐答案
为了简化您的程序,您可以使用循环来继续操作 operandStack
并将上一个结果的结果推送到堆栈。我留下了我的 println
语句,所以你可以看到它在做什么。此外,我修改了您的方法,以便它可以坐在一个独立的主要
方法。
To simply make your program do a bit more you can use a loop to keep operating on the operandStack
and pushing the result of the previous result to the stack. I left my println
statements in so you can see what its doing. Also I modified your method so it can sit inside a standalone main
method.
你应该研究分流 - 院子里的算法,它很有趣的实现,它有点像你在这里做什么。 http://en.wikipedia.org/wiki/Shunting-yard_algorithm
You should look into the Shunting-yard algorithm, its quite fun to implement and it is somewhat like what your doing here. http://en.wikipedia.org/wiki/Shunting-yard_algorithm
public static void main(String[] args) {
Stack<Integer> operandStack = new Stack<Integer>();
Stack<Character> operatorStack = new Stack<Character>();
String input = "12 + 13 - 4";
StringTokenizer strToken = new StringTokenizer(input, " ", false);
while (strToken.hasMoreTokens()) {
String i = strToken.nextToken();
int operand;
char operator;
try {
operand = Integer.parseInt(i);
operandStack.push(operand);
} catch (NumberFormatException nfe) {
operator = i.charAt(0);
operatorStack.push(operator);
}
}
// loop until there is only 1 item left in the operandStack, this 1 item left is the result
while(operandStack.size() > 1) {
// some debugging println
System.out.println("Operate\n\tbefore");
System.out.println("\t"+operandStack);
System.out.println("\t"+operatorStack);
// perform the operations on the stack and push the result back onto the operandStack
operandStack.push(operate(operandStack, operatorStack));
System.out.println("\tafter");
System.out.println("\t"+operandStack);
System.out.println("\t"+operatorStack);
}
System.out.println("Result is: " + operandStack.peek());
}
/**
* Performs math operations and returns the result. Pops 2 items off the operandStack and 1 off the operator stack.
* @param operandStack
* @param operatorStack
* @return
*/
private static int operate(Stack<Integer> operandStack, Stack<Character> operatorStack) {
char op = operatorStack.pop();
Integer a = operandStack.pop();
Integer b = operandStack.pop();
switch(op) {
case '-':
return b - a;
case '+':
return a + b;
default:
throw new IllegalStateException("Unknown operator '"+op+"'");
}
}
我离开操作
方法(以前称为 sum
)尽可能接近你所拥有的,但我认为您的代码可以通过简单地传递2个整数和一个运算符到该函数来改进。使这个函数改变你的堆栈不是一件好事,可能会导致混乱的问题。
I left the operate
method (previously called sum
) as close to what you had it as possible, however I think that your code could be improved by simply passing 2 integers and a operator to the function. Making the function alter your stacks isnt a great thing and could lead to confusing problems.
考虑使用你的方法签名:
Consider making your method signature this instead:
private static int operate(Integer a, Integer b, char operator) {
switch(operator) {
case '-':
return b - a;
case '+':
return a + b;
default:
throw new IllegalStateException("Unknown operator '"+operator+"'");
}
}
然后从堆栈中弹出并传递给方法。保持堆栈在一个地方更改代码。
and then popping from the stack and passing those to the method. Keeping your stack altering code all in one place.
operandStack.push(operate(operandStack.pop(), operandStack.pop(), operatorStack.pop()));
这篇关于前缀表达式同时评估多个表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!