Example #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);
        }
Example #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);
        }
Example #3
0
 /// <summary>
 /// Name:ExpressionTree
 /// Description:Initializes a new instance of the <see cref="ExpressionTree"/> class.
 /// </summary>
 /// <param name="inputedExpression">inputed expression by user or hardcoded if user doesn't set it to anything</param>
 public ExpressionTree(string inputedExpression)
 {
     this.root       = this.Compile(inputedExpression);
     this.Expression = inputedExpression;
 }
Example #4
0
 /// <summary>
 /// Name:ExpressionTree
 /// Description:Initializes a new instance of the <see cref="ExpressionTree"/> class.
 /// </summary>
 /// <param name="inputedExpression">inputed expression by user or hardcoded if user doesn't set it to anything</param>
 public ExpressionTree(string inputedExpression)
 {
     this.root       = Compile(inputedExpression);
     this.variables  = new Dictionary <string, double>();
     this.Expression = inputedExpression;
 }
Example #5
0
 /// <summary>
 /// Name:OperatorNode
 /// Description: Initializes a new instance of the <see cref="OperatorNode"/> class.
 /// </summary>
 /// <param name="inputedOp">inputed operator</param>
 /// <param name="left">left node</param>
 /// <param name="right">right node</param>
 public OperatorNode(char inputedOp, BasicNode left, BasicNode right)
 {
     this.operate = inputedOp;
     this.left    = left;
     this.right   = right;
 }