Exemple #1
0
 //Helper funton used to get the value of the var or const node with relation to the operand node
 public double evalSubTree(opNode op)
 {
     double temp = 0.0;
     //If left and right node is constant carry out computation
     if ((op.Left.GetType() == typeof(constNode) == true) && (op.Right.GetType() == typeof(constNode)))
     {
         constNode lConstant = (constNode)op.Left;
         constNode rConstant = (constNode)op.Right;
         double lvalue = lConstant.val;
         double rvalue = rConstant.val;
         temp = calculateChildren(op.op, lvalue, rvalue);
     }
     //If left node is var and right node is constant carry out computation
     else if ((op.Left.GetType() == typeof(varNode) == true) && (op.Right.GetType() == typeof(constNode)))
     {
         varNode lConstant = (varNode)op.Left;
         constNode rConstant = (constNode)op.Right;
         double lvalue = m_variables[lConstant.valName];
         double rvalue = rConstant.val;
         temp = calculateChildren(op.op, lvalue, rvalue);
     }
     //If left and right are var nodes look up values in dictionary and compute
     else if ((op.Left.GetType() == typeof(varNode) == true) && (op.Right.GetType() == typeof(varNode)))
     {
         varNode lConstant = (varNode)op.Left;
         varNode rConstant = (varNode)op.Right;
         double lvalue = m_variables[lConstant.valName];
         double rvalue = m_variables[rConstant.valName];
         temp = calculateChildren(op.op, lvalue, rvalue);
     }
     //if left is constant node and right is var carry out computations and neccessary steps
     else if ((op.Left.GetType() == typeof(constNode) == true) && (op.Right.GetType() == typeof(varNode)))
     {
         constNode lConstant = (constNode)op.Left;
         varNode rConstant = (varNode)op.Right;
         double lvalue = lConstant.val;
         double rvalue = m_variables[rConstant.valName];
         temp = calculateChildren(op.op, lvalue, rvalue);
     }
     //get right child if its a varNode 
     else if((op.Left.GetType() == typeof(opNode) == true) && (op.Right.GetType() == typeof(varNode)))
     {
         varNode rVar = (varNode)op.Right;
         double rvalue = m_variables[rVar.valName];
         return temp;
     }
     //get right child if its a constNode
     else if((op.Left.GetType() == typeof(opNode) == true) && (op.Right.GetType() == typeof(constNode)))
     {
         constNode rVar = (constNode)op.Right;
         double rvalue = rVar.val;
         temp = rvalue;
     }  
     //Left child is a varNode while right is an opNode
     else if((op.Left.GetType() == typeof(varNode)) && (op.Right.GetType() == typeof(opNode)))
     {
         varNode lvar = (varNode)op.Left;
         double lvalue = m_variables[lvar.valName];
         temp = lvalue;
     }
     //left child
     else if((op.Left.GetType() == typeof(constNode)) && (op.Right.GetType() == typeof(opNode)))
     {
         constNode lvar = (constNode)op.Left;
         double lvalue = lvar.val;
         temp = lvalue;
     }
     return temp;
 }
Exemple #2
0
        private static Node Compile(string exp, char op)
        {
            int counter = 0;
            //Iterate through expression from right to left
            for (int i = exp.Length - 1; i >= 0; i--)
            {
 
                if (')' == exp[i]) { counter++; }
                else if ('(' == exp[i]) { counter--; }
                if (counter == 0 && op == exp[i])
                {
                    opNode on = new opNode() { op = exp[i] };

                    on.Left = Compile(exp.Substring(0, i));
                    on.Right = Compile(exp.Substring(i + 1));

                    return on;
                }
            }
            //Throw exeception if we don't find a matching parenthesis
            if (counter != 0) { throw new Exception(); }
            return null;
        }