Exemplo n.º 1
0
        /// <summary>
        /// Function to evaluate the value of the expression tree.
        /// </summary>
        /// <param name="root"> Expression tree root.</param>
        /// <returns> evaluated value of tree.</returns>
        private double Evaluate(ExpressionTreeNode root)
        {
            ConstantNode constantNode = root as ConstantNode;   // will return null if node isn't constantNode.//

            if (constantNode != null)
            {
                return(constantNode.Value);
            }

            VariableNode variableNode = root as VariableNode;

            if (variableNode != null)
            {
                return(variableNode.Evaluate());
            }

            OperatorNode operatorNode = root as OperatorNode;

            if (operatorNode != null)
            {
                return(operatorNode.Evaluate());
            }

            throw new NotSupportedException();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds the tree.
        /// </summary>
        /// <param name="expression">expression in infix notation.</param>
        /// <returns>the root node of the new tree.</returns>
        private ExpressionTreeNode BuildTree(string expression)
        {
            Stack <ExpressionTreeNode> nodes = new Stack <ExpressionTreeNode>();
            var postfixExpression            = this.ShuntingYardAlogritthm(expression);

            foreach (var item in postfixExpression)
            {
                if (item.Length == 1 && this.IsOperatorOrParenthesis(item[0]))
                {
                    OperatorNode node = this.operatorNodeFactory.CreateOperatorNode(item[0]);
                    node.Right = nodes.Pop();
                    node.Left  = nodes.Pop();
                    nodes.Push(node);
                }
                else
                {
                    double num = 0.0;
                    if (double.TryParse(item, out num))
                    {
                        nodes.Push(new ConstantNode(num));
                    }
                    else
                    {
                        nodes.Push(new VariableNode(item, ref this.variables));
                    }
                }
            }

            return(nodes.Pop());
        }
Exemplo n.º 3
0
 /// <summary>
 /// Function to create operator node based on what the operator sign is.
 /// </summary>
 /// <param name="operatorSign"> character denoting operator.</param>
 /// <returns> OperatorNode object.</returns>
 public OperatorNode CreateOperatorNode(char operatorSign)
 {
     if (this.operators.ContainsKey(operatorSign))
     {
         OperatorNode operatorNode = (OperatorNode)Activator.CreateInstance(this.operators[operatorSign]);
         return(operatorNode);
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Function to convert string expression into expression tree.
        /// </summary>
        /// <param name="expression"> string denoting expression.</param>
        /// <returns> returns the expression Tree Node.</returns>
        private ExpressionTreeNode Compile(string expression)
        {
            Stack <ExpressionTreeNode> nodes = new Stack <ExpressionTreeNode>();

            // return null if string is empty.//
            if (string.IsNullOrEmpty(expression))
            {
                return(null);
            }

            var postFixExpression = this.ShuntingYardAlgorithm(expression);

            foreach (var item in postFixExpression)
            {
                if (item.Length == 1 && this.IsOperatorOrParenthesis(item[0]))
                {
                    OperatorNode node = this.operatorNodeFactory.CreateOperatorNode(item[0]);
                    node.Right = nodes.Pop();
                    node.Left  = nodes.Pop();
                    nodes.Push(node);
                }
                else
                {
                    double number = 0.0;
                    if (double.TryParse(item, out number))
                    {
                        nodes.Push(new ConstantNode(number));
                    }
                    else
                    {
                        nodes.Push(new VariableNode(item, ref this.variables));
                    }
                }
            }

            return(nodes.Pop());
        }