Infix expression to postfix (reverse Polish) expression

in principle:

Traverse infix expressions from left to right:

1. If it is a number, it will be output directly;

2. If it is a left parenthesis, it is directly pushed onto the stack;

3. If it is a right parenthesis, directly pop the top element of the stack and output it, until it encounters a left parenthesis, the left parenthesis is only popped off the stack, not output;

4. If it is a symbol, if the priority is not higher than the symbol on the top of the stack, the top elements of the stack are popped out of the stack and output in turn

If the + and - symbols are encountered, if the stack is not empty and the top element of the stack is not a left parenthesis, pop the stack and output, otherwise the current symbol is pushed onto the stack

If it is a *, / symbol, if the stack is not empty and the top element of the stack is * or /, pop the stack and output, otherwise the current symbol is pushed onto the stack

You can refer to a flowchart on the Internet: https://blog.csdn.net/qq_34992845/article/details/70313588

C++ implementation code:

#include<iostream>

#include<algorithm>

#include<string>

#include<stack>

using namespacestd;


int main(){

    string s;

    string s_back;

    getline ( cin , s);

    stack < char > stk;

    

    for(int i=0;i<s.size();i++){

        if(s[i]>='0'&&s[i]<='9'){

            s_back.push_back(s[i]);

            if (s[ i + 1 ]> '9' ||s[ i + 1 ]< '0' )s_back. push_back ( ' ' ); //In order to ensure continuous output of multiple digits such as 10 

        }

        else if(s[i]=='+'||s[i]=='-'){

            while(1){

                if (!stk.empty ( ) && stk.top ( )!= '(' ){ //+ and - in the expression have the lowest priority, so as long as the top of the stack is not '(', it will pop out of the stack 

                    s_back.push_back ( stk.top ()); // To separate with spaces

                    s_back.push_back(' ');

                    PCS. pop ();

                }

                else{

                    stk.push(s[i]);

                    //s_back.push_back(' ');

                    break;

                }

            }

        }

        else if(s[i]=='*'||s[i]=='/'){

            while(1){

                if (!stk.empty ( ) && (stk.top ( )== '*' || stk.top ( )== '/' )){ //The priority is not higher than */ 

                    s_back.push_back(stk.top());

                    s_back.push_back ( ' ' ); //To separate with spaces 

                    PCS. pop ();

                }

                else{

                    stk.push(s[i]);

                    break;

                }

            }

        }

        else if(s[i]=='('){

            stk.push(s[i]);

        }

        else if(s[i]==')'){

            while(1){

                if (stk. top ()! = '(' ) {

                    s_back.push_back(stk.top());

                    s_back.push_back ( ' ' ); //To separate with spaces 

                    PCS. pop ();

                }

                stk.pop (); // delete '(' 

                break;

            }

        }

    }

    while(!stk.empty()){

        s_back.push_back(stk.top());

        s_back.push_back ( ' ' ); //To separate with spaces 

        PCS. pop ();

    }

    cout<<s_back<<endl;

    return0;

}


operation result:

9+(3-1)*3+10/2

9 3 1 - 3 * + 10 2 / + 

Program ended with exit code: 0


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933991&siteId=291194637