Analysis of infix expression variable suffix

Source: Internet
Author: User
Tags numeric
infix expression variable suffix expression

@ (Algorithmic Learning)

Converting infix to a corresponding suffix requires that the stack be changed according to the priority.
The specific operation is: we can use a stack, storage is the symbol stack, digital direct output, because the symbol output order and timing needs to be adjusted, so use the stack to do the relay.
When scanning symbols, we say that the priority of the symbol is the out-of-stack priority, which determines who can enter the stack.
Wait until a non-numeric symbol is scanned, then there is a chance to go to the stack, with each non-numeric symbol having a priority definition. The elements that go into the stack, the priority to be reassigned.

In the stack to be compared with the top of the stack, across the threshold of the moment, the outside of the stack element is the outside of the stack of priority, the stack element is the priority of the elements within the stack. Only big outside the stack, can come in. Otherwise, smaller or equal than the top of the stack, you have to let the top of the stack, until there is a smaller stack than the stack top, and then go in.

The overall priority allocation is simple, 1~6.
The left parenthesis outside the stack must go in, so there is a maximum priority of 6, in the future, will be the latest out, therefore, in the future immediately into the smallest 1.

The right parenthesis is outside the stack, and when compared to the top of the stack, it needs to find the other half of the opening parenthesis inside the stack, so its priority is equal to the left parenthesis in the stack, which is 1, which is the smallest. Go into the maximum priority, in fact, the right parenthesis is not going to go.

The priority of the subtraction is 1 higher than the stack outside the stack.
The multiplication corresponds to a small 1.
No matter the stack inside the stack outside the multiplication must be higher than the addition and subtraction.

In this case, when the top of the stack is added and reduced, multiplication must come in.
When the top of the stack is multiplication, the addition and subtraction of the multiplication must be squeezed out first.

In addition, add in, the top of the stack is reduced, the top of the stack to go out, the same is the condition of the stack.
Similarly, multiplication is.
We call it the same kind of remnant.

Generally, an ICP(in coming priority) represents the out-of-stack precedence

The ISP (in stack priority) indicates that

the stack of precedence int ICP (char ch) is defined as follows:
int ICP (char ch)
{
     switch (CH)
    {case
           ' # ':
                 return 0;
          Case ' (':
                 return 6;
          Case ' + ': Case
          '-':
                 return 2;
          Case ' * ': Case '
          /': Case
          '% ':
               return 4;
         Case ') ':
            return 1;
     }
}
the int ISP (char ch) is defined as follows:
int ISP (char ch)
{
     switch (CH)
    {case
           ' # ':
                 return 0;
          Case ' (':
                 return 1;
          Case ' + ': Case
          '-':
                 return 3;
          Case ' * ': Case '
          /': Case
          '% ':
               return 5;
         Case ') ':
            return 6;
     }
}

Examples to be added.

For example: to find infix expression a/b+ (c*d-e*f)/g converted to a suffix expression process, when scanning to F, the elements in the stack are: + (−∗+ (-* scan to a, output scan to/, stack, stack sequence:/scan to B, output scan to +,+ not come, need to let '/' go out , stack sequence: + Scan to (, into the stack, stack sequence is: + (scan to C, output scan to, stack, stack sequence: + (scan to-, need to eject *, stack sequence becomes + (-scan to E, output scan to *, stack, stack sequence: + (-* scan to F, output

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.