infix expression Suffix expression:
(1+3)/8*3-5=
Constructs an empty operator stack. First press inside a ' = ' (easy to compare behind). The infix expression is then scanned from left to right, if it is an operand, the direct output can be, if the left parenthesis is directly into the stack, if the closing parenthesis, then the stack, until the opening parenthesis and open the left parenthesis, if it is other operators, then the multiplication precedence over the addition and subtraction, if it is multiplication (plus minus) who first, That is, a takes precedence over B for a>b, and if the top element of the stack takes precedence over the current element, the stack is stacked until the top priority of the stack is less than the current element priority, and the current element is stacked, if the priority is the same, just the stack.
/* Compare stack top elements with current operator precedence */char suf_compare (char Op1,char op2) { if (op1== ' + ' | | op1== '-') { if (op2== ' (' | | op2== ' * ' | | op2== '/') return ' < '; else return ' > '; } if (op1== ' * ' | | op1== '/') { if (op2== ' (') return ' < '; else return ' > '; } if (op1== ' = ' | | op1== ' (') { if ((op1== ' = ' &&op2== ' = ') | | (op1== ' (' &&op2== ') ')) { return ' = '; } else{ return ' < ';}}
infix expression Prefix expression:
Build two stacks, a cache operator empty stack (press in ' = ') and an empty stack with a prefix expression. The infix expression is scanned from right to left, and if it is an operand, it is pressed directly into the expression stack, and if it is a closing parenthesis, it is directly into the operator stack and , if it is an opening parenthesis, pushes the stack of operators into the expression stack until the closing parenthesis is encountered and the parentheses are discarded. If it is a different operator, then the multiplication takes precedence over the plus and minus, and if it is multiplication (plus minus) then follow the stack who takes precedence. If the top element of the stack takes precedence over the current element, the stack will be stacked until the top of the stack is less than the current element priority, and the current element is stacked, and if the priority is the same, just the stack.
/* The operator comparison function for the infix prefix, and the Fu top element is compared to the current operator precedence. */char Pre_compare (char A,char b) { if (a== ' + ' | | a== '-') { if (b== ' | | b== ' = ') { return ' > '; } else{ return ' < '; } } if (a== ' * ' | | a== '/') { if (b== ') ' | | b== ' * ' | | b== '/') { return ' < '; } else{ return ' > '; } } if (a== ') ' | | a== ' = ') { if (a== ') ' &&b== ' (' | | | a== ' = ' &&b== ' = ') { return ' = '; } else{ return ' < ';}}
Infix expression-to-suffix expression and prefix expression