title: Evaluate Infix expression without converting it into postfix
body: <p>I'm trying to evaluate an infix expression in 1 pass without converting it into postfix but it's not giving correct output for some expressions. For eg: 3-5*10/5+10 , (45+5)-5*(100/10)+5</p>
  
  <p>Can someone provide a proper solution to this problem in cpp. </p>
  
  <p>Link to the previous question asked: <a href="https://stackoverflow.com/questions/13421424/how-to-evaluate-an-infix-expression-in-just-one-scan-using-stacks">How to evaluate an infix expression in just one scan using stacks?</a></p>
  
  <p>Please don't mark it as duplicate as I have tried the algorithm answered in the above given thread but to no avail.</p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  
  int isoperand(char x)
  {
      if(x == '+' || x=='-'|| x=='*' || x=='/' || x==')' || x=='(')
          return 0;
      return 1;
  }
  
  int Pre(char x)
  {
      if(x == '+' || x == '-')
          return 1;
      if(x == '*' || x == '/')
          return 3;
      return 0;
  }
  
  int infixevaluation(std::string exp)
  {
      std::stack&lt;int&gt; s1; //Operand Stack
      std::stack&lt;char&gt; s2; //Operator Stack
      int i,x,y,z,key;
      i=0;
      while(exp[i]!='\0')
      {
  
          if(isoperand(exp[i]))
          {
              key = exp[i]-'0';
              s1.push(key);
              i++;
          }
          else if(!isoperand(exp[i]) &amp;&amp; s2.empty())
              s2.push(exp[i++]);
          else if(!isoperand(exp[i]) &amp;&amp; !s2.empty())
          {
              if(Pre(exp[i])&gt;Pre(s2.top()) &amp;&amp; exp[i]!=')')
                  s2.push(exp[i++]);
              else if(exp[i]==')' &amp;&amp; s2.top() == '(')
              {
                  s2.pop();
                  i++;
              }
              else if(exp[i]=='(')
                  s2.push(exp[i++]);
              else
              {
                  x = s1.top();
                  s1.pop();
                  y = s2.top();
                  s2.pop();
                  z = s1.top();
                  s1.pop();
                  if(y == '+')
                      s1.push(z+x);
                  else if(y == '-')
                      s1.push(z-x);
                  else if(y == '*')
                      s1.push(x*z);
                  else if(y == '/')
                      s1.push(z/x);
              } 
          }
      }
      while(!s2.empty())
      {
          x = s1.top();
          s1.pop();
          y = s2.top();
          s2.pop();
          z = s1.top();
          s1.pop();
          if(y == '+')
              s1.push(x+z);
          else if(y == '-')
              s1.push(z-x);
          else if(y == '*')
              s1.push(x*z);
          else if(y == '/')
              s1.push(z/x);
      }
      return s1.top();
  }
  
  int main(int argc, char const *argv[])
  {
      std::string s;
      getline(std::cin,s);
      std::cout&lt;&lt;infixevaluation(s)&lt;&lt;std::endl;
      return 0;
  }
  </code></pre>