예제 #1
0
        private double Evaluate(Node node)
        {
            if (node != null)
            {
                if (node is OperatorNode)
                {
                    OperatorNode opNode     = node as OperatorNode;
                    double       evaluation = opNode.Evaluate();
                    return(evaluation);
                }

                if (node is ConstantNode)
                {
                    ConstantNode constNode = node as ConstantNode;
                    return(constNode.Value);
                }

                if (node is VariableNode)
                {
                    VariableNode varNode = node as VariableNode;
                    return(this.Variables[varNode.Name]);
                }
            }

            return(0);
        }
예제 #2
0
        /// <summary>
        /// Evaluates the arithmetic logic of an expression tree.
        /// </summary>
        /// <param name="current">The head of the tree to be evaluated.</param>
        /// <returns>Returns the evaluation of the expression tree.</returns>
        private double EvaluateHelper(OperatorNode current)
        {
            // Both children are leaves.
            if (!this.IsOperatorNode(current.Left) && !this.IsOperatorNode(current.Right))
            {
                double leftVal, rightVal;
                if (this.IsVariableNode(current.Left))
                {
                    leftVal = this.variables[current.Left.Value]; // Get left value from dictionary.
                }
                else
                {
                    leftVal = Convert.ToDouble(current.Left.Value); // Get left value from left node.
                }

                if (this.IsVariableNode(current.Right))
                {
                    rightVal = this.variables[current.Right.Value]; // Get right value from dictionary.
                }
                else
                {
                    rightVal = Convert.ToDouble(current.Right.Value); // Get right value from right node.
                }

                return(current.Evaluate(leftVal, rightVal));
            }
            else if (!this.IsOperatorNode(current.Left))
            {
                // Only the right node is an operator node.
                double leftVal;
                if (this.IsVariableNode(current.Left))
                {
                    leftVal = this.variables[current.Left.Value]; // Get left value from dictionary.
                }
                else
                {
                    leftVal = Convert.ToDouble(current.Left.Value); // Get left value from left node.
                }

                return(current.Evaluate(leftVal, this.EvaluateHelper((OperatorNode)current.Right)));
            }
            else if (!this.IsOperatorNode(current.Right))
            {
                // Only the left node is an operator node.
                double rightVal;
                if (this.IsVariableNode(current.Right))
                {
                    rightVal = this.variables[current.Right.Value]; // Get right value from dictionary.
                }
                else
                {
                    rightVal = Convert.ToDouble(current.Right.Value); // Get right value from right node.
                }

                return(current.Evaluate(this.EvaluateHelper((OperatorNode)current.Left), rightVal));
            }
            else
            {
                // Both children are operator nodes.
                OperatorNode leftNode = (OperatorNode)current.Left, rightNode = (OperatorNode)current.Right;
                return(current.Evaluate(this.EvaluateHelper(leftNode), this.EvaluateHelper(rightNode)));
            }
        }