Ejemplo n.º 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());
            }
        }
Ejemplo n.º 2
0
        public void One()
        {
            StackByArray <int> sa = new StackByArray <int>();

            sa.Push(1);
            Assert.Equal(1, sa.Pop());
            Assert.True(sa.IsEmpty);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
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("错误,文件读完栈不为空");
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Example program.
 /// Test is positive with "1 2 + 2 3 - *".
 /// Test is negative with "1 + 2"
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
     Console.WriteLine("Enter your expression in postfix record");
     string expression = Console.ReadLine();
     StackByArray<int> stack = new StackByArray<int>();
     Console.WriteLine("Is your answer: {0}", Calculate(stack, expression));
 }
Ejemplo n.º 8
0
        public void None()
        {
            StackByArray <int> sa = new StackByArray <int>();

            Assert.True(sa.IsEmpty);
        }
Ejemplo n.º 9
0
 public void Intialize()
 {
     stack = new StackByArray <int>();
 }