I have an algorithm that is a piece of a mathematical equation parsing function I'm writing. It is inside the part where the current character (c or *it) has been determined to be a right-facing paranthesis ( and is supposed to increment it until it finds the closing paranthesis.
/* Iterate until the current character is the closing paranthesis or is off the end of the equation string */
std::string::const_iterator opi = it;
++it;
for (int nrfp = 1; nrfp > 0 && it != offend; c = *++it)
{
if (c == ')') --nrfp;
else if (c == '(') ++nrfp;
}
if (it == offend) throw "Unbalanced parantheses";
/* If we're here, the current character is the closing paranthesis, meaning the characters in the range (opi, it) are the inside of the paranthesis */
/* Check that the paranthesis are not empty */
if ((it - opi) == 1) throw "Empty paranthesis";
For reference, opi is supposed to mean "opening paranthesis iterator" and nrfp is supposed to mean "number of right-facing paranthesis" and offend is an iterator to the end() of the string I'm iteratring through.
How can I improve this in terms of readability, performance and modernity, without any compromise among the 3? Is there a standard library algorithm that I should be exploiting?
Aucun commentaire:
Enregistrer un commentaire