Change infix expression to suffix expression

Source: Internet
Author: User

The infix expression "9 + (3-1) * 3 + 10/2" is converted to the suffix expression "9 3 1-3 * + 10 2/+"

How to convert an infix expression to a suffix expression:
1. operations encountered: Direct output (added to the suffix expression)
2. When the stack is empty, an operator is used to directly access the stack.
3. Left brackets: add them to the stack
4. Right parenthesis: Execute the stack exit Operation and output the elements of the stack until the left parenthesis is displayed and the left parenthesis is not output.
5. Other operators: subtraction, multiplication, division
6. Finally, the elements in the stack are output in sequence.

Implement 9 + (3-1) * 3 + 10/2, stack = empty
1.9 output expression = 9 stack = NULL
2. + inbound stack expression = 9 stack = +
3. (inbound stack expression = 9 stack = + (
4.3 output, expression = 9 3 stack = + (
5.-In stack, expression = 9 3 stack = + (-
6.1 output, expression = 9 3 1 stack = + (-
7) Compare the stack top expression = 9 3 1-stack = +
8. * inbound stack expression = 9 3 1-stack = + *
9. Output, expression = 9 3 1-3 stack = + *
10. + compare stack top expression = 9 3 1-3 * + stack = +
11.10 output expression = 9 3 1-3 * + 10 stack = +
12./inbound stack expression = 9 3 1-3 * + 10 stack = +/
13.2 output expression = 9 3 1-3 * + 10 2 stack = +/
14. Stack clearing expression = 9 3 1-3 * + 10 2/+ stack = NULL

 

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.