13th lesson, calculator core analytic algorithm (middle)

Source: Internet
Author: User

First, infix turn suffix

1, infix expression to postfix expression process similar to the compilation process

(1), the parentheses in the arithmetic expression must match

(2), conversion based on operator precedence

(3), no parentheses in the converted Expression

(4), after the conversion can be sequentially calculated the final result

2. Conversion process

(1), when the element e is a number: output

(2), when the element e is an operator:

A, the precedence comparison with the top of the stack operator

B, less than equals: Output the top element of the stack, go to a execution

C, greater than: the current element e into the stack

(3), the current element is E for the left parenthesis: into the stack

(4), the current element e is a closing parenthesis:

A, eject the top element of the stack and output until the top of the stack is left bracket

B, pop the left parenthesis of the top of the stack from the stack.

Two, key points: bracket matching algorithm

(1), parentheses appear in pairs

(2), opening parenthesis must precede the closing parenthesis

Third, summary

(1), suffix expression is the basis for a program to calculate complex expressions

(2), infix suffix is based on the stack data structure

(3), the conversion process can find errors in the expression

13th lesson, calculator core analytic algorithm (middle)

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.