/// <inheritdoc /> public bool HandleTreeGeneration(string token, ref Stack <ExpNode> nodeStack, ref Dictionary <string, double> variables, ref bool encounteredError) { if (!Regex.IsMatch(token, RXnonParenOperators)) { return(false); } ExpNode tmp = new ExpNode(token); if (nodeStack.Count >= 2) { // when operator found, if there are at least two nodes // on the stack, pop the top two and add them as as right // and left nodes for the operator, then push operator to // the node stack tmp.RHS = nodeStack.Pop(); tmp.LHS = nodeStack.Pop(); nodeStack.Push(tmp); } else // not enough nodes in stack to assign to right/left of operator { encounteredError = true; } return(true); }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { if (!Regex.IsMatch(node.Token, RXnonParenOperators)) { return(false); } switch (node.Token) { case "+": value = node.LHS.Evaluate(variables) + node.RHS.Evaluate(variables); break; case "-": value = node.LHS.Evaluate(variables) - node.RHS.Evaluate(variables); break; case "*": value = node.LHS.Evaluate(variables) * node.RHS.Evaluate(variables); break; case "/": value = node.LHS.Evaluate(variables) / node.RHS.Evaluate(variables); break; case "^": value = Math.Pow(node.LHS.Evaluate(variables), node.RHS.Evaluate(variables)); break; default: value = 0.0f; break; } return(true); }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { if (!Regex.IsMatch(node.Token, RXfunctions)) { return(false); } switch (node.Token) { case "SIN": value = Math.Sin(node.LHS.Evaluate(variables)); break; case "COS": value = Math.Cos(node.LHS.Evaluate(variables)); break; case "LOG": value = Math.Log(node.LHS.Evaluate(variables), node.RHS.Evaluate(variables)); break; default: value = 0.0f; break; } return(true); }
/// <inheritdoc /> public bool HandleTreeGeneration(string token, ref Stack <ExpNode> nodeStack, ref Dictionary <string, double> variables, ref bool encounteredError) { if (!Regex.IsMatch(token, RXfunctions)) { return(false); } var node = new ExpNode(token); if (token == "LOG") { if (nodeStack.Count >= 2) { node.RHS = nodeStack.Pop(); node.LHS = nodeStack.Pop(); } else { encounteredError = true; } } else { if (nodeStack.Count >= 1) { node.LHS = nodeStack.Pop(); } else { encounteredError = true; } } nodeStack.Push(node); return(true); }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { if (!Regex.IsMatch(node.Token, RXvariable)) { return(false); } value = variables.ContainsKey(node.Token) ? variables[node.Token] : 0.0f; return(true); }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { if (!Regex.IsMatch(node.Token, RXnumber)) { return(false); } value = double.Parse(node.Token); return(true); }
//returns true only if both nodes are VariableNodes and the names of the variables are the same. public bool VariableNodeCompare(ExpNode inputLeftNode, ExpNode inputRightNode) { VarNode leftNodeAsVar = inputLeftNode as VarNode; VarNode righttNodeAsVar = inputRightNode as VarNode; if (leftNodeAsVar == null || righttNodeAsVar == null) //both are null, so they are not both variableNodes, and are not equal { return(false); } if (leftNodeAsVar.Variable == righttNodeAsVar.Variable) //variables match the same string { return(true); } return(false); //if the nodes do not contain the same string. }
// Factory for ExpNodes to create an expression tree. protected ExpNode ConstructTreeFromTokens(List <string> expressionInPostfixNotation) { Stack <ExpNode> stack = new Stack <ExpNode>(); foreach (string tok in expressionInPostfixNotation) { if (operators.TryGetValue(tok, out OperatorToken op)) //if the token is an operator { ExpNode newrightNode = stack.Pop(); //grabs the right and left nodes from the stack ExpNode newLeftNode = stack.Pop(); ExpNode newOpNode = new OpNode(ref newLeftNode, ref newrightNode, op.Symbol[0]); // Creates an OpNode with the nodes we just popped off the stack. stack.Push(newOpNode); // Adds the new operator node to the stack. } else // if the token is an operand { stack.Push(MakeDataNode(tok)); //push the operand node on to the stack } } //when there are no more tokens. There should only be one node on the stack. return(stack.Pop()); // returns the root node of the tree. }
/// <summary> /// generate expression tree from postfix expression /// </summary> private void GenerateTree() { Stack <ExpNode> nodeStack = new Stack <ExpNode>(); foreach (var token in _postfixTokens) { int i = 0; while (!_tokenHandlers[i].HandleTreeGeneration(token, ref nodeStack, ref Variables, ref _encounteredError)) { i++; } if (_encounteredError) { _root = null; return; } } // should be exactly one node in stack after completing tree // if there is not exactly one node, set root as null to indicate error _root = nodeStack.Count == 1 ? nodeStack.Pop() : null; }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { if (!Regex.IsMatch(node.Token, RXconstant)) { return(false); } switch (node.Token) { case "pi": value = Math.PI; break; case "e": value = Math.E; break; default: value = 0.0d; break; } return(true); }
public ExpTree(string expression) { StringExpression = expression; //saves the expression for easy display purposes root = ConstructTreeFromTokens(ShuntingYard(expression)); }
/// <inheritdoc /> public bool HandleTreeEvaluation(ExpNode node, Dictionary <string, double> variables, ref double value) { throw new NotImplementedException(); }
public OpNode(char opInput = '!') // constructor for a node with no children { left = null; right = null; operation = opInput; }
public OpNode(ref ExpNode leftNode, char opInput = '!') // constructor used in factory. Deals with the left node only { left = leftNode; right = null; operation = opInput; }
public OpNode(ref ExpNode leftNode, ref ExpNode rightNode, char opInput = '!') //extended constructor with all fields { left = leftNode; right = rightNode; operation = opInput; }