Пример #1
0
        /// <summary>
        /// Will go through the postfix expression and create the ExpressionTree.
        /// </summary>
        /// <param name="expression">Expression string the user inputs.</param>
        public void CreateExpressionTree(string expression)
        {
            List <string> postfix            = ConvertExpressionToPostfix(expression);
            Stack <ExpressionTreeNode> stack = new Stack <ExpressionTreeNode>();
            string       temp = string.Empty;
            OperatorNode current;

            for (int i = 0; i < postfix.Count; i++)
            {
                if (!IsOperator(postfix[i]))
                {
                    if (IsVariable(postfix[i]))
                    {
                        stack.Push(new VariableNode(postfix[i], ref this.variables)); // If variable node, push to stack
                    }
                    else
                    {
                        stack.Push(new ConstantNode(Convert.ToDouble(postfix[i]))); // Else is a constant node, push to stack
                    }
                }
                else
                {
                    current = this.factory.CreateOperatorNode(postfix[i]); // Create a new operator node based on the string operator

                    current.Right = stack.Pop();                           // Pop out the first node and set it to the right of the operator node
                    current.Left  = stack.Pop();                           // Pop out the second node and set it to the left of the operator node
                    stack.Push(current);                                   // Push the subexpression to the stack
                }
            }

            // Only element left in the stack will be the root of the expression tree
            this.root = stack.Pop(); // Set the root to the last element in the stack
        }
        /// <summary>
        ///  This method evaluate the given expression by first converting the expression to a postfix expression
        ///  and then build the postfix expression to a Tree.
        /// </summary>
        /// <returns> the evaluated value or 0.0  if Root is null.</returns>
        public double Evaluate()
        {
            if (this.Root != null)
            {
                ExpressionTreeNode root = this.Root;
                return(root.Evaluate(ref this.variables));
            }

            // if the root is empty.
            return(0.0);
        }
Пример #3
0
        /// <summary>
        /// Will go through the postfix expression and create the ExpressionTree.
        /// </summary>
        /// <param name="expression">Expression string the user inputs.</param>
        public void CreateExpressionTree(string expression)
        {
            List <string> infix              = this.ConvertExpressionToInfix(expression);
            List <string> postfix            = this.ConvertInfixToPostfix(infix);
            Stack <ExpressionTreeNode> stack = new Stack <ExpressionTreeNode>();
            string       temp = string.Empty;
            OperatorNode current;

            for (int i = 0; i < postfix.Count; i++)
            {
                if (!this.IsOperator(postfix[i]))
                {
                    if (IsVariable(postfix[i]))
                    {
                        if (!this.variables.ContainsKey(postfix[i]))
                        {
                            this.SetVariable(postfix[i], 0); // If a new variable, add it to dictionary and set value to 0.
                        }

                        stack.Push(new VariableNode(postfix[i], ref this.variables)); // If variable node, push to stack
                    }
                    else
                    {
                        stack.Push(new ConstantNode(Convert.ToDouble(postfix[i]))); // Else is a constant node, push to stack
                    }
                }
                else
                {
                    char op = postfix[i].ToCharArray()[0];
                    current = this.factory.CreateOperatorNode(op); // Create a new operator node based on the string operator

                    current.Right = stack.Pop();                   // Pop out the first node and set it to the right of the operator node
                    current.Left  = stack.Pop();                   // Pop out the second node and set it to the left of the operator node
                    stack.Push(current);                           // Push the subexpression to the stack
                }
            }

            // Only element left in the stack will be the root of the expression tree
            this.root = stack.Pop(); // Set the root to the last element in the stack
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExpressionTree"/> class.
 /// </summary>
 /// <param name="expression">A valid string expression.</param>
 public ExpressionTree(string expression)
 {
     this.variables     = new Dictionary <string, double>();
     this.variableNames = new List <string>();
     this.root          = this.BuildExpressionTree(expression);
 }
        /// <summary>
        /// Takes an expression tree node and returns if the node is a variable node type.
        /// </summary>
        /// <param name="node">Expression tree node.</param>
        /// <returns>Bool that represents is the node is a constant or variable node type.</returns>
        private bool IsVariableNode(ExpressionTreeNode node)
        {
            Type nodeType = node.GetType();

            return(nodeType.Equals(typeof(VariableNode)));
        }
        /// <summary>
        /// Takes an expression tree node and returns if the node is a constant node type.
        /// </summary>
        /// <param name="node">Expression tree node.</param>
        /// <returns>Bool that represents is the node is a constant or variable node type.</returns>
        private bool IsConstantNode(ExpressionTreeNode node)
        {
            Type nodeType = node.GetType();

            return(nodeType.Equals(typeof(ConstantNode)));
        }