从中缀转换为前缀

kar*_*kar 15 algorithm prefix notation

我正在准备考试,我无法理解中缀符号的转换为以下表达式的波兰符号:

(a–b)/c*(d + e – f / g)
Run Code Online (Sandbox Code Playgroud)

任何人都可以一步一步地告诉给定的表达式如何转换为前缀?

小智 26

Algorithm ConvertInfixtoPrefix

Purpose: Convert an infix expression into a prefix expression. Begin 
// Create operand and operator stacks as empty stacks. 
Create OperandStack
Create OperatorStack

// While input expression still remains, read and process the next token.

while( not an empty input expression ) read next token from the input expression

    // Test if token is an operand or operator 
    if ( token is an operand ) 
    // Push operand onto the operand stack. 
        OperandStack.Push (token)
    endif

    // If it is a left parentheses or operator of higher precedence than the last, or the stack is empty,
    else if ( token is '(' or OperatorStack.IsEmpty() or OperatorHierarchy(token) > OperatorHierarchy(OperatorStack.Top()) )
    // push it to the operator stack
        OperatorStack.Push ( token )
    endif

    else if( token is ')' ) 
    // Continue to pop operator and operand stacks, building 
    // prefix expressions until left parentheses is found. 
    // Each prefix expression is push back onto the operand 
    // stack as either a left or right operand for the next operator. 
        while( OperatorStack.Top() not equal '(' ) 
            OperatorStack.Pop(operator) 
            OperandStack.Pop(RightOperand) 
            OperandStack.Pop(LeftOperand) 
            operand = operator + LeftOperand + RightOperand 
            OperandStack.Push(operand) 
        endwhile

    // Pop the left parthenses from the operator stack. 
    OperatorStack.Pop(operator)
    endif

    else if( operator hierarchy of token is less than or equal to hierarchy of top of the operator stack )
    // Continue to pop operator and operand stack, building prefix 
    // expressions until the stack is empty or until an operator at 
    // the top of the operator stack has a lower hierarchy than that 
    // of the token. 
        while( !OperatorStack.IsEmpty() and OperatorHierarchy(token) lessThen Or Equal to OperatorHierarchy(OperatorStack.Top()) ) 
            OperatorStack.Pop(operator) 
            OperandStack.Pop(RightOperand) 
            OperandStack.Pop(LeftOperand) 
            operand = operator + LeftOperand + RightOperand 
            OperandStack.Push(operand)
        endwhile 
        // Push the lower precedence operator onto the stack 
        OperatorStack.Push(token)
    endif
endwhile 
// If the stack is not empty, continue to pop operator and operand stacks building 
// prefix expressions until the operator stack is empty. 
while( !OperatorStack.IsEmpty() ) OperatorStack.Pop(operator) 
    OperandStack.Pop(RightOperand) 
    OperandStack.Pop(LeftOperand) 
    operand = operator + LeftOperand + RightOperand

    OperandStack.Push(operand) 
endwhile

// Save the prefix expression at the top of the operand stack followed by popping // the operand stack.

print OperandStack.Top()

OperandStack.Pop()

End
Run Code Online (Sandbox Code Playgroud)


T.E*_*.D. 5

如果有什么关于什么是中缀和前缀意味着你不太明白,我强烈建议你重读你教科书的那一部分.如果你对这个问题给出了正确的答案,你就不会给自己任何好处,但仍然不理解这个概念.

算法方面,它非常简单.你自己就像电脑一样.首先按照计算的顺序在每个计算周围放置一个parens.然后(再次按照从第一次计算到最后的顺序),只需在左侧的表达式前面移动操作符.之后,您可以通过删除parens来简化.


Cal*_*ngh 5

(a–b)/c*(d + e – f / g)

前缀表示法(反向抛光有操作符最后一个,不清楚你的意思,但原理将完全相同):

  1. (/ f g)
  2. (+ d e)
  3. (- (+ d e) (/ f g))
  4. (- a b)
  5. (/ (- a b) c)
  6. (* (/ (- a b) c) (- (+ d e) (/ f g)))

  • 您的前缀输出似乎不正确。正确的应该是`*/-abc-+de/fg` (2认同)

Tho*_*ini -1

也许您正在谈论逆波兰表示法?如果是,您可以在维基百科上找到非常详细的转换步骤示例;如果不是,我不知道你在问什么:(

您可能还想阅读我对另一个问题的回答,我在其中提供了这样的实现:C++简单操作(+,-,/,*)评估类