Esempio n. 1
0
        public void StackEmptyandGetCount()
        {
            int maxSize = 100;
            var myStack = new MyStack(maxSize);

            Assert.AreEqual(0, myStack.GetCount());
        }
Esempio n. 2
0
        public void StackPushGetCount()
        {
            int maxSize = 100;
            var myStack = new MyStack(maxSize);

            myStack.Push(1);

            Assert.AreEqual(1, myStack.GetCount());
        }
Esempio n. 3
0
        public string ToPostfix(string inFix)
        {
            MyStack postFix   = new MyStack(inFix.Length);
            MyStack operators = new MyStack(inFix.Length);
            char    popUpOperator;

            foreach (char c in inFix.ToCharArray())
            {
                if (Char.IsLetterOrDigit(c))
                {
                    postFix.Push(c);
                }
                else if (c == '(')
                {
                    operators.Push(c);
                }
                else if (c == ')')
                {
                    popUpOperator = (char)operators.Pop();
                    while (popUpOperator != '(')
                    {
                        postFix.Push(popUpOperator);
                        popUpOperator = (char)operators.Pop();
                    }
                }
                else
                {
                    if (!(operators.IsEmpty()) && HigherOperatorPriority((char)operators.Peek(), c))
                    {
                        popUpOperator = (char)operators.Pop();
                        while (HigherOperatorPriority(popUpOperator, c))
                        {
                            postFix.Push(popUpOperator);

                            if (operators.IsEmpty())
                            {
                                break;
                            }

                            popUpOperator = (char)operators.Pop();
                        }
                        operators.Push(c);
                    }
                    else
                    {
                        operators.Push(c);
                    }
                }
            }
            while (operators.GetCount() > 0)
            {
                popUpOperator = (char)operators.Pop();
                postFix.Push(popUpOperator);
            }

            return(postFix.ItemToString());
        }
Esempio n. 4
0
        public void StackPush3ItemGetCount()
        {
            int maxSize = 100;
            var myStack = new MyStack(maxSize);

            myStack.Push(1);
            myStack.Push("1");
            myStack.Push("A");

            Assert.AreEqual(3, myStack.GetCount());
        }
Esempio n. 5
0
        public void StackPush3ItemAndPopGetCount()
        {
            int maxSize = 100;
            var myStack = new MyStack(maxSize);

            myStack.Push(1);
            myStack.Push("1");
            myStack.Push("A");
            var obj = myStack.Pop();

            Assert.AreEqual("A", obj);
            Assert.AreEqual(2, myStack.GetCount());
        }