Example #1
0
        public void Visit(UnaryOperator @operator)
        {
            var nodeVisitor = new DoubleVisitor();

            @operator.DependentNode.Accept(nodeVisitor);
            Result = nodeVisitor.Result * -1;
        }
Example #2
0
        private static int Main(string[] args)
        {
            var inputProcessor = new InputProcessor(Console.In);

            var treeBuilder = new TreeBuilder();
            var tree        = new Tree(null);

            Commands command;

            while ((command = inputProcessor.GetAllSubExpressions()) != Commands.End)
            {
                switch (command)
                {
                case Commands.EmptyLine:
                    break;

                case Commands.Error:
                    ErrorHandler.PrintError(Errors.Format);
                    break;

                case Commands.Expression:
                    tree.Clear();
                    treeBuilder.Clear();
                    var processingFailed = false;

                    for (var index = inputProcessor.GetSubExpressionCount(); index > 0; index--)
                    {
                        var subExpression = inputProcessor.GetSubExpression(index);
                        if (treeBuilder.Process(subExpression))
                        {
                            continue;
                        }
                        processingFailed = true;
                        break;
                    }

                    var root = treeBuilder.GetRoot();
                    if (root == null || processingFailed)
                    {
                        ErrorHandler.PrintError(Errors.Format);
                        break;
                    }

                    tree = new Tree(root);
                    break;

                case Commands.Integer:
                    if (tree.Root == null)
                    {
                        Program.Writer.WriteLine("Expression Missing");
                        break;
                    }

                    var intVisitor = new IntegerVisitor();
                    tree.Root.Accept(intVisitor);

                    if (intVisitor.Result == null)
                    {
                        break;
                    }

                    Program.Writer.WriteLine(intVisitor.Result);
                    break;

                case Commands.Double:
                    if (tree.Root == null)
                    {
                        Program.Writer.WriteLine("Expression Missing");
                        break;
                    }

                    var doubleVisitor = new DoubleVisitor();
                    tree.Root.Accept(doubleVisitor);

                    Program.Writer.WriteLine(doubleVisitor.Result.ToString("f05"));
                    break;

                case Commands.FullParentheses:
                    if (tree.Root == null)
                    {
                        Program.Writer.WriteLine("Expression Missing");
                        break;
                    }

                    var fullParenthesesVisitor = new FullParenthesesVisitor();
                    tree.Root.Accept(fullParenthesesVisitor);

                    Program.Writer.WriteLine(fullParenthesesVisitor.ParenthesizedExpression.ToString());
                    break;

                case Commands.MinParentheses:
                    if (tree.Root == null)
                    {
                        Program.Writer.WriteLine("Expression Missing");
                        break;
                    }

                    var minParenthesesVisitor = new MinParenthesesVisitor();
                    tree.Root.Accept(minParenthesesVisitor);

                    Program.Writer.WriteLine(minParenthesesVisitor.ParenthesizedExpression.ToString());
                    break;
                }
            }

            return(0);
        }