Example #1
0
        /// <summary>
        /// Implementation for codex "Vyhodnocovani vyrazu"
        /// Has parametrized input and output streams for testing purposes of evaluating in int precision.
        /// </summary>
        /// <param name="input">Input stream. (e.g. Console.In)</param>
        /// <param name="output">Output stream. (e.g. Console.Out)</param>
        public static void RunInt(TextReader input, TextWriter output)
        {
            string expression = input.ReadLine();

            try {
                ExpressionParser parser  = new ExpressionParser();
                IntVisitor       visitor = new IntVisitor();
                Node             root    = parser.ParsePrefixExpression(expression);
                if (root == null)
                {
                    throw new FormatException();
                }
                output.WriteLine(root.Accept(visitor));
            }
            catch (OverflowException) {
                output.WriteLine("Overflow Error");
            }
            catch (DivideByZeroException) {
                output.WriteLine("Divide Error");
            }
            catch (FormatException) {
                output.WriteLine("Format Error");
            }
        }
Example #2
0
        /// <summary>
        /// Implementation for codex "Vyhodnocovani vyrazu II"
        /// Has parametrized input and output streams for testing purposes.
        /// Evaluates line by line for lines like:
        /// "= 'valid expression'" which will parse expression for further evaluation
        /// "i" which evaluates parsed expression in int precission
        /// "d" which evaluates parsed expression in double precission
        /// </summary>
        /// <param name="input">Input stream. (e.g. Console.In)</param>
        /// <param name="output">Output stream. (e.g. Console.Out)</param>
        public static void Run(TextReader input, TextWriter output)
        {
            var parser        = new ExpressionParser();
            var doubleVisitor = new DoubleVisitor();
            var intVisitor    = new IntVisitor();

            Node lastExpressionTree = null;
            bool intEvaluated       = false;
            bool doubleEvaluated    = false;

            string expression = input.ReadLine();

            while (expression != null && expression != "end")
            {
                if (expression.Length == 0)
                {
                    expression = input.ReadLine();
                    continue;
                }

                try {
                    switch (expression[0])
                    {
                    case '=':
                        lastExpressionTree = null;
                        intEvaluated       = false;
                        doubleEvaluated    = false;

                        if (expression.Length <= 2 || expression[1] != ' ')
                        {
                            throw new FormatException();
                        }

                        lastExpressionTree = parser.ParsePrefixExpression(expression.Substring(1));
                        if (lastExpressionTree == null)
                        {
                            throw new FormatException();
                        }

                        break;

                    case 'i':
                        if (expression.Length > 1)
                        {
                            throw new FormatException();
                        }

                        if (lastExpressionTree == null)
                        {
                            throw new NullReferenceException();
                        }

                        if (intEvaluated)
                        {
                            output.WriteLine(intVisitor.lastEvaluation);
                        }
                        else
                        {
                            output.WriteLine(lastExpressionTree.Accept(intVisitor));
                        }

                        intEvaluated = true;
                        break;

                    case 'd':
                        if (expression.Length > 1)
                        {
                            throw new FormatException();
                        }

                        if (lastExpressionTree == null)
                        {
                            throw new NullReferenceException();
                        }

                        if (doubleEvaluated)
                        {
                            output.WriteLine("{0:F5}", doubleVisitor.lastEvaluation);
                        }
                        else
                        {
                            output.WriteLine("{0:F5}", lastExpressionTree.Accept(doubleVisitor));
                        }

                        doubleEvaluated = true;
                        break;

                    default:
                        throw new FormatException();
                    }
                }
                catch (NullReferenceException) {
                    output.WriteLine("Expression Missing");
                }
                catch (OverflowException) {
                    output.WriteLine("Overflow Error");
                }
                catch (DivideByZeroException) {
                    output.WriteLine("Divide Error");
                }
                catch (FormatException) {
                    output.WriteLine("Format Error");
                }

                expression = input.ReadLine();
            }
        }