Infix expression to suffix expression, infix expression suffix

Source: Internet
Author: User

Infix expression to suffix expression, infix expression suffix
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 <iostream>#include <stack>using namespace std; void changePosfix();int priorty(charch); void main(){         changePosfix();} void changePosfix(){         stack<char>chStack;         charch;         bool bg= false;         while(ch = getchar() )         {                   switch(ch)                   {                   case'(':                            chStack.push(ch);                            break;                    case')':                            while( chStack.top() != '(')                            {                                     cout<<chStack.top();                                     chStack.pop();                            }                            chStack.pop();                            break;                    case'+':                   case'-':                   case'*':                   case'/':                            //for example a+b*c+d,when meet last                            while( !chStack.empty() &&priorty(chStack.top() )>=priorty(ch) )                            {                                     cout<<chStack.top();                                     chStack.pop();                            }                            // at last put ch in the end                            chStack.push(ch);                            break;                    case'\n':                            while( !chStack.empty() )                            {                                     cout<<chStack.top();                                     chStack.pop();                            }                            bg = true;                            break;                    default:                            cout<<ch;                   }                   if(bg )                            break;         }} inline int priorty(char ch){         switch(ch)         {         case '+':         case '-':                   return1;         case '*':         case'/':                   return2;         default:                   return0;         }}


 


How can I convert an infix expression to a prefix suffix expression?

Infix-to-suffix expression: Two stacks are designed. One is the operator type, which stores operators to convert arithmetic expressions into expressions without parentheses. The other is the floating-point type, which stores the operands, evaluate an unsigned expression. Assume the operator priority: (); */; + -.
First, use a left brace '(' into the stack as the bottom element of the stack; then scan the arithmetic expression from left to right, and read a character each time. If the left brace '(', then it is pushed to the stack. If an operand is encountered, it is immediately output. If an operator is encountered, if its priority is higher than that of the top element of the stack, it is directly pushed to the stack, otherwise, the top element of the stack is output until the new top element has a lower priority than the top element of the stack, and the new top element is pushed to the stack. If it encounters a right brace ')', the operator at the top of the stack is output, and the element at the top of the stack is '('. After talking about it, the left and right parentheses offset each other. If the final expression is to be scanned, output all operators in the stack to eliminate the left brackets '(' at the bottom of the stack '('.
I just learned how to use the data structure. Can you see if it helps you?

Infix expression to suffix expression

First, you need to set the stack st of an operator. from the left, only the scan infix expression is required.
1. if you encounter a number, put it directly at the end of the suffix expression;
2. If an operator is encountered
A: if the site is empty at this time, it will directly go to the stack;
B: loop: If the stack st is not empty and the top operator priority is greater than or equal to the current operator, the top operator is placed at the end of the suffix expression;
C: If the stack st is not empty and the top of the stack operator has a lower priority than the current operator, the operator is directly added to the stack;
Repeat 1 and 2 to know that the whole infix expression scan is complete. If the stack st is not empty at this time, the operators at the top of the stack are output to the stack and placed at the end of the suffix expression in sequence.

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.