开发者

How to detect } bracket error with stack? [closed]

开发者 https://www.devze.com 2023-04-12 07:30 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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" + "   开发者_C百科  " + "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);
        }
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号