Пример #1
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();
        }
Пример #2
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);
        }
        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());
        }