Пример #1
0
        /// <summary>
        /// Name:Evaluate
        /// Description:Evaluates the expression tree
        /// </summary>
        /// <param name="node">inputed node of the expression tree</param>
        /// <returns>returns a double of the evaluated expression</returns>
        private double Evaluate(BasicNode node)
        {
            ConstantNode constnode = node as ConstantNode;

            if (constnode != null)
            {
                return(constnode.OperatorValue);
            }

            VariableNode varnode = node as VariableNode;

            if (varnode != null)
            {
                // used to be a try/catch, but now we set every new variable to 0 when the tree is made, so there will always be a value to obtain.
                return(this.variables[varnode.Name]);
            }

            OperatorNode opnode = node as OperatorNode;

            if (opnode != null)
            {
                return(opnode.Evaluate(this.Evaluate(opnode.Left), this.Evaluate(opnode.Right)));
            }

            return(0);
        }
Пример #2
0
        /// <summary>
        /// Name:Evaluate
        /// Description:Evaluates the expression tree
        /// </summary>
        /// <param name="node">inputed node of the expression tree</param>
        /// <returns>returns a double of the evaluated expression</returns>
        private double Evaluate(BasicNode node)
        {
            if (node != null && node is OperatorNode)
            {
                OperatorNode temp = (OperatorNode)node;

                return(temp.Evaluate(this.Evaluate(temp.Left), this.Evaluate(temp.Right)));
            }

            if (node != null && node is VariableNode)
            {
                try
                {
                    return(this.variables[node.Name]);
                }
                catch
                {
                    throw new System.ArgumentException("Variable " + node.Name + " has not been defined.", "Variable Not Defined");
                }
            }

            if (node != null && node is ConstantNode)
            {
                ConstantNode temp = (ConstantNode)node;
                return(temp.OperatorValue);
            }

            return(0);
        }