Method of infix expression suffix expression

Source: Internet
Author: User

Usually used in the standard arithmetic expression, that is, "9+ (3-1) *3+10/2" is called infix expression. Because all the operational symbols are in the middle of two digits.

This infix expression "9+ (3-1) *3+10/2" into the suffix expression "9 3 1-3*+ 10 2/+", how to do it.

The rules are as follows:

1. Operand encountered: Direct output (added to suffix expression) 
2. When the stack is empty, the operator is encountered, directly into the stack 
3. Open parenthesis: Put it into stack 
4. The closing parenthesis is encountered: Perform a stack operation and output the elements out of the stack until the pop-up stack is left parenthesis and the left parenthesis
does not output. 
5. Encountered other operators: subtraction: Pop-up all priority greater than or equal to the operator of the top of the stack element, then the
operator into the stack ....
6. Finally, the stack of elements in sequence out of the stack, output.

For example a+b* (C (d+f))/e The process of the suffix expression is as follows:

1, read a, direct output a
2, read +, put in the stack
3, read B, direct output, at this time the stack has +, output AB
4, read *, because * priority is higher than +, into the stack, the stack has + * (right for the top of the stack)
5, read (, the highest priority, Encountered) only out, into the stack, the stack has + * (
6, read C, output ABC
7, read-, into the stack, the stack has + *  (-
8, read (, into the stack, the stack has +  *  (
9, read D, output ABCD
10, read +, into the stack, the stack has +  *  (  -(+
11, read F, Output ABCDF
12, read), out stack +, output abcdf+, the stack has +  *  (-
13, read), out Stack-. Output abcdf+-, the stack has +  *
14, read/, out stack *, into the stack/, output abcdf+-*, the stack has +/
15, read E, output abcdf+-*e
15, out stack/, Output abcdf+-*e/
16, out Stack +, output abcdf+-*e/+
so suffix expression is abcdf+-*e/+

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.