如何使用堆栈检测 } 括号错误?

发布于 2024-12-08 22:03:10 字数 2584 浏览 0 评论 0原文

我的代码可以检测到 { 括号错误,如果文件中只有 } 括号,那么它将抛出空堆栈异常。我想知道如何检测两个括号错误,如果 { this 或 } 这个括号丢失,那么它将显示错误。 类 StackCS {

    #region Vaiables
    Stack stack;
    char[] charArray;
    string[] stringArray;
    int linenumber;

    string chError = string.Empty;
    public string ChError { get { return chError +" "+ linenumber; } set { chError = value; } }

    public StackCS()
    {
        charArray = new char[1000];
        stack = new Stack();
    }

    #endregion 

    #region File

    private void ReadCharFromFile(string add)
    {
        using (StreamReader rdr = new StreamReader(add))
        {
            for (int i = 0; i < charArray.Length; i++)
            {
                charArray[i] = (char)rdr.Read();
            }
        }
    }

    public void ReadFile(string add)
    {
        ReadCharFromFile(add);
        stringArray = File.ReadAllLines(add);
    }

    #endregion

    #region Stack

    public void FillingStack()
    {
        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == '{')
            {
                stack.Push('{');
            }
            else if (charArray[i] == '}')
                stack.Pop();
        }
    }

    public void CheckingStack()
    {
        for (int i = 0; i < stringArray.Length; i++)
        {
            if (stack.Count != 0)
            {
                chError = "Error At:";
                linenumber = i;
            }
            else
            {
                chError = "No Error";
            }
        }
    }

    public override string ToString()
    {
        string temp = string.Empty;
        for (int i = 0; i < stringArray.Length; i++)
        {
            temp +=i+"\t\t"+stringArray[i] + "\n";
        }
        return temp;
    }

    #endregion

}

        static void Main(string[] args)
    {
        StackCS obj = new StackCS();
        try
        {
            // set the file location, according to ur PC setting

            obj.ReadFile(@"C:\Users\5609\Desktop\Class1.cs");
            obj.FillingStack();
            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content"+"\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);

            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content" + "\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
    }

My code can detect { bracket error, if in file there is only } bracket then it will throw empty stack exception. I want to know how can I detect both bracket errors, if { this or } this bracket missing then it will show error.
class StackCS
{

    #region Vaiables
    Stack stack;
    char[] charArray;
    string[] stringArray;
    int linenumber;

    string chError = string.Empty;
    public string ChError { get { return chError +" "+ linenumber; } set { chError = value; } }

    public StackCS()
    {
        charArray = new char[1000];
        stack = new Stack();
    }

    #endregion 

    #region File

    private void ReadCharFromFile(string add)
    {
        using (StreamReader rdr = new StreamReader(add))
        {
            for (int i = 0; i < charArray.Length; i++)
            {
                charArray[i] = (char)rdr.Read();
            }
        }
    }

    public void ReadFile(string add)
    {
        ReadCharFromFile(add);
        stringArray = File.ReadAllLines(add);
    }

    #endregion

    #region Stack

    public void FillingStack()
    {
        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == '{')
            {
                stack.Push('{');
            }
            else if (charArray[i] == '}')
                stack.Pop();
        }
    }

    public void CheckingStack()
    {
        for (int i = 0; i < stringArray.Length; i++)
        {
            if (stack.Count != 0)
            {
                chError = "Error At:";
                linenumber = i;
            }
            else
            {
                chError = "No Error";
            }
        }
    }

    public override string ToString()
    {
        string temp = string.Empty;
        for (int i = 0; i < stringArray.Length; i++)
        {
            temp +=i+"\t\t"+stringArray[i] + "\n";
        }
        return temp;
    }

    #endregion

}

        static void Main(string[] args)
    {
        StackCS obj = new StackCS();
        try
        {
            // set the file location, according to ur PC setting

            obj.ReadFile(@"C:\Users\5609\Desktop\Class1.cs");
            obj.FillingStack();
            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content"+"\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);

            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content" + "\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

草莓酥 2024-12-15 22:03:10

如果您没有最后的右大括号,那么堆栈上会留下一些内容,如果堆栈中有任何元素,最后只需返回即可。如果不这样做,则表达式无效。

另一种情况是您有一个没有正式表达式的右大括号,在这种情况下您会遇到 } 但堆栈为空(错误)。

因此,一旦检查了堆栈,您就可以确保 { } 中包含的所有表达式都是正确的,而不是堆栈上没有未检查的元素,因此 stack.Empty() 为 true 则表达式有效。

If you don't have a final closing brace then something will be left on the stack, at the end simply return if you have any elements in the stack. If you dont the expression is invalid.

The other case is that you have a closing brace without a formal expression in which case you encounter } but the stack is empty (error).

So once you've checked the stack you have made sure that all expressions contained in { } are proper than that no elements were left unchecked on the stack so stack.Empty() is true then the expression is valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文