Infix expression to suffix expression

Source: Internet
Author: User

Infix expression to suffix expression
Infix expression to suffix expression

1. Basic Concepts

There are three expressions in a computer: prefix expression (Polish expression), infix expression, and suffix expression (inverse polish expression ).

For example, expression: a + B * (c-d)-e/f

Prefix expression:-+ a * B-cd/ef

Infix expression: a + B * (c-d)-e/f

Suffix expression: abcd-* + ef /-

1.1 features and advantages and disadvantages

Brackets of an infix expression are indispensable. Its advantage is that it complies with our human writing and reading habits, but its disadvantage is that it is not convenient for computer processing.

The disadvantages of prefix and suffix expressions are not our reading habits, but they are easy for computer processing. For example, for a suffix expression, it pushes the operands into the stack all the time. When an operator is encountered, it extracts two numbers from the stack for calculation, then, press the result into the stack. Therefore, prefixes and suffixes are very convenient for computers to process.

Now we want to convert an infix expression to a suffix expression or an input infix expression to a suffix expression.

2 conversion Principle

The principle is not difficult. When we encounter operands, we can directly output them. When we encounter operators (including '(', '+, we need to press the symbol into the stack,

2.1 When:

We need to pop up the symbol from the top of the stack in sequence until '(' is encountered and '(' is popped up. For example: (a * (B + c) in the stack is (* (+, when ')', we will pop up '+ ','('.

2.2 When '(')

There is nothing to say at this time. Press the stack directly.

2.3 when you encounter '+:

We need to compare the priority of the symbol of the top element of the stack with the priority of the input symbol. If the top element of the stack has a high priority, we need to pop up the top element of the stack in sequence, until the top priority of the stack is lower than the input priority or the stack is empty.

For example, a + B + c + d is different from a + B * c + d because of this priority.

2.4 When an operand is encountered:

No doubt, direct output

3 Programs

#include 
 
  #include 
  
   using namespace std; void changePosfix();int priorty(charch); void main(){         changePosfix();} void changePosfix(){         stack
   
    chStack;         charch;         bool bg= false;         while(ch = getchar() )         {                   switch(ch)                   {                   case'(':                            chStack.push(ch);                            break;                    case')':                            while( chStack.top() != '(')                            {                                     cout<
    
     =priorty(ch) )                            {                                     cout<
     
      

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.