Infix expression-to-suffix expression and prefix expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.