Polish expression, Reverse Polish Notation

Infix expression

Infix expression is the most common operation expressions, such as: 5 + 3 * (2 + 6) --1

Polish Notation

Polish prefix expression expression is also known, which is conjugated to an expression Central converted after a certain manner of
such expression as infix: 3 + 5x (2 + 6 ) -1 prefix corresponding expression is: - + 3 x 5 + 2 6 1

For right to left traversal infix expressions convert prefix expression, with the middle if the stack is stored
conversion rule is as follows:

  • When faced with right parenthesis when it directly onto the stack
  • When faced with a left parenthesis , the contents of the stack after another from the stack, and classified it into a prefix expression, until it encounters a closing parenthesis , and finally popped right parenthesis, right parenthesis at this time is not classified as a prefix expression formula
  • When faced with the operand when it directly fall under the prefix expression
  • If other operator, the operator compares the current top of the stack and stack operator priority if the priority of the current operator is greater than or equal to the priority of the stack operators, operators direct current onto the stack, if the current operator priority less than the stack operator precedence, the top of the stack the stack operator, and fall into the prefix expression, the operator until the current priority is greater than or equal to the top of the stack operator precedence the current operator onto the stack
  • After the above steps, if the current stack is not empty, the current stack elements sequentially in the stack, and fall into the prefix expressions.
  • Finally, reverse output prefix expression to get the final result.

Poland expression code:

Subject to the provisions: Suppose there is only the following notation expression: left parenthesis, right parenthesis, addition, subtraction, multiplication, and division

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;

stack<char> sta;
char ch[50],res[50];
int ind;

int main()
{
    scanf("%s",ch);
    int len = strlen(ch);
    for(int i=len-1;i>=0;--i)
    {
        if(ch[i]>='0' && ch[i]<='9')
        {
            res[ind++] = ch[i];
            continue;
        }
        if(ch[i]=='+' || ch[i]=='-')
        {
            while(!sta.empty()&&(sta.top()=='*' || sta.top()=='/'))
            {
                res[ind++] = sta.top();
                sta.pop();
            }
            sta.push(ch[i]);
            continue;
        }
        if(ch[i]=='*' || ch[i]=='/')
        {
            sta.push(ch[i]);
            continue;
        }
        if(ch[i] == ')')
        {
            sta.push(ch[i]);
            continue;
        }
        if(ch[i] == '(')
        {
            while(!sta.empty()&&sta.top()!=')')
            {
                res[ind++] = sta.top();
                sta.pop();
            }
            if(!sta.empty())
                sta.pop();
        }
    }
    while(!sta.empty())
    {
        res[ind++] = sta.top();
        sta.pop();
    }
    for(int i=ind-1;i>=0;--i)
        printf("%c",res[i]);

    return 0;
}

Reverse Polish Notation

Reverse Polish notation, also known as postfix expression, expression in a certain neutral conjugated converted from manner
such as infix expression: 3 + 5x (2 + 6 ) -1 suffix corresponding to the expression: 3526+ x1- +
this conversion also requires the stack to store operators, conversion rules are as follows:

  • Conversion is traversed from left to right to convert
  • When faced with a left parenthesis when it directly onto the stack
  • When faced with a right parenthesis , the contents of the stack after another from the stack, and classifies it into the suffix expression, until it encounters a left parenthesis , and finally left parenthesis from the stack, then left parenthesis is not classified as a suffix expression formula
  • When faced with the operand , the direct extension to be classified as an expression
  • If other operator, the operator compares the current top of the stack and stack operator priority if the priority of the current operator is greater than or equal to the priority of the stack operators, operators direct current onto the stack, if the current operator priority less than the stack operator precedence, the top of the stack the stack operator, and classified into postfix, the operator until the current priority is greater than or equal to the top of the stack operator precedence the current operator onto the stack
  • After the above steps, if the current stack is not empty, the current stack elements sequentially in the stack, and classified into the postfix expression.
  • Finally, the positive sequence output can be.

Reverse Polish Notation Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;

stack<char> sta;
char sour[100],res[100];
int ind;

int main()
{
    scanf("%s",sour);
    int len = strlen(sour);
    for(int i=0;i<len;++i)
    {
        if(sour[i]=='(')
        {
            sta.push(sour[i]);
            continue;
        }
        if(sour[i]>='0'&&sour[i]<='9')
        {
            res[ind++] = sour[i];
            continue;
        }
        if(sour[i]=='+'||sour[i]=='-')
        {
            while(!sta.empty()&&(sta.top()=='*'||sta.top()=='/'))
            {
                res[ind++] = sta.top();
                sta.pop();
            }
            sta.push(sour[i]);
            continue;
        }
        if(sour[i]==')')
        {
            while(!sta.empty()&&sta.top()!='(')
            {
                res[ind++] = sta.top();
                sta.pop();
            }
            sta.pop();
            continue;
        }
        if(sour[i]=='*'||sour[i]=='/')
            sta.push(sour[i]);
    }
    while(!sta.empty())
    {
        res[ind++] = sta.top();
        sta.pop();
    }
    for(int i=0;i<ind;++i)
        printf("%c",res[i]);
    return 0;
}

Published 61 original articles · won praise 7 · views 3629

Guess you like

Origin blog.csdn.net/weixin_42469716/article/details/104848033