Пример #1
0
        public double GetValue()
        {
            stack = new MyStack <double>();
            string[] Arr = Str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in Arr)
            {
                if (double.TryParse(item, out double result))
                {
                    stack.Push(result);
                }
                else
                {
                    switch (item)
                    {
                    case "+":
                        stack.Push(stack.Pop() + stack.Pop());
                        break;

                    case "*":
                        stack.Push(stack.Pop() * stack.Pop());
                        break;

                    case "-":
                        stack.Push(0 - stack.Pop() + stack.Pop());
                        break;

                    case "/":
                        stack.Push(1 / stack.Pop() * stack.Pop());
                        break;

                    default:
                        throw new Exception();
                    }
                }
            }
            if (stack.Count == 1)
            {
                return(stack.Pop());
            }
            throw new Exception();
        }
Пример #2
0
        public bool Check()
        {
            stack = new MyStack <char>();
            for (int i = 0; i < Str.Length; i++)
            {
                switch (Str[i])
                {
                case '(':
                case '{':
                case '[':
                    stack.Push(Str[i]);
                    break;

                case ')':
                    if (stack.Count == 0 || stack.Pop() != '(')
                    {
                        return(false);
                    }
                    break;

                case '}':
                    if (stack.Count == 0 || stack.Pop() != '{')
                    {
                        return(false);
                    }
                    break;

                case ']':
                    if (stack.Count == 0 || stack.Pop() != '[')
                    {
                        return(false);
                    }
                    break;
                }
            }
            if (stack.Count != 0)
            {
                return(false);
            }
            return(true);
        }