Esempio n. 1
0
        /// <summary>
        /// 计算结果
        /// </summary>
        /// <returns></returns>
        public float GetResult()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            float             result = 0;
            string            a = "";
            float             top, topnext;
            LinkStack <float> numbers = new LinkStack <float>();

            foreach (char c in suffix)
            {
                if (char.IsDigit(c) || c == '.')
                {
                    a += c;
                }
                else if (c == ' ' && a != "")
                {
                    numbers.Push(float.Parse(a));
                    Console.WriteLine("传入了数" + a);
                    a = "";
                }
                if (c == '+')
                {
                    top     = numbers.Pop();
                    topnext = numbers.Pop();
                    float temp = topnext + top;
                    Console.WriteLine("加法运算结果 " + temp);
                    numbers.Push(temp);
                }
                else if (c == '-')
                {
                    top     = numbers.Pop();
                    topnext = numbers.Pop();
                    float temp = topnext - top;
                    Console.WriteLine("减法运算结果 " + temp);
                    numbers.Push(temp);
                }
                else if (c == '*')
                {
                    top     = numbers.Pop();
                    topnext = numbers.Pop();
                    float temp = topnext * top;
                    Console.WriteLine("乘法运算结果 " + temp);
                    numbers.Push(temp);
                }
                else if (c == '/')
                {
                    top     = numbers.Pop();
                    topnext = numbers.Pop();
                    float temp = topnext / top;
                    Console.WriteLine("除法运算结果 " + temp);
                    numbers.Push(temp);
                }
            }
            result = numbers.Pop();
            numbers.Clear();
            Console.ForegroundColor = ConsoleColor.White;
            return((float)Math.Round(result, 4));
        }
Esempio n. 2
0
        /// <summary>
        /// 数学符号判断与检测
        /// </summary>
        /// <param name="item"></param>
        private void AddSign(String item)
        {
            item = item == "÷" ? "/" : item; //统一规定为 /  符号
            string top     = item;
            string topnext = string.Empty;

            if (buffer.IsEmpty())
            {
                buffer.Push(item);
                return;
            }
            else
            {
                topnext = buffer.GetByID(1);
            }
            if (top == "=")
            {
                return;
            }
            if (top == ")")
            {
                Console.WriteLine("触发事件!遇到 ) 符号");  // 测试数据   (111+222+333)+12345
                string sign = buffer.Pop();
                while (sign != "(")
                {
                    suffix += sign + " ";
                    sign    = buffer.Pop();
                }
                return;
            }
            if (Regex.IsMatch(top, @"[+-]") && Regex.IsMatch(topnext, @"[/*]"))
            {
                Console.WriteLine("触发事件!top " + top + "  topnext " + topnext);
                while (!buffer.IsEmpty())
                {
                    string sign = buffer.Pop();
                    suffix += sign + " ";
                }
                buffer.Push(item);
            }
            else
            {
                buffer.Push(item);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            //Stack<char> stack = new Stack<char>();

            //IStackDS<char> stack = new SeqStack<char>(30);

            IStackDS <char> stack = new LinkStack <char>();

            stack.Push('a');

            stack.Push('b');

            stack.Push('c');

            Console.WriteLine("push a b c 之后的数据个数为:" + stack.Count);

            char temp = stack.Pop();

            Console.WriteLine("pop之后得到的数据是:" + temp);

            Console.WriteLine("pop之后,栈中数据的个数是:" + stack.Count);

            char temp2 = stack.Peek();

            Console.WriteLine("peek之后得到的数据是:" + temp2);

            Console.WriteLine("peek之后,栈中数据的个数是:" + stack.Count);



            stack.Clear();

            Console.WriteLine("clear之后栈中数据的个数:" + stack.Count);

            Console.ReadKey();
        }