【问题标题】:Infix to postfix using two stacks使用两个堆栈中缀到后缀
【发布时间】:2015-05-22 21:09:55
【问题描述】:

我的评估方法需要帮助。我得到的输出是完全错误的。我的代码这样做:输入:(5 + 3) * 3 输出:操作数堆栈:[ (, 5, + 3 +, ), * 4 * ] 运算符堆栈:[] 输出应该是: 操作数栈: [ 5 3 + 4 * ] 操作数栈: [ ] *注意:数字之间应该有空格

这是我的评估方法:

/**
	 * Evaluates the specified postfix expression. If an operand is encountered,
	 * it is pushed onto the operand stack. If an operator is encountered, two
	 * operands are popped, the expression is evaluated, and the result is
	 * pushed onto the operand stack.
	 * 
	 * @param expr string representation of a postfix expression
	 * @return value of the given expression
	 */
	public String evaluate(String expr) {

		// A variable to hold the resulting postfix expression.
		String result;
		// A string variable to hold the individual elements of user's input string.
		String token;
		// A parser to read the individual tokens of user input, tokens must be separated by whitespace.
		Scanner parser = new Scanner(expr);

		while (parser.hasNext()) {
			// Evaluating the expression one token at a time.
			token = parser.next();
			
			// 1) An operand is either the first or second operand for an operator.
			// If the operator stack is empty or if a left parenthesis is on top of the stack,
			// the operand is the first operand for an operator. Push the operand onto the operand stack.
			if (operatorStack.isEmpty() || operatorStack.peek().equals("(")) {
				// Operand 1 
				operand1 = token;
							
				// Pushing operand1 onto the operand stack
				operandStack.push(operand1);
			}

			// 1) Otherwise, the operand is the second operand for an operator. The first operand should be on top of
			// the operand stack, and the operator should be on top of the operator stack. Pop the
			// first operand and the operator from the stacks, and form the postfix expression
			// representing applying the operator to its two operands. This expression is an operand
			// to which another operator may be applied, so push it onto the operand stack.
		
			else {
				// Operand 2
				String operand2 = token;

				// Popping current operator and operand1
				 operand1 = operandStack.pop();
				 currentOperator = operatorStack.pop();

				// Forming the resulting expression
				 resultingExpression = (operand1 + " " + operand2 + " " + currentOperator + " ");

				// Pushing the resulting expression onto the operandStack
				operandStack.push(resultingExpression);
				
			}
			
			// 2) A right parenthesis marks the end of an infix operand expression. The matching left
			// parenthesis should be on top of the operator stack, and the postfix operand expression
			// corresponding to the infix operand expression should be on top of the operand stack.
			// Pop the left parenthesis off the operator stack. If the stack is now empty or if the token 
			// on top of the stack is another left parenthesis, the operand on top of the operand stack
			// is the first operand for an operator, assuming the end of the whole infix expression has
			// not been reached. In this case, do nothing further.Otherwise, the postfix expression
			// on top of the operand stack is the second operand for an operator. 
			// The first operand should be immediately below it, and the operator should be on
			// top of the operator stack.Pop both operands and the operator from the stacks, and form
			// the postfix expression representing applying the operator to its operands. As in 1
			// above, push this expression onto the operand stack.
			
			if (isRightParen(token)) {
				// Popping left parenthesis
				operatorStack.pop();
				
				if (operatorStack.isEmpty() || operatorStack.peek().equals("(")) {
					// First operand for an operator
					operand1 = operandStack.peek();
				} 
				
				else {

					// Second operand for an operator
					operand2 = operandStack.peek();

					// Pop both operands
					operandStack.pop();
					operandStack.pop();

					// Pop operator
					currentOperator = operatorStack.pop();

					// Forming the resulting expression
					resultingExpression = (operand1 + " " + operand2 + " " + currentOperator + " ");

					// Pushing the resulting expression onto the operandStack
					operandStack.push(resultingExpression);
					}
			

				}
			
			// 3) To process an operator or a left parenthesis, simply push it onto the operator stack.
			
				// If token is an operator push it on the operator stack.
					if (isOperator(token)) {
						operatorStack.push(token);	
							}
				// If token is a left parenthesis push it on to the operator stack.
					else if (isLeftParen(token)) {
						operatorStack.push(token);
						}
			

			
			}
		
		// Returning the completed postfix result
		result = ("Operand Stack: " + operandStack.toString() + " " + "Operator Stack: " + operatorStack.toString());
		return result;
	}

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括... 重现它所必需的最短代码
  • 这是最短的片段

标签: java data-structures stack infix-notation


【解决方案1】:

快速浏览一下您的代码,我可以在不透露实际答案的情况下给您以下建议,因为这似乎是某种类项目?看看 EWD 的 Shunting-yard AlgorithmReverse Polish Notation (RPN)。这两篇维基百科文章都包含描述如何实现算法的伪代码。 我希望这会有所帮助,祝你好运!

【讨论】:

  • 这些都使用运算符的优先级并且只有一个堆栈。我想使用两个堆栈分别处理表达式的每个标记,一个用于运算符,一个用于操作数。
猜你喜欢
  • 2013-02-26
  • 2012-04-05
  • 2013-09-23
  • 2013-03-28
  • 2015-09-12
  • 1970-01-01
  • 2015-05-09
  • 2015-09-13
  • 2013-11-08
相关资源
最近更新 更多