Esempio n. 1
0
        /// <summary>
        /// Push new node onto the top of the stack
        /// </summary>
        /// <param name="value"></param>
        public void push(T value)
        {
            var newNode = new StackNode(value);

            Size++;
            //if this is the first node in stack, it is also the top
            if (Size == 1)
            {
                top = newNode;
                return;
            }
            //else bottom is already established, only change top
            newNode.Next = top;
            top          = newNode;
        }
Esempio n. 2
0
        public double TinhBieuThuc(string posfix)
        {
            string[] tt = posfix.Split(' ');
            var      kq = new StackNode <string>();

            for (int i = 0; i < tt.Length - 1; i++)
            {
                if (!LaToanTu(tt[i][0]))
                {
                    kq.Push(tt[i]);
                }
                else
                {
                    string a = kq.Top();
                    kq.Pop();
                    string b = kq.Top();
                    kq.Pop();
                    kq.Push(TinhToan(tt[i], b, a));
                }
            }
            double ketqua = Convert.ToDouble(kq.Top());

            return(ketqua);
        }
Esempio n. 3
0
 public StackNode(T t)
 {
     next = null;
     data = t;
 }
Esempio n. 4
0
        public string InfixtoPosfix(string nhap)
        {
            var    a           = new StackNode <string>();
            var    ketQuahauto = "";
            string tk;

            nhap = nhap.Trim(); //Cắt hết khoảng trắng ở đầu và cuối chuỗi
            var s = "";

            for (int i = 0; i < nhap.Length; i++)
            {
                if (nhap[i] != ' ')
                {
                    s += nhap[i];
                }
            }

            var demMoNgoac   = 0;
            var demDongNgoac = 0;
            var demToanTu    = 0;
            var demToanHang  = 0;

            for (int i = 0; i < s.Length;)
            {
                GetTokken(s, out tk, ref i);
                if (tk[0] == '(')
                {
                    demMoNgoac++;
                }
                else if (tk[0] == ')')
                {
                    demDongNgoac++;
                }
                else if (LaToanTu(tk[0]))
                {
                    demToanTu++;
                }
                else
                {
                    demToanHang++;
                }
            }
            if (demDongNgoac == demMoNgoac && demToanHang - 1 == demToanTu)
            {
                for (int i = 0; i < s.Length;)
                {
                    GetTokken(s, out tk, ref i);
                    if (!LaToanTu(tk[0]))
                    {
                        ketQuahauto += tk + " ";
                    }
                    else
                    {
                        if (tk[0] == '(')
                        {
                            a.Push(tk);
                        }
                        else if (tk[0] == ')')
                        {
                            while (a.Top()[0] != '(')
                            {
                                ketQuahauto += a.Top() + " ";
                                a.Pop();
                            }
                            if (a.Top()[0] == '(')
                            {
                                a.Pop();
                            }
                        }
                        else
                        {
                            if (!a.IsEmpty() && DoUuTien(a.Top()[0]) < DoUuTien(tk[0]))
                            {
                                a.Push(tk);
                                continue;
                            }
                            else if (a.IsEmpty())
                            {
                                a.Push(tk);
                                continue;
                            }
                            while (!a.IsEmpty() && DoUuTien(a.Top()[0]) >= DoUuTien(tk[0]))
                            {
                                ketQuahauto += a.Top() + " ";
                                a.Pop();
                            }
                            a.Push(tk);
                        }
                    }
                }
                while (!a.IsEmpty())
                {
                    if (a.Top()[0] == '(')
                    {
                        a.Pop();
                        continue;
                    }
                    ketQuahauto += a.Top() + " ";
                    a.Pop();
                }
                return(ketQuahauto);
            }
            else
            {
                return("\nBieu thuc sai");
            }
        }