Esempio n. 1
0
        // This will render the tree
        // Here's an example on how the tree can be read
        // so this function ONLY reads the end result after all is said and
        // done
        double resolve(Node traverse)
        {
            double evaluation = 0;

            if (traverse is constNode)
            {
                return((traverse as constNode).value);
            }
            else if (traverse is VarNode)
            {
                return(m_vars[(traverse as VarNode).Name]);
            }
            // The only other thing traverse could be would be an OpNode
            else
            {
                OpNode on = (traverse as OpNode);
                if (on.Op == '+')
                {
                    evaluation += resolve(on.Left) + resolve(on.Right);
                }
                else if (on.Op == '-')
                {
                    evaluation += resolve(on.Left) - resolve(on.Right);
                }
                else if (on.Op == '*')
                {
                    evaluation += resolve(on.Left) * resolve(on.Right);
                }
                else
                {
                    evaluation += resolve(on.Left) / resolve(on.Right);
                }
            }
            return(evaluation);
        }
Esempio n. 2
0
        // This function builds the node tree
        // I grab the least precedent tuple that holds the operator and its
        // location and place it on the tree, I go through the whole list
        // until it's exhausted, I will then add the variables/constants
        Node Compile(string exp)
        {
            OpNode traverse;

            // If this is null that means we have no operators thus we only
            // have a constant or a variable
            if (OpPrecedence(Ops) == null)
            {
                return(BuildSimple(exp));
            }
            Tuple <char, OP, int, int> ToBeNode = OpPrecedence(Ops);
            // This will grab the least precedent Op
            //
            OpNode root = new OpNode(ToBeNode.Item1);
            // Now that the 'root' node has been established I can traverse
            // and determine how

            //int index = 0;// GetOpIndex(exp);
            //if (-1 == index)
            //    return BuildSimple(exp);
            //OpNode root = new OpNode(exp[index]);
            //root.Left = Compile(exp.Substring(0, index));
            //root.Right = Compile(exp.Substring(index + 1));
            //return root;
        }