不知道如何在 C# 中测试一行代码
我在 if (command[0] == '(')) 之后测试 throw 命令时遇到问题。我认为 if 语句意味着如果命令中的第一个字符不等于 ) 会抛出错误。我已经尝试了几个没有 ) 的语句,但仍然无法执行 throw 命令。任何想法。
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;
}
这是 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的意思是单元测试?如果是:
You mean a UnitTest? If yes: