Exemplo n.º 1
0
        public decimal result(string input)
        {
            input = correction(input);
            input = toUnarMinus(input);
            List<string> operators = new List<string>(standart_operators);
            Stack<string> stack = new Stack<string>();
            Queue<string> queue = new Queue<string>(ConvertToPostfixNotation(input));
            string str = queue.Dequeue();
            while (queue.Count >= 0)
            {
                if (!operators.Contains(str))
                {
                    try
                    {
                        stack.Push(str);
                        str = queue.Dequeue();
                    }
                    catch
                    {
                        stack.Clear();
                        stack.Push("Unparsable");
                        break;

                    }
                }
                else
                {
                    decimal summ = 0;
                    switch (str)
                    {

                        case "+":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    decimal b = Convert.ToDecimal(stack.Pop());
                                    summ = a + b;
                                    break;
                                }
                                catch
                                {
                                    return Convert.ToDecimal("asdasd");
                                    break; }
                            }
                        case "-":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    decimal b = Convert.ToDecimal(stack.Pop());
                                    summ = b - a;
                                    break;
                                }
                                catch
                                {
                                    return Convert.ToDecimal("asdasd");
                                    break; }
                            }
                        case "*":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    decimal b = Convert.ToDecimal(stack.Pop());
                                    summ = a * b;
                                    break;
                                }
                                catch
                                {
                                    return Convert.ToDecimal("asdasd");
                                    break; }
                            }
                        case "/":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    decimal b = Convert.ToDecimal(stack.Pop());

                                    summ = b / a;
                                    break;
                                }
                                catch {
                                    return Convert.ToDecimal("asdasd");
                                 break; }
                            }
                        case "^":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    decimal b = Convert.ToDecimal(stack.Pop());
                                    summ = Convert.ToDecimal(Math.Pow(Convert.ToDouble(b), Convert.ToDouble(a)));
                                    break;
                                }
                                catch
                                {
                                    return Convert.ToDecimal("asdasd");
                                    break; }

                            }
                        case "@":
                            {
                                try
                                {
                                    decimal a = Convert.ToDecimal(stack.Pop());
                                    summ = -a;
                                    break;
                                }
                                catch {
                                    break;
                                }
                            }
                    }

                    stack.Push(summ.ToString());
                    if (queue.Count > 0)
                        str = queue.Dequeue();
                    else
                        break;
                }

            }

            if (stack.ToArray().Length > 1)
            {
                string s = "sasdasd";
                decimal b = Convert.ToDecimal(s);
                return b;
            }
            else
            {
                return Convert.ToDecimal(stack.Pop());
            }
        }