An infix expression is a common expression, while a suffix expression is a computer expression.
For example, if there is an infix expression a * (B + C)/(d-e), the process of manually converting it to a suffix expression is to put the operator after the operand by priority.
In the above example, the brackets have the highest priority. First, calculate the * (BC +)/(De-) in the brackets. In fact, you can remove the brackets. In this step, ABC + * de -/.
In computer systems, converting an infix expression to a suffix expression is often implemented using stacks. One stack stores the operands and one stack stores the operators.
1. When an operand is encountered, it is directly placed on the operand stack.
2. If it is not an operand, it can be divided into the following situations:
(1) If the operator stack is empty
(2) If the stack is not empty and the top-stack operator has a higher priority than the current operator, it is written into the stack. If the top-stack operator has a higher priority than or equal to the top-stack operator, the operator stacks out of the stack, until the top-stack operator has a higher priority than the current operator.
(3) if it is a left brace '(',
(4) If it is a right brace ')', the operator stack always goes out of the stack, and the elements of the exit Stack are placed in the operand stack until the left brace '(' is encountered '('.
Finally, if the operator stack is not empty, extract the operator stack elements from the stack and input them to the operand stack.
# Include <iostream> # include <stack> # include <string>/* determine whether the operator is @ Param C: character @ return: true or false */inline bool isoperator (const char & C) {Switch (c) {Case '+': Case '-': Case '*': Case '/': case '(': case') ': Return true;} return false;}/* determine the operator priority @ Param C: Operator @ return: +-the priority is less than */inline int priority (const char & C) {Switch (c) {Case '+': Case '-': return 1; case '*': case'/': return 2;} return 0;}/* convert the infix expression into a suffix expression @ Param INF IX: infix expression @ return: suffix expression */STD: String Postfix (const STD: string & infix) {STD: Stack <char> operatorstack; STD :: string post; int Len = infix. length (); For (INT I = 0; I <Len; ++ I) {// if it is an operator if (isoperator (infix [I]) {// if it is a right brace, find the left brace if (infix [I] = ') {char C; while (true) {c = operatorstack. top (); operatorstack. pop (); If (C! = '(') Post. push_back (c); elsebreak ;}// if the operator stack is empty, the top operator priority is smaller than the current operator, or the left bracket else if (operatorstack. empty () | (priority (infix [I])> priority (operatorstack. top () | infix [I] = '(') operatorstack. push (infix [I]); // the priority of the current operator is not lower than that of the top operator of the stack. Else if (priority (infix [I])> = Priority (operatorstack. top () {char C; while (! Operatorstack. empty () {c = operatorstack. top (); If (priority (c)> = Priority (infix [I]) {operatorstack. pop (); Post. push_back (c);} elsebreak;} operatorstack. push (infix [I]) ;}// otherwise it is the operand elsepost. push_back (infix [I]);} while (! Operatorstack. empty () {post. push_back (operatorstack. top (); operatorstack. pop ();} return post;} int main () {STD: String STR = "A * (B + C)/(d-e)"; STD :: cout <Postfix (STR) <STD: Endl; return 0 ;}
Infix expression to suffix expression