Beispiel #1
0
        private bool Match(string str)
        {
            SqStackClass st = new SqStackClass();
            int          i  = 0;
            string       x  = "";

            while (i < str.Length)
            {
                if (str[i] == '(')
                {
                    st.Push("(");
                }
                else if (str[i] == ')')
                {
                    if (!st.StackEmpty())
                    {
                        st.Pop(ref x);
                    }
                    else
                    {
                        return(false);
                    }
                }
                i++;
            }
            if (st.StackEmpty())
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #2
0
        private bool Palindrome(string str)
        {
            int          i;
            string       x  = "";
            SqStackClass st = new SqStackClass();

            for (i = 0; i < str.Length; i++)
            {
                x = str[i].ToString();
                st.Push(x);
            }
            for (i = 0; i < str.Length; i++)
            {
                st.Pop(ref x);
                if (string.Compare(str[i].ToString(), x) != 0)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #3
0
        private void EnterBtn_Click(object sender, EventArgs e)
        {
            string x;
            int    k;

            x = textBox2.Text.Trim();
            if (EnterBtn.Text == "进顺序栈")
            {
                if (x == "")
                {
                    infolabel.Text = "发生错误了";
                }
                else
                {
                    if (sq.Push(x))
                    {
                        Display1();
                        infolabel.Text = "成功进栈";
                    }
                }
            }

            if (EnterBtn.Text == "进链栈")
            {
                if (x == "")
                {
                    infolabel.Text = "发生错误了";
                }
                else
                {
                    li.Push(x);
                    infolabel.Text = "成功进栈";
                    Display3();
                }
            }

            if (EnterBtn.Text == "进队")
            {
                if (x == "")
                {
                    infolabel.Text = "发生错误了";
                }
                else
                {
                    if (textBox4.Text == "")
                    {
                        if (qu.enQueue(x))
                        {
                            Display2();
                            infolabel.Text = "成功进队";
                        }
                    }
                    else
                    {
                        k = Convert.ToInt16(textBox4.Text.Trim());
                        if (enQueue(k, x))
                        {
                            Display2();
                        }
                        infolabel.Text = "成功进队";
                    }
                }
            }

            if (EnterBtn.Text == "判断配对")
            {
                string str;
                str = textBox1.Text.Trim();
                if (str == "")
                {
                    infolabel.Text = "发生错误了";
                }
                else
                {
                    if (Match(str))
                    {
                        infolabel.Text = "配对正确😀";
                    }
                    else
                    {
                        infolabel.Text = "配对失败😭";
                    }
                }
            }

            if (EnterBtn.Text == "判断回文")
            {
                string str;
                str = textBox1.Text.Trim();
                if (str == "")
                {
                    infolabel.Text = "发生错误了";
                }
                else
                {
                    if (Palindrome(str))
                    {
                        infolabel.Text = "是回文!😀";
                    }
                    else
                    {
                        infolabel.Text = "不是回文😭";
                    }
                }
            }
        }