/// <summary> /// Name:Evaluate /// Description:Evaluates the expression tree /// </summary> /// <param name="node">inputed node of the expression tree</param> /// <returns>returns a double of the evaluated expression</returns> private double Evaluate(BasicNode node) { ConstantNode constnode = node as ConstantNode; if (constnode != null) { return(constnode.OperatorValue); } VariableNode varnode = node as VariableNode; 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(this.variables[varnode.Name]); } OperatorNode opnode = node as OperatorNode; if (opnode != null) { return(opnode.Evaluate(this.Evaluate(opnode.Left), this.Evaluate(opnode.Right))); } return(0); }
/// <summary> /// Name:Evaluate /// Description:Evaluates the expression tree /// </summary> /// <param name="node">inputed node of the expression tree</param> /// <returns>returns a double of the evaluated expression</returns> private double Evaluate(BasicNode node) { if (node != null && node is OperatorNode) { OperatorNode temp = (OperatorNode)node; return(temp.Evaluate(this.Evaluate(temp.Left), this.Evaluate(temp.Right))); } if (node != null && node is VariableNode) { try { return(this.variables[node.Name]); } catch { throw new System.ArgumentException("Variable " + node.Name + " has not been defined.", "Variable Not Defined"); } } if (node != null && node is ConstantNode) { ConstantNode temp = (ConstantNode)node; return(temp.OperatorValue); } return(0); }
/// <summary> /// Name:Compile /// Description:Compiles the expression /// </summary> /// <param name="expression">inputed expression</param> /// <returns>returns the basenode of the expression tree</returns> private static BasicNode Compile(string expression) { if (expression.Length != 0) { int operatorIndex = expression.Length - 1; while (operatorIndex > 0 && !ExpressionTreeFactory.IsValidOperator(expression[operatorIndex])) { operatorIndex--; } if (ExpressionTreeFactory.IsValidOperator(expression[operatorIndex])) { OperatorNode newNode = ExpressionTreeFactory.CreateOperatorNode(expression[operatorIndex]); newNode.Left = Compile(expression.Substring(0, operatorIndex)); newNode.Right = Compile(expression.Substring(operatorIndex + 1)); return(newNode); } double number; if (double.TryParse(expression, out number)) { ConstantNode newNode = new ConstantNode(number); return(newNode); } else { VariableNode newNode = new VariableNode(expression); return(newNode); } } return(null); }
/// <summary> /// Name:Compile /// Description:Compiles the expression /// </summary> /// <param name="expression">inputed expression</param> /// <returns>returns the basenode of the expression tree</returns> private BasicNode Compile(string expression) { expression = expression.Replace(" ", string.Empty); int counter = 1, i = 0; if (expression[i] == '(') { for (i = 1; expression.Length > i; i++) { if (expression[i] == '(') { counter++; } else if (expression[i] == ')') { counter--; if (counter == 0) { if (i == expression.Length - 1) { return(this.Compile(expression.Substring(1, expression.Length - 2))); } break; } } } if (counter != 0) { throw new System.ArgumentException("Too many or too few parentheses", "Invalid Expression"); } } int index = GetLowestOperatorInheritanceIndex(expression); if (index != -1) { OperatorNode node = ExpressionTreeFactory.CreateOperatorNode(expression[index]); node.Right = this.Compile(expression.Substring(index + 1)); node.Left = this.Compile(expression.Substring(0, index)); return(node); } else if (index == -2) { throw new System.ArgumentException("Too many or too few parentheses", "Invalid Expression"); } return(this.BuildVNode(expression)); }