栈—中缀、后缀表达式

利用栈计算后缀表达式

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
    stack<int> s;
    char str[50];
    cin.getline( str,sizeof(str),'\n' );
    //cout<<strlen(str)<<endl;

    for( int i=0; i<strlen(str); ++i ){

        int num=0;
        if( str[i]<='9'&&str[i]>='0' ){

            while( str[i]<='9'&&str[i]>='0' ){
                num= num*10 + (str[i]-'0');
                ++i;
            }
            s.push( num );
            --i;
            //cout<<"i:  "<<i<<endl;
        }
        else if( str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/' ){

            int a,b,res;
            a = s.top();//cout<<a<<endl;
            s.pop();
            b = s.top();//cout<<b<<endl;
            s.pop();
            switch( str[i] ){

                case '+': res = b+a; break;
                case '-': res = b-a; break;
                case '*': res = b*a; break;
                case '/': res = b/a; break;
            }
            s.push( res );
        }
    }

    cout<<"result:  "<<s.top()<<endl;

    return 0;
}

利用栈,把中缀表达式变成后缀表达式

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

int main()
{
    //定义运算符优先级
    map<char,int> priority;
    priority['('] = 0;
    priority['+'] = priority['-'] = 1;
    priority['*'] = priority['/'] = 2;

    char str[50];
    cin.getline( str,sizeof(str),'\n' );

    stack<char> s;

    for( int i=0; i<strlen(str); ++i ){

        //遇到数字就输出
        if( str[i]<='9'&&str[i]>='0' ){

            while( str[i]<='9'&&str[i]>='0' ){
                cout<<str[i];
                ++i;
            }
            --i;
            cout<<' ';
        }
        //遇到运算符
        else if( str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/' ){

            //如果栈为空,直接压入栈中
            if( s.empty() ){
                s.push( str[i] );
            }
            //如果栈不空,判断当前运算符的优先级。从栈顶开始,将栈中优先级 <= 它的都输出。
            else if( priority[str[i]] <= priority[s.top()] ){

                while(  priority[str[i]] <= priority[s.top()] ){
                    cout<<s.top()<<' ';
                    s.pop();
                    if( s.empty() )  break;
                }
                s.push( str[i] );
            }
            //如果该运算符优先级比栈顶高,则压入栈。
            else {
                s.push( str[i] );
            }

        }
        //如果是 ( ,直接压入栈
        else if( str[i]=='(' ){

            s.push( str[i] );
        }
        // 一旦遇到 ) ,从栈顶开始输出,直到遇到 ( ,左括号不输出,但弹出
        else if( str[i]==')' ){

            while( s.top()!='(' ){
                cout<<s.top()<<' ';
                s.pop();
            }
            s.pop();
        }
    }

    //上面完成之后,如果栈内还有剩余运算符没有输出,都输出
    if( !s.empty() ){
        while ( !s.empty() ){
            if( s.top()=='(' ){
                s.pop();
                continue;
            }
            cout<<s.top()<<' ';
            s.pop();
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/CY05627/article/details/83503793