title: How to find wrong infix expression?
body: <p>I have to convert infix expression to postfix. My Infix to Postfix code is working without any error. But I have to also find the wrong infix expression. How to solve this problem? </p>
  
  <p>Here is my code : ( I have used my custom stack file)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "stacktype.cpp"
  using namespace std;
  
  string infixToPostFix(string infix);
  int higherPrecedenceValidate(char op1, char op2);
  int getPrecedence(char op);
  int evaluatePostFix(string postfix);
  
  int main()
  {
      string infix,postfix;
      int result;
      cin &gt;&gt; infix;
      postfix = infixToPostFix(infix);
      cout &lt;&lt; postfix &lt;&lt;endl;
      result = evaluatePostFix(postfix);
      cout &lt;&lt; "result: " &lt;&lt; result &lt;&lt; endl; 
  
  }
  
  
  string infixToPostFix(string infix){
      StackType&lt;char&gt; operators;
      string postfix;
      for(int i = 0; i &lt; infix.size(); i++){
          // Checking Operator
          if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/'){
              while (!operators.IsEmpty() &amp;&amp; higherPrecedenceValidate(operators.Top(),infix[i]))
              {
                  postfix = postfix + operators.Top();
                  operators.Pop();
              }
              operators.Push(infix[i]);
  
          }
          // Checking Operand
          else if(infix[i] &gt;= '0' &amp;&amp; infix[i] &lt;= '9')
          {
              postfix = postfix + infix[i];
  
          }
          //Checking open bracket
          else if(infix[i] == '(' ){
              operators.Push(infix[i]);
          }
  
          //Checking closing bracket
          else if(infix[i] == ')' ){
              while (!operators.IsEmpty() &amp;&amp; operators.Top() != '(')
              {
                  postfix = postfix + operators.Top();
                  operators.Pop();
              }
              // poping the opening bracket
              operators.Pop();
          }
  
  
      }
      // poping rest of element from the stack..
      while (!operators.IsEmpty())
      {
          postfix = postfix + operators.Top();
          operators.Pop();
      }
      return postfix;
  }
  
  int evaluatePostFix(string postfix){
      StackType&lt;int&gt; finalNumbers;
      for(int i = 0; i &lt; postfix.size(); i++){
          // Checking Operator
          if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/'){
              int resultOfTwoNumber;
              int number2 = finalNumbers.Top();
              finalNumbers.Pop();
              int number1 = finalNumbers.Top();
              finalNumbers.Pop();
              switch (postfix[i])
              {
                  case '+':
                      resultOfTwoNumber = number1 + number2;
                      break;
                  case '-':
                      resultOfTwoNumber = number1 - number2;
                      break;
                  case '*':
                      resultOfTwoNumber = number1 * number2;
                      break;
                  case '/':
                      resultOfTwoNumber = number1 / number2;
                      break;
              }
              finalNumbers.Push(resultOfTwoNumber);
  
          }
          // Checking Operand
          else if(postfix[i] &gt;= '0' &amp;&amp; postfix[i] &lt;= '9')
          {
              finalNumbers.Push(postfix[i] - '0');
  
          }
      }
      return finalNumbers.Top();
  }
  
  
  int higherPrecedenceValidate(char operator1, char operator2)
  {
      int op1 = getPrecedence(operator1);
      int op2 = getPrecedence(operator2);
      if(op1 == op2)
          return true;
      return op1 &gt; op2 ?  true: false;
  }
  
  int getPrecedence(char op)
  {
      int weight = 0;
      switch(op)
      {
      case '+':
      case '-':
          weight = 1;
          break;
      case '*':
      case '/':
          weight = 2;
          break;
      }
      return weight;
  }
  </code></pre>
  
  <blockquote>
    <p>Now I solved this problem. Here is my wrong infix expression checking code with infix to postfix and postfix to result evalution :</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "stacktype.cpp"
  using namespace std;
  
  string infixToPostFix(string infix);
  int higherPrecedenceValidate(char op1, char op2);
  int getPrecedence(char op);
  int evaluatePostFix(string postfix);
  
  int main()
  {
      string infix,postfix;
      int result;
      cout &lt;&lt; "Infix: ";
      cin &gt;&gt; infix;
      postfix = infixToPostFix(infix);
      cout &lt;&lt; "\nPostfix: " &lt;&lt; postfix &lt;&lt; endl &lt;&lt; endl;
      if(postfix != "Wrong Expression"){
          result = evaluatePostFix(postfix);
          cout &lt;&lt; "Result: " &lt;&lt; result &lt;&lt; endl &lt;&lt; endl; 
      }
  
  
  }
  
  
  string infixToPostFix(string infix){
      StackType&lt;char&gt; operators;
      bool isMathOperatorRepeated = false;
      bool isOperaendRepeated = false;
      string postfix;
      for(int i = 0; i &lt; infix.size(); i++){
          // Checking Operator
          if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/'){
              if(isMathOperatorRepeated){
                  postfix = "Wrong Expression";
  
                  /* 
                  After this for loop there is while loop
                  which is checking rest of the char and add it with postfix string .
                  So this pushed char should be pop out 
                  beacuse infix expression is wrong.
                  */
  
                  while (!operators.IsEmpty())
                  {
                      operators.Pop();
                  }
                  break;
              }
              while (!operators.IsEmpty() &amp;&amp; higherPrecedenceValidate(operators.Top(),infix[i]))
              {
                  postfix = postfix + operators.Top();
                  operators.Pop();
              }
              operators.Push(infix[i]);
              isMathOperatorRepeated = true;
              isOperaendRepeated = false;
  
          }
          // Checking Operand
          else if(infix[i] &gt;= '0' &amp;&amp; infix[i] &lt;= '9')
          {
              if(isOperaendRepeated){
                  postfix = "Wrong Expression";
  
                  /* 
                  After this for loop there is while loop
                  which is checking rest of the char and add it with postfix string .
                  So this pushed char should be pop out 
                  beacuse infix expression is wrong.
                  */
  
                  while (!operators.IsEmpty())
                  {
                      operators.Pop();
                  }
                  break;
              }
              postfix = postfix + infix[i];
              isMathOperatorRepeated = false;
              isOperaendRepeated = true;
  
          }
          //Checking open bracket
          else if(infix[i] == '(' ){
              operators.Push(infix[i]);
              isMathOperatorRepeated = false;
              isOperaendRepeated = false;
          }
  
          //Checking closing bracket
          else if(infix[i] == ')' ){
  
              while (!operators.IsEmpty() &amp;&amp; operators.Top() != '(')
              {
                  postfix = postfix + operators.Top();
                  operators.Pop();
              }
  
              /*
              checking stack beacuse we know 
              that if the infix char is ')'  
              and the stack is empty then the infix expression is wrong
  
              */
              if(operators.IsEmpty()){
                  postfix = "Wrong Expression";
                  break;
  
              }
              else{
                  operators.Pop();
              }
              // poping the opening bracket
              isMathOperatorRepeated = false;
              isOperaendRepeated = false;
          }
  
          // checking that infix expression has invalid char
          else{
              postfix = "Wrong Expression";
  
              /* 
                  After this for loop there is while loop
                  which is checking rest of the char in stack.
                  So this pushed char should be pop out 
                  beacuse infix expression is wrong.
              */
              while (!operators.IsEmpty())
              {
                  operators.Pop();
              }
              break;
          }
  
  
      }
      // poping rest of element from the stack..
      while (!operators.IsEmpty())
      {
          if(operators.Top() == '('){
              postfix = "Wrong Expression";
              break;
          }
          else{
              postfix = postfix + operators.Top();
              operators.Pop();
          }
      }
      return postfix;
  }
  
  int evaluatePostFix(string postfix){
      StackType&lt;int&gt; finalNumbers;
      for(int i = 0; i &lt; postfix.size(); i++){
          // Checking Operator
          if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/'){
              int resultOfTwoNumber;
              int number2 = finalNumbers.Top();
              finalNumbers.Pop();
              int number1 = finalNumbers.Top();
              finalNumbers.Pop();
              switch (postfix[i])
              {
                  case '+':
                      resultOfTwoNumber = number1 + number2;
                      break;
                  case '-':
                      resultOfTwoNumber = number1 - number2;
                      break;
                  case '*':
                      resultOfTwoNumber = number1 * number2;
                      break;
                  case '/':
                      resultOfTwoNumber = number1 / number2;
                      break;
              }
              finalNumbers.Push(resultOfTwoNumber);
  
          }
          // Checking Operand
          else if(postfix[i] &gt;= '0' &amp;&amp; postfix[i] &lt;= '9')
          {
              finalNumbers.Push(postfix[i] - '0');
  
          }
      }
      return finalNumbers.Top();
  }
  
  
  int higherPrecedenceValidate(char operator1, char operator2)
  {
      int op1 = getPrecedence(operator1);
      int op2 = getPrecedence(operator2);
      if(op1 == op2)
          return true;
      return op1 &gt; op2 ?  true: false;
  }
  
  int getPrecedence(char op)
  {
      int weight = 0;
      switch(op)
      {
      case '+':
      case '-':
          weight = 1;
          break;
      case '*':
      case '/':
          weight = 2;
          break;
      }
      return weight;
  }
  </code></pre>