RPN evaluation C++

亡梦爱人 提交于 2019-12-11 08:04:28

问题


Hello) This is my code for converting from a infix expression to a postfix one , however I just can't understand how can I evaluate the postfix expression that I get and I will be very grateful for any tips. I am not asking for a code although that would be helpful.

#include <iostream> 
#include <stack> 
#include <string>

using namespace std; 
bool operation(char b) 
{
    return b=='+' || b=='-' || b=='*' || b=='/' ; 

} 

bool priority(char a, char b) 
{
    if(a=='(')
    {
        return true; 
    } 
    if(a=='+' || a=='-') 
    {
        return true; 

    } 
    if(b=='+' || b=='-') 
    {
        return false; 
    } 

    return true; 

} 
int main() 
{

    string a;
    string res; 
    stack<char> s; 
        cin>>a; 
    for(int i=0; i<a.size(); i++) 
    {
        if(a[i]!='(' && a[i]!=')' && operation(a[i])==false) 
        {

            res=res+a[i]; 
        } 
        if(a[i]=='(') 
        {
            s.push(a[i]) ; 
        } 
        if(a[i]==')') 
        {
            while(s.top()!='(') 
            {
                res+=s.top(); 
                s.pop();
            }
            s.pop(); 
        } 
        if(operation(a[i])==true) 
        {
            if(s.empty() || (s.empty()==false && priority(s.top(), a[i])) ) 
            {
                s.push(a[i]); 
            }
            else 
            {
                while(s.empty()==false && priority(s.top(),a[i])==false ) 
                { 
                    res+=s.top(); 
                    s.pop(); 
                }
                s.push(a[i]) ; 
            }

        } 

    } 
    while(s.empty()==false) 
    {
        res+=s.top(); 
        s.pop(); 
    } 
    cout<<res; 




    return 0; 
} 

P.S. I don't have any comments but I suppose that the code itself is self-explanatory))
P.P.S. Thank you in advance.


回答1:


If you form your posfix expression separated by space, following will be one of the easiest way to code the evaluator, just merely following the algorithm of evaluation

This assumes RPN like 5 1 2 + 4 * + 3 - (separated by space)

int evaluate_posfix ( const std::string& expression )
{

    int l,r,ans;
    std::stringstream postfix(expression);
    std::vector<int> temp;
    std::string s;
    while ( postfix >> s )
    {
        if( operation(s[0]) )
        {
            //Pull out top two elements
            r = temp.back();
            temp.pop_back();
            l = temp.back();
            temp.pop_back();
            // Perform the maths
            switch( s[0])
            {
                case '+': ans =  l + r ; break;
                case '-': ans =  l - r ; break;
                case '*': ans =  l * r ; break;
                case '/': ans =  l / r ; break; // check if r !=0
            }

            temp.push_back( ans ); // push the result of above operation
        }
        else
        {
            temp.push_back( std::stoi(s) );
        }
    }

    return temp[0] ; //last element is the answer
} 


来源:https://stackoverflow.com/questions/22128772/rpn-evaluation-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!