1

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.

David_D
  • 25
  • 1
  • 8
  • 3
    Typically, it's easier to learn something if you actually try. Maybe write down the steps on a piece of paper (or document in a text editor if you like), and follow the flow - start with something simple, then work your way up to `(a + (b + c * d) / e)` and such. – Mats Petersson Mar 02 '14 at 14:25
  • Tried, got it working only if I have two operands. I would not post this question here if I could figure it out on my own. – David_D Mar 02 '14 at 14:26
  • 2
    But you are not showing what you have done so far... – Mats Petersson Mar 02 '14 at 14:28
  • Yeah, I posted the part that actually worked and did not look that awful. So can you help me or not ? – David_D Mar 02 '14 at 14:30
  • 2
    The idea is really that YOU do the work (this is clearly for learning purposes, so YOU are the one that needs to learn, and you don't really learn anything by reading/copying someone elses code!), we give you advice. Maybe you are looking for wedoyourhomeworkforyou.com? – Mats Petersson Mar 02 '14 at 14:42
  • Y exactly can't I learn by reading someone elses code , huh ? There is a lot of code in the textbooks, maybe I don't need those as well ?? – David_D Mar 02 '14 at 14:49
  • 1
    Of course, it's POSSIBLE to learn from reading code. However, you will not learn by simply taking someone else's code and inserting it in your program. Because a large part of programming is learning how to think as a programmer - in other words, how to come up with code from a concept. – Mats Petersson Mar 02 '14 at 14:52
  • Mate, I am completely with you on this one , but I was not planning to insert someone else's code in my program, it's just sometimes easier to me to get the idea by actually reading code. And as I stated in my question ,, I am not asking for a code although that would be helpful.''. You could just give some tips instead of being Captain Obvious and giving me a lesson on how should I learn stuff) Anyway, thank you, Mr. Petersson. – David_D Mar 02 '14 at 14:59
  • 1
    My point is that if you show us what you have done, we can guide you. If you are just saying "I want to do X", without actually showing "where you are", then there are two obvious conclusions: 1. You haven't actually done anything, and hopes that the internet is the answer to all questions. 2. You have done something, but isn't showing it, for whatever reason. Unfortunately, without you sharing the code, it's pretty easy to assume #1 of those two. Also, suggesting a way to improve your existing code is much easier than to explain every step through the code in this case. – Mats Petersson Mar 02 '14 at 15:12

1 Answers1

0

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
} 
P0W
  • 46,614
  • 9
  • 72
  • 119
  • 1
    This is untested code, you need to follow it and accordingly modify it as per your need. Its just basic approach. – P0W Mar 02 '14 at 14:46
  • I might be wrong, but the "postfix algorithm" chapter in Wikipedia no longer exists (link is kinda broken) – Thomas Weller Aug 25 '22 at 18:19