# Equation Verifier! Almost complete!

• 11-30-2002
Nakeerb
Equation Verifier! Almost complete!
Code:

``` void checkSymbols(string infix){         string acceptedSymbols = "0123456789+-*/^()",               acceptedNumbers = "0123456789",                   acceptedOperators = "+-*/^";         int numberCount = 0, opCount = 0, leftParCount = 0, rightParCount = 0;         for(unsigned int i = 0; i < infix.length(); i++){                         if (acceptedSymbols.find(infix[i], 0) == string::npos)                         outputPostfixError(1);                                         if (acceptedNumbers.find(infix[i], 0) != string::npos)                         numberCount++;                                         if (acceptedOperators.find(infix[i], 0) != string::npos)                         opCount++;                                         if (infix[i] == '(')                         leftParCount++;                                         if (infix[i] == ')')                         rightParCount++;         }         if ((numberCount - 1) != opCount)                 outputPostfixError(2);                         if (leftParCount != rightParCount)                 outputPostfixError(3);         } //end void checkSymbols```
This is a code to verify if a given equation is correct. Like if I enter "3 + 4 - (6 * 5) / 2" it is correct, but if I do "a + 5" or "a ++ 3)" or "((1 + 2)" they will all give errors, for it must be a solvable equation with no unknown variables.

However, I am pretty sure all these errors have been fixed, but I do not know how to check for incorrect parenthesis direction. Like if the user enters ")1 + 2( - 2" or something to that extent. Parenthesis can be nested so this makes it a bit harder, like this would be accepted: ((1 + 5) / 3)

But of course, I don't want ")(1 + 5( / 3)"

Any way I can solve this?
• 11-30-2002
Lazy Student
I had an assignment similar to this! :D

You can use stack to solve this problem:
PHP Code:

``` for (every character in the infix) { case '(': push to the stack case ')':   if (stack is not empty)      pop the stack   else    return 0; default:  // do nothing } if (stack is not empty)  return 0;  ```
• 11-30-2002
Magos
Even simplier, use an integer value (that starts at 0). Traverse the expression. For every ( add 1 to the variable, and for every ) subtract 1.
If the variable ever go below 0 or if it isn't 0 at the end, there is something wrong.
• 11-30-2002
Nakeerb
```void checkSymbols(string infix){         string acceptedSymbols = "0123456789+-*/^()",               acceptedNumbers = "0123456789",                   acceptedOperators = "+-*/^";         int numberCount = 0, opCount = 0;         int parenthesisChecker = 0;         for(unsigned int i = 0; i < infix.length(); i++){                         if (acceptedSymbols.find(infix[i], 0) == string::npos)                         outputPostfixError(1);                                         if (acceptedNumbers.find(infix[i], 0) != string::npos)                         numberCount++;                                         if (acceptedOperators.find(infix[i], 0) != string::npos)                         opCount++;                                         if (infix[i] == '(')                         parenthesisChecker++;                                 if (infix[i] == ')')                         parenthesisChecker--;                                                 if (parenthesisChecker < 0)                         outputPostfixError(3);                                         } //end for loop                 if (((numberCount - 1) != opCount) || (opCount == 0))                 outputPostfixError(2);                         if (parenthesisChecker != 0)                         outputPostfixError(3);         } //end void checkSymbols```