Example #1
0
        public void BulkRepeat()
        {
            StackByArray <int> sa = new StackByArray <int>();

            for (int i = 0; i < 10000; i++)
            {
                sa.Push(i);
            }

            for (int j = 0; j < 1000; j++)
            {
                for (int i = 0; i < 1000; i++)
                {
                    sa.Push(i);
                }

                for (int i = 999; i >= 0; i--)
                {
                    Assert.Equal(i, sa.Pop());
                }
            }

            for (int i = 9999; i >= 0; i--)
            {
                Assert.Equal(i, sa.Pop());
            }
        }
Example #2
0
        public void Two()
        {
            StackByArray <int> sa = new StackByArray <int>();

            sa.Push(1);
            sa.Push(2);
            Assert.Equal(2, sa.Pop());
            Assert.False(sa.IsEmpty);
            Assert.Equal(1, sa.Pop());
            Assert.True(sa.IsEmpty);
        }
        /// <summary>
        /// 正式使用
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public string[] Translate(char[] array)
        {
            string outputStr = "";

            string[] outputArray = null;

            IStack <string> stack = new StackByArray <StringNode, string>();

            for (int i = 0; i < array.Length; i++)
            {
                string element = array[i].ToString();
                if (IsSymbol(element))
                {
                    var node = stack.Top();
                    try
                    {
                        while (node != null && node.Value != "(" && GetPriority(element) <= GetPriority(node.Value))
                        {
                            outputStr += stack.Pop().Value + " ";
                            node       = stack.Top();
                        }
                        if (element == ")" && node != null && node.Value == "(")
                        {
                            stack.Pop();
                            continue;
                        }
                        stack.Push(element);
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine(e.Message);
                        System.Console.WriteLine("输入数组不正确");
                        return(null);
                    }
                }
                else
                {
                    outputStr += element + " ";
                }
            }
            //文件尾把剩余符号输出
            while (stack.Top() != null)
            {
                outputStr += stack.Pop().Value + " ";
            }
            //截掉最后一个空格
            outputStr   = outputStr.Substring(0, outputStr.Length - 1);
            outputArray = outputStr.Split(" ");
            return(outputArray);
        }
Example #4
0
        public void PopTest()
        {
            Assert.IsTrue(stack.IsEmpty());
            stack.Push(3);
            stack.Push(4);
            stack.Push(5);
            Assert.IsFalse(stack.IsEmpty());

            Assert.AreEqual(3, stack.GetLength());
            Assert.AreEqual(5, stack.Pop());
            Assert.AreEqual(2, stack.GetLength());
            Assert.AreEqual(4, stack.Pop());
            Assert.AreEqual(3, stack.Pop());
            Assert.IsTrue(stack.IsEmpty());
        }
Example #5
0
        public void Five()
        {
            StackByArray <int> sa = new StackByArray <int>();

            sa.Push(1);
            sa.Push(2);
            sa.Push(3);
            sa.Push(4);
            sa.Push(5);
            Assert.Equal(5, sa.Pop());
            Assert.False(sa.IsEmpty);
            Assert.Equal(4, sa.Pop());
            Assert.False(sa.IsEmpty);
            Assert.Equal(3, sa.Pop());
            Assert.False(sa.IsEmpty);
            Assert.Equal(2, sa.Pop());
            Assert.False(sa.IsEmpty);
            Assert.Equal(1, sa.Pop());
            Assert.True(sa.IsEmpty);
        }
Example #6
0
        public string GetResult(string[] array)
        {
            IStack <string> stack = new StackByArray <StringNode, string>();

            for (int i = 0; i < array.Length; i++)
            {
                string element = array[i];
                if (!IsSymbol(element))
                {
                    stack.Push(element);
                }
                else
                {
                    var node  = stack.Pop();
                    var node2 = stack.Pop();
                    if (node == null || node2 == null)
                    {
                        // throw new Exception("栈中数字不足");
                        System.Console.WriteLine("栈中数字不足");
                        return(null);
                    }
                    string left   = node.Value;
                    string right  = node2.Value;
                    double result = Calculate(element, left, right);
                    stack.Push(result.ToString());
                }
            }
            var finalNode = stack.Pop();

            if (finalNode == null)
            {
                System.Console.WriteLine("错误,栈中无结果");
                return(null);
            }
            else
            {
                System.Console.WriteLine("result:" + finalNode.Value);
            }
            return(finalNode.Value);
        }
Example #7
0
        public void Run()
        {
            string[] testArray = null;
            // testArray =new string[] { "(", ")", "[", "{", "}" };
            // testArray = new string[] { "(", ")", "]", "{", "}"};
            testArray = new string[] { "(", ")", "[", "}" };
            string[]        openElement  = { "(", "[", "{" };
            string[]        closeElement = { ")", "]", "}" };
            IStack <string> stack        = new StackByArray <StringNode, string>();

            for (int i = 0; i < testArray.Length; i++)
            {
                string symbol = testArray[i];
                //判断是开放元素还是封闭元素
                if (openElement.Contains(symbol))
                {
                    stack.Push(symbol);
                }
                else
                {
                    if (stack.IsEmpty())
                    {
                        System.Console.WriteLine("错误,封闭元素遇上栈空");
                        break;
                    }
                    else
                    {
                        string left = stack.Pop().Value;
                        if (IsCouple(left, symbol))
                        {
                        }
                        else
                        {
                            System.Console.WriteLine("错误,开放元素对不上");
                            break;
                        }
                    }
                }
            }
            if (!stack.IsEmpty())
            {
                System.Console.WriteLine("错误,文件读完栈不为空");
            }
        }