I am having touble testing the throw command after the if (command[0] == '(')). The if statment I think means that if the first char in command does not equel ) throw an error. I have tried a several statments without a ) but have still not been able to execute the throw command. Any ideas.
private double ParseTerm(ref string command) { double returnValue=0; if (command.Length != 0) { if (command[0] == '(')) { command = command.Substring(1,command.Length -1); // skip the open paren returnValue= ParseExpr(ref command); if (command[0] != ')') // make sure there is a close paren for each open parenthesis throw new System.FormatException(); command = command.Substring(1,command.Length -1); // skip the close paren } return returnValue; } Here is ParseExpr
private double ParseExpr(ref string command) { double op, op2; if (command == "") // Handle the empty expression case return 0; op = ParseFactor(ref command); // parse left side of expression if (command != "") // if a right side exists, parse it { if (command[0] == '+') // test for '+' { command = command.Substring(1,command.Length -1); // skip to + if (command.Length == 0) throw new System.FormatException(); // no right hand side operator op2 = ParseExpr(ref command); // parse remainder of the expression op += op2; } else if (command[0] == '-') { command = command.Substring(1,command.Length -1); if (command.Length == 0) throw new System.FormatException(); op2 = ParseExpr(ref command); op -= op2; } } return op; } private double ParseFactor(ref string command) { double op, op2; op = ParseExp(ref command); if (command != "") { if (command[0] == '*') { command = command.Substring(1,command.Length -1); if (command.Length == 0) throw new System.FormatException(); op2 = ParseFactor(ref command); op *= op2; } else if (command[0] == '/' || command[0] == '\\') { command = command.Substring(1,command.Length -1); if (command.Length == 0) throw new System.FormatException(); op2 = ParseFactor(ref command); if (op2 == 0) // don't allow divide 0 throw new System.DivideByZeroException(); // the division operation won't return op /= op2; // throw the exception since we are using doubles } else if (command[0] == '%') { command = command.Substring(1,command.Length -1); if (command.Length == 0) throw new System.FormatException(); op2 = ParseFactor(ref command); op = (int)op % (int)op2; } } return op; }
ParseExpr()does...ParseFactor()modifycommand?