Inverse Polish algorithm

Source: Internet
Author: User

0 7

Priority level 0 1 2 3 4 5) 6 7
Operator # +- */      @~     !%^   )
Description














First, generate inverse Polish expressions

First build a memory , another symbol stack , storage is left-to-right storage data, and the symbol stack adhere to the principle of LIFO, the calculation of the expression in order from left to right scan.

*  read into a data (note: Numeric and function names are not single characters, need to be judged)

1. If the Zodan operator or function name, directly into the symbol stack, such as the sign ~ @ Max sin

2. If the right single-eye operator, directly into the memory stack, such as factorial! % with percent percent

3. If it is traffic, write directly to the memory, check whether the top of the symbol stack has a single-eye operator, and some words are all out of the stack, and write to the memory;

4. If the opening parenthesis "(", then directly into the symbol stack;

5.  Span style= "line-height:1.5;" > ")" ( Span style= "line-height:1.5;" >)

6. If the binocular operator, then the stack top symbol priority, if it is greater than the top of the stack, then the stack top symbol and write to the memory, until the top of the stack of the operation priority of the symbol;

7 . If the function parameter is concatenated with a comma "," the symbol stack data pops up until a left parenthesis ( or comma ) is encountered, and then the comma , into the symbol stack;

8 . If it is terminator (indicating that the expression has been read all), the symbol stack pops up and writes to the memory, otherwise continues to read the next data according to the above rules;

The inverse Polish-style generation process is described in steps below:

The expression is scanned from left to right and the characters marked red are the characters scanned for the current step:

0 to an expression End identifier " #

the post-processing expression is: the ( 8+4^2 ) *3#

1,2+(8+4^2)*3#:Scan "2", by rule3, the calculation amount2Press into the memory as follows:

Memory

2


Symbol stack: Temporary space


2,2+ (8+4^2)*3#:Scan "+", by rule6, press the plus sign into the symbol stack , as follows:

2

+


This article is from the "11486263" blog, please be sure to keep this source http://11496263.blog.51cto.com/11486263/1783907

Inverse Polish algorithm

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.