Ejemplo n.º 1
0
        public string MiddleToPost()  //中缀表达式转换为后缀表达式
        {
            char[]         aa   = middleExpression.ToCharArray();
            int            leng = aa.Length + 1;
            MyStack <char> save = new MyStack <char>(leng);

            save.Push('#');
            char[] showCase = new char[aa.Length];
            int    showNum  = 0;

            for (int i = 0; i < aa.Length; i++)
            {
                if (CharJuddging(aa[i]) == "OPD")
                {
                    showCase[showNum] = aa[i];
                    showNum++;
                }
                else if (aa[i] != '(' && aa[i] != ')')
                {
                    if (CharComparing(aa[i], save.GetTop()) == 1 || save.GetTop() == '(')
                    {
                        save.Push(aa[i]);
                    }
                    else
                    {
                        while (CharComparing(aa[i], save.GetTop()) < 1)
                        {
                            showCase[showNum] = save.Pop();
                            showNum++;
                        }
                        save.Push(aa[i]);
                    }
                }
                else
                {
                    if (aa[i] == '(')
                    {
                        save.Push(aa[i]);
                    }
                    else
                    {
                        while (save.GetTop() != '(')
                        {
                            showCase[showNum] = save.Pop();
                            showNum++;
                        }
                        save.Pop();
                    }
                }
            }
            while (save.GetLength() > 1)
            {
                showCase[showNum] = save.Pop();
                showNum++;
            }
            string tt = new string(showCase);

            return(tt);
        }
Ejemplo n.º 2
0
        static bool IsSymmetry(char[] xx)  //判断是否为中心对称串的函数
        {
            int            leng  = xx.Length + 1;
            MyStack <char> exam1 = new MyStack <char>(leng);
            int            a     = xx.Length % 2;

            if (a == 0)
            {
                for (int i = 0; i < xx.Length; i++)
                {
                    if (i == 0 || (i > 0 && xx[i] != exam1.GetTop()))
                    {
                        exam1.Push(xx[i]);
                    }
                    else
                    {
                        exam1.Pop();
                    }
                }
            }
            else
            {
                for (int i = 0; i < xx.Length; i++)
                {
                    if (i == (xx.Length - 1) / 2)
                    {
                        continue;
                    }
                    else if (i == 0 || (i > 0 && xx[i] != exam1.GetTop()))
                    {
                        exam1.Push(xx[i]);
                    }
                    else
                    {
                        exam1.Pop();
                    }
                }
            }
            if (exam1.IsEmpty())
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }