示例#1
0
        static void Main(string[] args)
        {
            var easyStack = new EasyStack <int>();

            easyStack.Push(1);
            easyStack.Push(2);
            easyStack.Push(3);

            var item1 = easyStack.Pop();
            var item2 = easyStack.Peek();

            Console.WriteLine(item1);
            Console.WriteLine(item2);

            Console.ReadLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);
            //linkedStack.Push(20);
            //linkedStack.Push(30);
            //linkedStack.Push(40);
            //linkedStack.Push(50);

            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());

            Console.ReadLine();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(100);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);
            arrayStack.Push(500);
            //arrayStack.Push(600);

            Console.WriteLine(arrayStack.Count);
            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Count);
            Console.WriteLine(arrayStack.MaxCount);
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.MaxCount);
            Console.WriteLine(arrayStack.Count);
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.MaxCount);
            Console.WriteLine(arrayStack.Count);
            Console.WriteLine(arrayStack.Peek());

            Console.ReadLine();
        }
示例#2
0
        public int Result()
        {
            int result = 0;
            int lastop = 0;

            try
            {
                char[] c = expression.ToCharArray();
                for (int i = 0; i < c.Length; i++)
                {
                    bool b = int.TryParse(c[i].ToString(), out int m);
                    if (b)
                    {
                        number.Push(m);
                    }
                    else
                    {
                        if (operation.IsEmpty())
                        {
                            operation.Push(c[i].ToString());
                            lastop = dict[c[i].ToString()];
                        }
                        else if (lastop > dict[c[i].ToString()])
                        {
                            //下面代码有问题不能连乘
                            int    x  = number.Pop();
                            int    y  = number.Pop();
                            string op = operation.Pop();
                            int    r  = calc(x, y, op);
                            number.Push(r);
                            lastop = dict[c[i].ToString()];
                            operation.Push(c[i].ToString());
                        }
                        else
                        {
                            operation.Push(c[i].ToString());
                            lastop = dict[c[i].ToString()];
                        }
                    }
                }

                while (!operation.IsEmpty())
                {
                    int    x  = number.Pop();
                    int    y  = number.Pop();
                    string op = operation.Pop();
                    int    r  = calc(x, y, op);
                    number.Push(r);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            result = number.Pop();
            Console.WriteLine(result);
            return(result);
        }
示例#3
0
文件: Program.cs 项目: NazarSqa/Stack
        static void Main(string[] args)
        {
            var easyStack = new EasyStack <int>();

            easyStack.Push(1);
            easyStack.Push(2);
            easyStack.Push(3);
            easyStack.Push(4);
            easyStack.Push(5);
            Console.WriteLine("кількість елементів до операцій = " + easyStack.Count);
            var item  = easyStack.Pop();
            var item2 = easyStack.Pop();
            var item3 = easyStack.Peek();

            Console.WriteLine("кількість елементів після операцій = " + easyStack.Count);

            Console.WriteLine(item);
            Console.WriteLine(item2);
            Console.WriteLine(item3);
            Console.ReadLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);


            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());
            Console.ReadLine();


            var arrayStack = new ArrayStack <int>(5);

            Console.WriteLine(arrayStack.Count);
            arrayStack.Push(100);
            Console.WriteLine(arrayStack.Count);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);
            arrayStack.Push(500);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());

            Console.ReadLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            ArrayStack <int> a = new ArrayStack <int>();

            a.Push(1);
            a.Push(2);
            System.Console.WriteLine(a.Pop());
            System.Console.WriteLine(a.Pop());
            System.Console.WriteLine(a.Pop());

            //System.Console.WriteLine(String.Join(", ", a.ToArray()));
        }
示例#5
0
        /// <summary>
        /// 栈(数组)测试
        /// </summary>
        static void ArrayStackTest()
        {
            ArrayStack <int> stack = new ArrayStack <int>(10);

            Console.WriteLine("01初始化堆栈,当前为空栈");
            Console.WriteLine("IsEmpty:{0}", stack.IsEmpty());

            Random rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                stack.Push(rand.Next(1, 10));//随机生成1—10压栈
            }
            Console.WriteLine("02随机压入1-10内10个数据");
            Console.WriteLine("IsEmpty:{0}", stack.IsEmpty());
            Console.WriteLine("Size:{0}", stack.Size);

            Console.WriteLine("03弹出栈内10个元素,依次打印他们的值");
            for (int i = 0; i < 10; i++)
            {
                int node = stack.Pop();
                Console.Write(node + " ");//依次打印弹出元素,用“ ”隔开
            }
            Console.WriteLine();
            Console.WriteLine("IsEmpty:{0}", stack.IsEmpty());
            Console.WriteLine("Size:{0}", stack.Size);
        }
示例#6
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Stack collections *****");

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(10);
            arrayStack.Push(20);
            arrayStack.Push(30);
            arrayStack.Push(40);
            arrayStack.Push(50);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());
        }
示例#7
0
        static void Main(string[] args)
        {
            SimpleStack <int> myStack = new SimpleStack <int>();

            myStack.Push(1);
            myStack.Push(2);
            myStack.Push(3);

            var item       = myStack.Pop();
            var secondItem = myStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(secondItem);
            Console.ReadLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);
            linkedStack.Push(40);

            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());
            Console.ReadLine();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(100);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);
            arrayStack.Push(500);

            System.Console.WriteLine(arrayStack.Peek());
            System.Console.WriteLine(arrayStack.Pop());
            System.Console.WriteLine(arrayStack.Pop());
            System.Console.WriteLine(arrayStack.Peek());

            Console.ReadLine();
        }
示例#8
0
        static void Main(string[] args)
        {
            Model.EasyStack <int> easyStack = new Model.EasyStack <int>();

            easyStack.Push(1);
            easyStack.Push(2);
            easyStack.Push(3);

            var item  = easyStack.Pop();
            var item2 = easyStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(item2);
            Console.WriteLine();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);
            linkedStack.Push(40);
            linkedStack.Push(50);

            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(100);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());
            Console.ReadLine();
        }
        private static void Main()
        {
            ArrayStack <int> array = new ArrayStack <int>(5);

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");

            array.Push(100);
            array.Push(200);
            array.Push(300);
            array.Push(400);
            array.Push(500);

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");

            Console.WriteLine(array.Peek());

            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");
        }
示例#10
0
        static void Main(string[] args)
        {
            ArrayStack <int> s = new ArrayStack <int>(10);

            s.Push(1);
            s.Push(2);
            s.Push(3);
            s.Push(4);
            s.Push(5);
            s.Pop();
            s.Push(6);
            s.Push(7);
            //s.ToString();

            ExpressionCalculation expression = new ExpressionCalculation("5-1-2");

            expression.Result();
            Console.ReadLine();
        }
示例#11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var stack = new ArrayStack <int>();

            stack.Print();
            stack.Push(1);
            stack.Print();
            stack.Push(2);
            stack.Print();
            stack.Push(3);
            stack.Print();
            var res = stack.Pop();

            Console.WriteLine("Pop:" + res);
            stack.Print();
            DecToBinary(25);
            Console.ReadLine();
        }
示例#12
0
        /// <summary>
        /// 十进制转二进制
        /// </summary>
        static void DecToBinary(int num)
        {
            var n       = num;
            var stack   = new ArrayStack <int>();
            var residue = 0;

            while (num != 0)
            {
                residue = num % 2;
                stack.Push(residue);
                num = num / 2;
            }
            stack.Print();
            var result = "Binary:";

            while (stack.Size > 0)
            {
                result += stack.Pop();
            }
            Console.WriteLine("Dec:" + n + "->" + result);
        }
示例#13
0
        static void Main(string[] args)
        {
            var easyStack = new EasyStack <int>();

            easyStack.Push(1);
            easyStack.Push(4);
            easyStack.Push(2);
            easyStack.Push(6);

            var item = easyStack.Pop();

            var item2 = easyStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(item2);

            Console.WriteLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(101);
            arrayStack.Push(10);
            arrayStack.Push(33);
            arrayStack.Push(20);
            arrayStack.Push(15);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());

            Console.WriteLine(arrayStack.ToString());


            Console.ReadLine();
        }
        internal void Run()
        {
            ArrayStack numStack  = new ArrayStack(10);
            ArrayStack operStack = new ArrayStack(10);

            int    index = 0;//指针
            int    num1, num2, oper, res;
            char   ch;
            string keepNum = "";

            while (true)
            {
                //扫描每一个字符
                ch = expression.Substring(index, 1).ToCharArray()[0];
                if (IsOper(ch))
                {
                    //判断当前符号栈是否为空
                    if (!operStack.IsEmpty())
                    {
                        if (Priority(ch) <= Priority(operStack.Peek()))
                        {
                            //从数栈中pop两个数,从符号栈中pop出一个符号,计算;
                            //将计算结果放入数栈,将当前符号栈如栈
                            num1 = numStack.Pop();
                            num2 = numStack.Pop();
                            oper = operStack.Pop();
                            res  = Calc(num1, num2, oper);
                            numStack.Push(res);
                            operStack.Push(ch);
                        }
                        else
                        {
                            //入栈
                            operStack.Push(ch);
                        }
                    }
                    else
                    {
                        //为空直接入栈
                        operStack.Push(ch);
                    }
                }
                else
                {
                    //如果是数字,直接入栈
                    //numStack.Push(ch-48);
                    //如果是多位数的话,不能直接入栈

                    keepNum += ch;//先不着急入栈,先存一下
                    if (index == expression.Length - 1)
                    {
                        //如果ch已经是最后一个了,就不要往后看了
                        numStack.Push(ch - 48);
                    }
                    else
                    {
                        //继续扫描,往后看一位,直到后面的是运算符,拼接后入栈
                        if (IsOper(expression.Substring(index + 1, 1).ToCharArray()[0]))
                        {
                            //如果后一位是运算符,入栈
                            numStack.Push(Convert.ToInt32(keepNum));
                            keepNum = "";
                        }
                    }
                }
                index++;
                if (index >= expression.Length)
                {
                    break;
                }
            }

            //扫描完后,依次弹出计算
            while (true)
            {
                //如果符号栈为空,数栈中的就是结果
                if (operStack.IsEmpty())
                {
                    break;
                }
                num1 = numStack.Pop();
                num2 = numStack.Pop();
                oper = operStack.Pop();
                res  = Calc(num1, num2, oper);
                numStack.Push(res);
            }

            res = numStack.Pop();

            Console.WriteLine("计算结果为" + res.ToString());
        }