public int Calculate(string str)
        {
            char[]        ch    = str.ToCharArray();
            int           value = 0;
            LBStack <int> st    = new LBStack <int>();

            foreach (char c in ch)
            {
                if (c == '+' || c == '-' || c == '*' || c == '/')
                {
                    int c1 = st.Pop();
                    int c2 = st.Pop();
                    switch (c)
                    {
                    case '+': value = c2 + c1; break;

                    case '-': value = c2 - c1; break;

                    case '*': value = c2 * c1; break;

                    case '/': value = c2 / c1; break;

                    default: break;
                    }
                    st.Push(value);
                }
                else
                {
                    st.Push(Convert.ToInt32(c.ToString()));
                }
            }
            return(value);
        }
        public IDictionary <int, int> GetNextGreaterElement()
        {
            LBStack <int> st = new LBStack <int>();

            foreach (int i in items)
            {
                st.Push(i);
            }
            while (!st.IsEmpty())
            {
                int item = st.Peek();
                if (st.top == 0 || st.top == items.Length - 1)
                {
                    st.Pop();
                    data.Add(item, -1);
                }
                else
                {
                    st.Pop();
                    data.Add(item, max);
                }
                max = item < max ? max : item;
            }

            return(data);
        }
Exemple #3
0
        public static bool Validate(string str)
        {
            LBStack <char> st = new LBStack <char>();

            map.Add('}', '{');
            map.Add(')', '(');
            map.Add(']', '[');

            char[] ch = str.ToCharArray();
            foreach (char c in ch)
            {
                string s = c.ToString();
                switch (s)
                {
                case "{":
                case "(":
                case "[": st.Push(c); break;

                case "}":
                case ")":
                case "]":
                    string popped2 = st.Pop().ToString();
                    if (map[c].ToString() != popped2)
                    {
                        return(false);
                    }
                    break;

                default: break;
                }
            }
            return(st.IsEmpty());
        }
        public int GetMaxWithStack()
        {
            int           i  = 0;
            LBStack <int> st = new LBStack <int>();

            while (i < items.Length)
            {
                if (items[i] > items[st.Peek()] || st.IsEmpty())
                {
                    st.Push(i);
                }
                else
                {
                    while (!st.IsEmpty() && items[i] <= items[st.Peek()])
                    {
                        calculateArea(items, st, i);
                    }
                    st.Push(i);
                }
                i++;
            }

            while (!st.IsEmpty())
            {
                calculateArea(items, st, i);
            }

            return(max);
        }
        private void calculateArea(int[] items, LBStack <int> st, int index)
        {
            int pop_index   = st.Pop();
            int lower_bound = st.IsEmpty() ? -1 : st.Peek();
            int area        = items[pop_index] * ((index - 1) - lower_bound);

            UpdateArea(area);
        }