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?