不知道如何在 C# 中测试一行代码

发布于 2024-12-11 16:12:42 字数 3620 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-12-18 16:12:42

你的意思是单元测试?如果是:

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void ParseTerm_when_the_last_char_is_not_a_close_parenthesis_should_throw_FormatException()
{
    //Call the method here:
    ParseTerm("(some string without close parenthesis");
}

You mean a UnitTest? If yes:

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void ParseTerm_when_the_last_char_is_not_a_close_parenthesis_should_throw_FormatException()
{
    //Call the method here:
    ParseTerm("(some string without close parenthesis");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文