diff --git a/ParseTree/ParseTree.Src/Nodes/OperationNode.cs b/ParseTree/ParseTree.Src/Nodes/OperationNode.cs index b176098..26a1137 100644 --- a/ParseTree/ParseTree.Src/Nodes/OperationNode.cs +++ b/ParseTree/ParseTree.Src/Nodes/OperationNode.cs @@ -24,7 +24,7 @@ public override string ToString() { if (LeftChild is not null && RightChild is not null) { - return $"( {operation} {LeftChild.ToString()} {RightChild.ToString()})"; + return $"({operation} {LeftChild.ToString()} {RightChild.ToString()})"; } throw new IncorrectTreeException(); } diff --git a/ParseTree/ParseTree.Src/Operation.cs b/ParseTree/ParseTree.Src/Operation.cs index 006b49f..b5b2174 100644 --- a/ParseTree/ParseTree.Src/Operation.cs +++ b/ParseTree/ParseTree.Src/Operation.cs @@ -2,8 +2,20 @@ namespace ParseTree.Dependencies; +/// +/// Class implementing arithmetic operations based on char input +/// class Operation { + /// + /// Do the arithmetic operation corresponding to char + /// + /// Char '*', '+', '-' or '/' + /// Left operand + /// Right operand + /// Result of the operation + /// Thrown if operandRight is 0 and operation is '/' + /// thrown if argument operation is something different to '*', '+', '-' or '/' public static double Calculate(char operation, double operandLeft, double operandRight) { switch (operation) diff --git a/ParseTree/ParseTree.Src/Tree.cs b/ParseTree/ParseTree.Src/Tree.cs index 0053de2..50f3452 100644 --- a/ParseTree/ParseTree.Src/Tree.cs +++ b/ParseTree/ParseTree.Src/Tree.cs @@ -44,6 +44,7 @@ private static INode Parse(string expression, ref int index) if (currentChar == '(') { ++index; + SkipSpaces(expression, ref index); newNode = new OperationNode(expression[index]); ++index; newNode.LeftChild = Parse(expression, ref index); diff --git a/ParseTree/ParseTree.Tests/ParseTree.Tests.cs b/ParseTree/ParseTree.Tests/ParseTree.Tests.cs index 5883500..f51bf49 100644 --- a/ParseTree/ParseTree.Tests/ParseTree.Tests.cs +++ b/ParseTree/ParseTree.Tests/ParseTree.Tests.cs @@ -5,7 +5,7 @@ namespace ParseTree.Tests; public class Tests { - [TestCase("(* (+ 1 1) 2)", 4)] + [TestCase("( * (+ 1 1 ) 2 )", 4)] [TestCase("5", 5)] [TestCase("(/ 5 2)", 2.5)] public void TestEvaluation(string expression, double expectedResult) @@ -24,5 +24,16 @@ public void IncorrectInputTest(string expression) { Assert.Throws(() => new Tree(expression)); } + + [TestCase("(* (+ 1 1) 2)")] + [TestCase("5")] + [TestCase("(/ 5 2)")] + public void ToStringTests(string expression) + { + var tree = new Tree(expression); + var result = tree.ToString(); + + Assert.That(result, Is.EqualTo(expression)); + } } \ No newline at end of file