Exemplo n.º 1
0
        private Node determineNode(string expression, int operandBegin, int i)
        {
            string operandName = expression.Substring(operandBegin, i - operandBegin);

            // Figure out if operand is constant or variable valued
            if (65 < expression[operandBegin])
            {
                // We know that the operand, left of current operator, is a variable
                if (variableDictionary.ContainsKey(operandName) == true)
                {
                    VarNode newNode = new VarNode(operandName, variableDictionary[operandName]);
                    return(newNode);
                }
                else
                {
                    VarNode newNode = new VarNode(operandName, 0);
                    variableDictionary.Add(operandName, newNode.NodeValue);
                    return(newNode);
                }
            }

            else
            {
                // We know that the operand, left of current operator, is a constant
                ConstNode newNode = new ConstNode(operandName, Convert.ToDouble(operandName));
                return(newNode);
            }
        }
Exemplo n.º 2
0
        private double Eval(Node node)
        {
            //Evaluate based on the kind of node

            ConstNode constnode = node as ConstNode;

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

            VarNode varnode = node as VarNode;

            if (varnode != null)
            {
                try
                { return(variableDict[varnode.Name]); }
                catch
                {
                    Console.WriteLine("Variable " + varnode.Name + " has not been defined. Continuing evaluation with variable equal to 0.");
                }
            }

            OpNode opnode = node as OpNode;

            if (opnode != null)
            {
                switch (opnode.Op)
                {
                case '+':
                    return(Eval(opnode.Left) + Eval(opnode.Right));

                case '-':
                    return(Eval(opnode.Left) - Eval(opnode.Right));

                case '*':
                    return(Eval(opnode.Left) * Eval(opnode.Right));

                case '/':
                    return(Eval(opnode.Left) / Eval(opnode.Right));
                }
            }

            return(0);
        }
Exemplo n.º 3
0
        private double Eval(Node node)
        {
            //Evaluate based on the kind of node

            ConstNode constnode = node as ConstNode;

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

            VarNode varnode = node as VarNode;

            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(variableDict[varnode.Name]);
            }

            OpNode opnode = node as OpNode;

            if (opnode != null)
            {
                switch (opnode.Op)
                {
                case '+':
                    return(Eval(opnode.Left) + Eval(opnode.Right));

                case '-':
                    return(Eval(opnode.Left) - Eval(opnode.Right));

                case '*':
                    return(Eval(opnode.Left) * Eval(opnode.Right));

                case '/':
                    return(Eval(opnode.Left) / Eval(opnode.Right));
                }
            }

            return(0);
        }