DSA

DSA


#include<iostream>

#include<string>

#include<stack>

using namespace std;

int postfixeval(string exp);


int main()

{

  char exp[20],*e;

  cout<<"Enter postfix expression\n";

  cin>>exp;

  e=exp;

  cout<<"\nConverted expression= "<<postfixeval(e);

  return 0;

}


int postfixeval(string exp)

{

  stack<int>s;

  for(int i=0;i<exp.length();i++)

  {

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

    {

      s.push(exp[i]-'0');

    }

    else

    {

      int x=s.top();

      s.pop();

      int y=s.top();

      s.pop();

      if(exp[i]=='+')

        {s.push(y+x);}

      else if (exp[i]=='-')

        {s.push(y-x);}

      else if (exp[i]=='*')

        {s.push(y*x);}

      else if (exp[i]=='/')

        {s.push(y/x);}

      else if (exp[i]=='^')

        {s.push(y^x);}

    }

  }

  return s.top();

}

Report Page