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.