Convert infix to suffix and evaluate

Source: Internet
Author: User

I. infix to suffix

Infix to suffix tips:

1. When an operand is encountered, it is output directly.

2. When the stack is empty, an operator is directly added to the stack.

3. In case of left brackets, add them to the stack.

4. When the right parenthesis is encountered, execute the output stack and output the elements of the stack until the left parenthesis is displayed (left and right parentheses are not output ).

5. When an operator is encountered, the top elements (to the output) of the stack whose priority is greater than or equal to the operator are displayed in sequence, and then the elements are added to the stack. Otherwise, the elements are directly added to the stack.

6. The final top elements of the stack are output in sequence.

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

   

Let's take a look at this process.

1. Initialize an empty stack to use the symbol to access the stack.

2. The first character is the number 9, and the output 9 is followed by the symbol "+.

3. The third character is "(", which is still a symbol. Because it is only left parenthesis and has not been paired, It is pushed to the stack.

4. The fourth character is the number 3, the output, the total expression is 9 3, followed by "-" into the stack.

5. next is the number 1, output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the top of the stack in turn out of the stack, and output until "(" goes out of the stack. At this time, there is only "-" above the left bracket, so the output "-" is output, and the total output expression is 9 3 1-

6. followed by the number 3, output. The total expression is 9 3 1-3. Followed by the symbol "*", because the top symbol of the stack is "+", the priority is lower than "*", so no output is made and pushed to the stack.

7. the symbol "+" is followed. At this time, the top element of the current stack has a higher priority than this "+", so the elements in the stack are output and output (there is no lower priority than the "+, the total output expression is 9 3 1-3 * +. then, the current symbol "+" is added to the stack. That is to say, the "+" at the bottom of the stack of the first six images refers to the "+" next to the 9 in the infix expression, and the "+" at the bottom of the stack (also the top of the stack) "+" refers to the last "+" in "9 + (3-1) * 3 + ".

8. After the number is 10, the total expression is 9 3 1-3 * + 10.

9. The last number 2 is output. The total expression is 9 3 1-3 * + 10 2.

10. Because it has reached the end, all the symbols in the stack are output to the stack. The final output suffix expression is 9 3 1-3 * + 10 2/+

2. Calculate the suffix expression

Formulas for calculating suffix expressions:

1. The operands are directly written into the stack.

2. When an operator pops up the top two elements of the stack, the result of the calculation is merged into the stack.

 

Suffix expression: 9 3 1-3 * + 10 2/+

 

The detailed steps are as follows:

 

1. Initialize an empty stack. This operator is used to access and use numbers to be computed.

 

2. In the suffix expression, the first three are numbers, so 9, 3, and 1 are added to the stack.

 

 

 

3. The next step is the minus sign "-". Therefore, the first-out stack in the stack is used as the reduction, the third-out stack is used as the reduction, and 2 is calculated for 3-1, and then 2 is pushed to the stack.

 

4. Next, the number 3 goes into the stack.

 

 

5. The following is the multiplication "*", which means that 3 and 2 in the stack are output stacks, 2 is multiplied by 3, 6 is obtained, and 6 is pushed to the stack.

 

6. The following is the addition "+". Therefore, find "6" and "9", add "9" and "6", get "15", and add "15" to the stack.

 

 

7. The numbers 10 and 2 are added to the stack.

 

8. The following is the symbol. Therefore, 2 and 10 at the top of the stack are separated from 2. 5 is obtained and 5 is pushed to the stack.

 

 

9. The last one is the symbol "+". Therefore, 15 and 5 are searched and added to get 20 and 20 are pushed to the stack.

 

10. The result is 20 and the stack becomes empty.

 

 

Convert infix to suffix and evaluate

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.