コード例 #1
0
        ///<summary>
        ///Calculates division
        ///</summary>
        ///<param name="divOp">Division operator that represents a tree of expressions</param>
        ///<returns>
        ///Calculated expression
        ///</returns>
        Expression IVisitor <Expression> .visit(DivOp divOp)
        {
            Expression left  = divOp.left.acceptVisitor(this);
            Expression right = divOp.right.acceptVisitor(this);

            if (left is Literal && right is Literal)
            {
                return(new Literal(((Literal)left).value / ((Literal)right).value));
            }

            return(new DivOp(left, right));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Eon37/Calculator
        static void Main(string[] args)
        {
            Expression ex = new DivOp(new AddOp(new Variable("x"), new Literal(5)), new MultOp(new Literal(10), new Literal(20)));

            Console.WriteLine((ex.acceptVisitor(new CalcExpr())).acceptVisitor(new PrintExpr()));
        }