public void TreeHasCorrectStringForm() { var tree = new Arithmetic.Product<int>(VariableNode.Make<int>(0, "x"), new Arithmetic.Plus<int>(VariableNode.Make<int>(1, "y"), Constant.Int(3))); Assert.AreEqual(tree.ToString(), "(x ∙ (y + 3))"); }
private static INode GetTree() { try { if (_tokens.Count == 0) { throw new ParseException("Not enough operands"); } if (_tokens[0] is IOperator) { var oper = (Operator) _tokens[0]; _tokens.RemoveAt(0); INode temp; switch (oper.Value) { case "+": temp = new Arithmetic.Plus<double>(GetTree(), GetTree()); temp.SwitchCoupleOfChildren(); return temp; case "-": temp = new Arithmetic.Minus<double>(GetTree(), GetTree()); temp.SwitchCoupleOfChildren(); return temp; case "∙": temp = new Arithmetic.Product<double>(GetTree(), GetTree()); temp.SwitchCoupleOfChildren(); return temp; case "/": temp = new Arithmetic.Divide<double>(GetTree(), GetTree()); temp.SwitchCoupleOfChildren(); return temp; case "^": temp = new Arithmetic.Pow<double>(GetTree(), GetTree()); temp.SwitchCoupleOfChildren(); return temp; } } if (_tokens[0] is IOperand) { var operand = (Operand) _tokens[0]; _tokens.RemoveAt(0); if (NodeElementNames.GetVariableNodeNames().IndexOf(operand.Name) != -1) { return VariableNode.Make<double>(NodeElementNames.GetVariableNodeNames().IndexOf(operand.Name), operand.Name); } return Constant.Double(Double.Parse(operand.Name)); } } catch(FormatException exp) { throw new ParseException("Error in parsing. Perhaps you've forgotten the multiplication sign '*'", exp); } throw new ParseException(string.Format("Unexpected symbol '{0}'", _tokens[0])); }
public void TreeHasCorrectStringForm() { var tree = new Arithmetic.Product<int>(VariableNode.Make<int>(0, "x"), new Arithmetic.Plus<int>(VariableNode.Make<int>(1, "y"), Constant.Int(3))); Assert.AreEqual(tree.ToString(), "(x ∙ (y + 3))"); var tree2 = new Logic.MultipleOr(new PredicateNode("P", VariableNode.Make<int>(0, "x")), new PredicateNode("Q", VariableNode.Make<int>(1, "y"), VariableNode.Make<int>(2, "z")), new PredicateNode("H", new FunctionNode("f", VariableNode.Make<int>(0, "x")), new FunctionNode("c"))); Assert.AreEqual(tree2.ToString(), "P(x) V Q(y,z) V H(f(x),c)"); }