From 345590951e931c48e35f0b0d76c031b1f5d3804c Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 23 Mar 2022 23:52:31 +0300 Subject: [PATCH 01/12] add basic function for Parse Tree, writing tests --- Homework4/ParsingTree/ParsingTree.sln | 31 +++ .../ParsingTree/ParsingTree/ParsingTree.cs | 186 ++++++++++++++++++ .../ParsingTree/ParsingTree.csproj | 10 + .../ParsingTree/ParsingTreeInterface.cs | 30 +++ Homework4/ParsingTree/ParsingTree/Solution.cs | 11 ++ .../TestParsingTree/TestParsingTree.cs | 50 +++++ .../TestParsingTree/TestParsingTree.csproj | 21 ++ 7 files changed, 339 insertions(+) create mode 100644 Homework4/ParsingTree/ParsingTree.sln create mode 100644 Homework4/ParsingTree/ParsingTree/ParsingTree.cs create mode 100644 Homework4/ParsingTree/ParsingTree/ParsingTree.csproj create mode 100644 Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs create mode 100644 Homework4/ParsingTree/ParsingTree/Solution.cs create mode 100644 Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs create mode 100644 Homework4/ParsingTree/TestParsingTree/TestParsingTree.csproj diff --git a/Homework4/ParsingTree/ParsingTree.sln b/Homework4/ParsingTree/ParsingTree.sln new file mode 100644 index 0000000..69baee1 --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{3A685608-3E27-4534-942C-AE951EF76977}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestParsingTree", "TestParsingTree\TestParsingTree.csproj", "{921D199E-504F-47CD-AC97-9C93A98C8B0D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.Build.0 = Release|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DD0BE32A-1D93-45E3-ACD3-92A75D23D824} + EndGlobalSection +EndGlobal diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs new file mode 100644 index 0000000..f63e897 --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -0,0 +1,186 @@ +using System; + +namespace Tree; + +/// +/// Class representing the parse tree +/// +public class ParsingTree : IParsingTree +{ + /// + /// Class for storing tree nodes + /// + public class Node + { + private Node? leftSon; + private Node? rightSon; + private string? stringValue; + public Node? LeftSon { get => leftSon; set { leftSon = value; } } + public Node? RightSon { get => rightSon; set { rightSon = value; } } + public string? Value { get => stringValue; set { stringValue = value; } } + + } + + private Node? treeRoot; + + /// + /// Function for deleting a tree + /// + public void DeleteTree() + { + DeleteTreeRecursive(treeRoot); + } + + private void DeleteTreeRecursive(Node? root) + { + if (root == null) + { + return; + } + DeleteTreeRecursive(root.LeftSon); + DeleteTreeRecursive(root.RightSon); + root = null; + } + + /// + /// Function for traversing the tree + /// + public float TreeTraversal() + { + return RecursiveTreeTraversal(treeRoot); + } + + private float RecursiveTreeTraversal(Node? root) + { + if (root == null || root.Value == null) + { + return 0; + } + if (root.LeftSon != null) + { + RecursiveTreeTraversal(root.LeftSon); + } + if (root.RightSon != null) + { + RecursiveTreeTraversal(root.RightSon); + } + if (root.RightSon != null && root.LeftSon != null && root.LeftSon.Value != null && root.RightSon.Value != null) + { + if (root.Value == "+") + { + root.Value = (float.Parse(root.LeftSon.Value) + float.Parse(root.RightSon.Value)).ToString(); + } + if (root.Value == "-") + { + root.Value = (float.Parse(root.LeftSon.Value) - float.Parse(root.RightSon.Value)).ToString(); + } + if (root.Value == "*") + { + root.Value = (float.Parse(root.LeftSon.Value) * float.Parse(root.RightSon.Value)).ToString(); + } + if (root.Value == "/") + { + try + { + if (float.Parse(root.RightSon.Value).CompareTo(0) == 0) + { + throw new DivideByZeroTreeException("division by 0"); + } + } + catch (DivideByZeroTreeException exception) + { + Console.WriteLine($"Ошибка: {exception.Message}"); + throw; + } + root.Value = (float.Parse(root.LeftSon.Value) / float.Parse(root.RightSon.Value)).ToString(); + } + } + return float.Parse(root.Value); + } + + /// + /// Function for printing a tree + /// + public string PrintTree() + { + string element = ""; + PrintTreeRecursive(ref element, treeRoot); + return element; + } + + private void PrintTreeRecursive(ref string element, Node? root) + { + if (root == null) + { + return; + } + if (root.Value != null && StringIsOperator(root.Value)) + { + element += "("; + element += root.Value; + PrintTreeRecursive(ref element, root.LeftSon); + PrintTreeRecursive(ref element, root.RightSon); + element += ")"; + + } + else + { + element += root.Value; + element += " "; + } + } + + /// + /// Function for building a tree + /// + /// The expression on the basis of which the tree is built + public void BuildTree(string expression) + { + int index = 0; + Node? root = null; + treeRoot = PrivateBuildTree(expression, ref index, root); + } + + private Node? PrivateBuildTree(string expression, ref int index, Node? node) + { + if (index >= expression.Length) + { + return node; + } + while (expression[index] == '(' || expression[index] == ')' || expression[index] == ' ' && index < expression.Length) + { + index++; + } + if ((CharIsOperator(expression[index]) && index < expression.Length) || (index + 1 < expression.Length + && !IsOperand(expression[index + 1]) && CharIsOperator(expression[index]))) + { + node = InitializeNode("" + expression[index]); + index++; + node.LeftSon = PrivateBuildTree(expression, ref index, node.LeftSon); + node.RightSon = PrivateBuildTree(expression, ref index, node.RightSon); + return node; + } + int newIndex = expression[index] == '-' ? index + 1 : index; + string nodeValue = ""; + while (newIndex < expression.Length && + IsOperand(expression[newIndex])) + { + nodeValue += expression[newIndex]; + newIndex++; + } + Node newNode = expression[index] == '-' ? InitializeNode("-" + nodeValue) : InitializeNode(nodeValue); + index = newIndex; + return newNode; + } + + private Node InitializeNode(string element) + { + Node newNode = new Node(); + newNode.Value = element; + return newNode; + } + + private bool CharIsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; + private bool StringIsOperator(string element) => element == "+" || element == "-" || element == "*" || element == "/"; + private bool IsOperand(char element) => element <= '9' && element >= '0'; +} diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs new file mode 100644 index 0000000..87acbc6 --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs @@ -0,0 +1,30 @@ +using System; + +namespace Tree; + +/// +/// Parse Tree Interface +/// +public interface IParsingTree +{ + /// + /// Function for deleting a tree + /// + public void DeleteTree(); + + /// + /// Function for printing a tree + /// + public string PrintTree(); + + /// + /// Function for building a tree + /// + /// The expression on the basis of which the tree is built + public void BuildTree(string expression); + + /// + /// Function for traversing the tree + /// + public float TreeTraversal(); +} \ No newline at end of file diff --git a/Homework4/ParsingTree/ParsingTree/Solution.cs b/Homework4/ParsingTree/ParsingTree/Solution.cs new file mode 100644 index 0000000..8563792 --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree/Solution.cs @@ -0,0 +1,11 @@ +using System; + + +namespace ParsingTree; + +public class Solution +{ + public static void Main(string[] args) + { + } +} \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs new file mode 100644 index 0000000..bcebc88 --- /dev/null +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -0,0 +1,50 @@ +using NUnit.Framework; +using Tree; +using System; + +namespace TestParsingTree; + +/// +/// A class for testing a parsing tree +/// +public class TestsrsingTree +{ + Tree.ParsingTree? tree; + + [SetUp] + public void Setup() + { + tree = new Tree.ParsingTree(); + } + + [Test] + public void PrintExpressionFromSingleElement() + { + tree?.BuildTree("12"); + Assert.AreEqual("12 ", tree?.PrintTree()); + tree?.DeleteTree(); + } + + [Test] + public void PrintExpressionFromZeroElement() + { + tree?.BuildTree(""); + Assert.AreEqual("", tree?.PrintTree()); + tree?.DeleteTree(); + } + + [Test] + public void PrintExpressionFromRandomElement() + { + tree?.BuildTree("(*(-(*(2 7))12)3)"); + Assert.AreEqual("(*(-(*2 7 )12 )3 )", tree?.PrintTree()); + tree?.DeleteTree(); + } + + [Test] + public void DivideByZeroExpression() + { + tree?.BuildTree("/ 2 0"); + Assert.Throws(() => tree?.TreeTraversal()); + } +} \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.csproj b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.csproj new file mode 100644 index 0000000..18963a6 --- /dev/null +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + From 11d835b94c2820b27ec913a5ff22c42e048396ec Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 23 Mar 2022 23:56:05 +0300 Subject: [PATCH 02/12] creating a class to count the value of an expression --- .../CalculationExpression.sln | 31 +++++++++++++++++++ .../CalculationExpresion.cs | 27 ++++++++++++++++ .../CalculationExpression.csproj | 14 +++++++++ .../CalculationExpression/Solution.cs | 20 ++++++++++++ Homework4/ParsingTree/ParsingTree.sln | 31 ------------------- .../ParsingTree/ParsingTree/Exception.cs | 17 ++++++++++ .../ParsingTree/ParsingTree/ParsingTree.cs | 3 +- .../ParsingTree/ParsingTreeInterface.cs | 4 +-- .../TestParsingTree/TestParsingTree.cs | 2 +- 9 files changed, 114 insertions(+), 35 deletions(-) create mode 100644 Homework4/CalculationExpression/CalculationExpression.sln create mode 100644 Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs create mode 100644 Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj create mode 100644 Homework4/CalculationExpression/CalculationExpression/Solution.cs delete mode 100644 Homework4/ParsingTree/ParsingTree.sln create mode 100644 Homework4/ParsingTree/ParsingTree/Exception.cs diff --git a/Homework4/CalculationExpression/CalculationExpression.sln b/Homework4/CalculationExpression/CalculationExpression.sln new file mode 100644 index 0000000..5fb73b3 --- /dev/null +++ b/Homework4/CalculationExpression/CalculationExpression.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculationExpression", "CalculationExpression\CalculationExpression.csproj", "{F28CD497-C9A1-4AFA-835B-745308E9096C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "..\ParsingTree\ParsingTree\ParsingTree.csproj", "{F7711424-3889-4E40-9CE9-C0BA5B9E9343}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F28CD497-C9A1-4AFA-835B-745308E9096C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F28CD497-C9A1-4AFA-835B-745308E9096C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F28CD497-C9A1-4AFA-835B-745308E9096C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F28CD497-C9A1-4AFA-835B-745308E9096C}.Release|Any CPU.Build.0 = Release|Any CPU + {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8FD715E1-7A53-4187-A7BA-14A278A50D41} + EndGlobalSection +EndGlobal diff --git a/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs b/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs new file mode 100644 index 0000000..d001f9d --- /dev/null +++ b/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs @@ -0,0 +1,27 @@ +using System; +using Tree; + +namespace CalculationExpression; + +public class CalculatExpression +{ + IParsingTree tree = new Tree.ParsingTree(); + public float CountTheExpression(string expression) + { + tree.BuildTree(expression); + float answer = tree.TreeTraversal(); + tree.DeleteTree(); + tree.BuildTree(expression); + return answer; + } + + public void PrintExpression(string expression) + { + tree.PrintTree(); + } + + public void DeleteExpression(string expression) + { + tree.DeleteTree(); + } +} \ No newline at end of file diff --git a/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj b/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj new file mode 100644 index 0000000..54d5fdb --- /dev/null +++ b/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Homework4/CalculationExpression/CalculationExpression/Solution.cs b/Homework4/CalculationExpression/CalculationExpression/Solution.cs new file mode 100644 index 0000000..7586ca9 --- /dev/null +++ b/Homework4/CalculationExpression/CalculationExpression/Solution.cs @@ -0,0 +1,20 @@ +using System; + +using CalculationExpression; + +public class Solution +{ + public static void Main(string[] args) + { + Console.WriteLine("Please, input the expression"); + string? expression = Console.ReadLine(); + if(expression == null) + { + return; + } + CalculatExpression calculationExpression = new CalculatExpression(); + Console.WriteLine(calculationExpression.CountTheExpression(expression)); + calculationExpression.PrintExpression(expression); + calculationExpression.DeleteExpression(expression); + } +} diff --git a/Homework4/ParsingTree/ParsingTree.sln b/Homework4/ParsingTree/ParsingTree.sln deleted file mode 100644 index 69baee1..0000000 --- a/Homework4/ParsingTree/ParsingTree.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.1.32228.430 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{3A685608-3E27-4534-942C-AE951EF76977}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestParsingTree", "TestParsingTree\TestParsingTree.csproj", "{921D199E-504F-47CD-AC97-9C93A98C8B0D}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.Build.0 = Release|Any CPU - {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {DD0BE32A-1D93-45E3-ACD3-92A75D23D824} - EndGlobalSection -EndGlobal diff --git a/Homework4/ParsingTree/ParsingTree/Exception.cs b/Homework4/ParsingTree/ParsingTree/Exception.cs new file mode 100644 index 0000000..d0941ad --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree/Exception.cs @@ -0,0 +1,17 @@ +namespace Tree; + +/// +/// A class for creating custom exceptions +/// +public class InvalidCharacterException : Exception +{ + public InvalidCharacterException(string? message) : base(message) { } +} + +/// +/// A class for creating custom exceptions +/// +public class DivideByZeroTreeException : Exception +{ + public DivideByZeroTreeException(string? message) : base(message) { } +} diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index f63e897..c413f80 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -168,7 +168,8 @@ public void BuildTree(string expression) nodeValue += expression[newIndex]; newIndex++; } - Node newNode = expression[index] == '-' ? InitializeNode("-" + nodeValue) : InitializeNode(nodeValue); + Node newNode = + expression[index] == '-' ? InitializeNode("-" + nodeValue) : InitializeNode(nodeValue); index = newIndex; return newNode; } diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs index 87acbc6..0210a03 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs @@ -5,8 +5,8 @@ namespace Tree; /// /// Parse Tree Interface /// -public interface IParsingTree -{ + public interface IParsingTree + { /// /// Function for deleting a tree /// diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs index bcebc88..a9a3a48 100644 --- a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -47,4 +47,4 @@ public void DivideByZeroExpression() tree?.BuildTree("/ 2 0"); Assert.Throws(() => tree?.TreeTraversal()); } -} \ No newline at end of file +} From 1a6b78b9c5f505ac1577aa32748f5eb0c3649c2d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 11 Apr 2022 20:42:37 +0300 Subject: [PATCH 03/12] Now the classes of operators and operands count themselves and output --- .../CalculationExpression.sln | 31 -- .../CalculationExpresion.cs | 27 -- .../CalculationExpression.csproj | 14 - .../CalculationExpression/Solution.cs | 20 -- Homework4/ParsingTree/ParsingTree.sln | 31 ++ .../ParsingTree/ParsingTree/Exception.cs | 17 - .../ParsingTree/ParsingTree/ParsingTree.cs | 292 +++++++++++------- .../ParsingTree/ParsingTreeInterface.cs | 15 +- Homework4/ParsingTree/ParsingTree/Solution.cs | 11 - .../TestParsingTree/TestParsingTree.cs | 33 +- 10 files changed, 211 insertions(+), 280 deletions(-) delete mode 100644 Homework4/CalculationExpression/CalculationExpression.sln delete mode 100644 Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs delete mode 100644 Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj delete mode 100644 Homework4/CalculationExpression/CalculationExpression/Solution.cs create mode 100644 Homework4/ParsingTree/ParsingTree.sln delete mode 100644 Homework4/ParsingTree/ParsingTree/Exception.cs delete mode 100644 Homework4/ParsingTree/ParsingTree/Solution.cs diff --git a/Homework4/CalculationExpression/CalculationExpression.sln b/Homework4/CalculationExpression/CalculationExpression.sln deleted file mode 100644 index 5fb73b3..0000000 --- a/Homework4/CalculationExpression/CalculationExpression.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.1.32228.430 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculationExpression", "CalculationExpression\CalculationExpression.csproj", "{F28CD497-C9A1-4AFA-835B-745308E9096C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "..\ParsingTree\ParsingTree\ParsingTree.csproj", "{F7711424-3889-4E40-9CE9-C0BA5B9E9343}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {F28CD497-C9A1-4AFA-835B-745308E9096C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F28CD497-C9A1-4AFA-835B-745308E9096C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F28CD497-C9A1-4AFA-835B-745308E9096C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F28CD497-C9A1-4AFA-835B-745308E9096C}.Release|Any CPU.Build.0 = Release|Any CPU - {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F7711424-3889-4E40-9CE9-C0BA5B9E9343}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {8FD715E1-7A53-4187-A7BA-14A278A50D41} - EndGlobalSection -EndGlobal diff --git a/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs b/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs deleted file mode 100644 index d001f9d..0000000 --- a/Homework4/CalculationExpression/CalculationExpression/CalculationExpresion.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Tree; - -namespace CalculationExpression; - -public class CalculatExpression -{ - IParsingTree tree = new Tree.ParsingTree(); - public float CountTheExpression(string expression) - { - tree.BuildTree(expression); - float answer = tree.TreeTraversal(); - tree.DeleteTree(); - tree.BuildTree(expression); - return answer; - } - - public void PrintExpression(string expression) - { - tree.PrintTree(); - } - - public void DeleteExpression(string expression) - { - tree.DeleteTree(); - } -} \ No newline at end of file diff --git a/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj b/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj deleted file mode 100644 index 54d5fdb..0000000 --- a/Homework4/CalculationExpression/CalculationExpression/CalculationExpression.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - - - - - - diff --git a/Homework4/CalculationExpression/CalculationExpression/Solution.cs b/Homework4/CalculationExpression/CalculationExpression/Solution.cs deleted file mode 100644 index 7586ca9..0000000 --- a/Homework4/CalculationExpression/CalculationExpression/Solution.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; - -using CalculationExpression; - -public class Solution -{ - public static void Main(string[] args) - { - Console.WriteLine("Please, input the expression"); - string? expression = Console.ReadLine(); - if(expression == null) - { - return; - } - CalculatExpression calculationExpression = new CalculatExpression(); - Console.WriteLine(calculationExpression.CountTheExpression(expression)); - calculationExpression.PrintExpression(expression); - calculationExpression.DeleteExpression(expression); - } -} diff --git a/Homework4/ParsingTree/ParsingTree.sln b/Homework4/ParsingTree/ParsingTree.sln new file mode 100644 index 0000000..69baee1 --- /dev/null +++ b/Homework4/ParsingTree/ParsingTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{3A685608-3E27-4534-942C-AE951EF76977}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestParsingTree", "TestParsingTree\TestParsingTree.csproj", "{921D199E-504F-47CD-AC97-9C93A98C8B0D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A685608-3E27-4534-942C-AE951EF76977}.Release|Any CPU.Build.0 = Release|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {921D199E-504F-47CD-AC97-9C93A98C8B0D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DD0BE32A-1D93-45E3-ACD3-92A75D23D824} + EndGlobalSection +EndGlobal diff --git a/Homework4/ParsingTree/ParsingTree/Exception.cs b/Homework4/ParsingTree/ParsingTree/Exception.cs deleted file mode 100644 index d0941ad..0000000 --- a/Homework4/ParsingTree/ParsingTree/Exception.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Tree; - -/// -/// A class for creating custom exceptions -/// -public class InvalidCharacterException : Exception -{ - public InvalidCharacterException(string? message) : base(message) { } -} - -/// -/// A class for creating custom exceptions -/// -public class DivideByZeroTreeException : Exception -{ - public DivideByZeroTreeException(string? message) : base(message) { } -} diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index c413f80..62b8b8a 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -1,147 +1,146 @@ -using System; +namespace Tree; -namespace Tree; +using System; /// /// Class representing the parse tree /// -public class ParsingTree : IParsingTree +public class ParsingTree { /// /// Class for storing tree nodes /// - public class Node + private interface INode { - private Node? leftSon; - private Node? rightSon; - private string? stringValue; - public Node? LeftSon { get => leftSon; set { leftSon = value; } } - public Node? RightSon { get => rightSon; set { rightSon = value; } } - public string? Value { get => stringValue; set { stringValue = value; } } + public float Count(); + public void Print(); - } - - private Node? treeRoot; - - /// - /// Function for deleting a tree - /// - public void DeleteTree() - { - DeleteTreeRecursive(treeRoot); - } - - private void DeleteTreeRecursive(Node? root) - { - if (root == null) + public class Operand : INode { - return; + public string Value; + public Operand(string element) + { + Value = element; + } + public float Count() + { + return float.Parse(Value); + } + public void Print() + { + Console.Write(Value); + Console.Write(" "); + } } - DeleteTreeRecursive(root.LeftSon); - DeleteTreeRecursive(root.RightSon); - root = null; - } - /// - /// Function for traversing the tree - /// - public float TreeTraversal() - { - return RecursiveTreeTraversal(treeRoot); - } - - private float RecursiveTreeTraversal(Node? root) - { - if (root == null || root.Value == null) - { - return 0; - } - if (root.LeftSon != null) - { - RecursiveTreeTraversal(root.LeftSon); - } - if (root.RightSon != null) + public abstract class Operator : INode { - RecursiveTreeTraversal(root.RightSon); - } - if (root.RightSon != null && root.LeftSon != null && root.LeftSon.Value != null && root.RightSon.Value != null) - { - if (root.Value == "+") + public INode? LeftSon; + public INode? RightSon; + public abstract float Count(); + public abstract void Print(); + + public void OperatorPrintTemplate(string symbol) { - root.Value = (float.Parse(root.LeftSon.Value) + float.Parse(root.RightSon.Value)).ToString(); + Console.Write("("); + Console.Write(symbol); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); } - if (root.Value == "-") + + public class Plus : Operator { - root.Value = (float.Parse(root.LeftSon.Value) - float.Parse(root.RightSon.Value)).ToString(); + public override float Count() + { + if (LeftSon == null || RightSon == null) + { + throw new NullReferenceException(); + } + return LeftSon.Count() + RightSon.Count(); + } + public override void Print() + { + if (LeftSon == null || RightSon == null) + { + return; + } + OperatorPrintTemplate("+"); + } } - if (root.Value == "*") + + public class Minus : Operator { - root.Value = (float.Parse(root.LeftSon.Value) * float.Parse(root.RightSon.Value)).ToString(); + public override float Count() + { + if (LeftSon == null || RightSon == null) + { + throw new NullReferenceException(); + } + return LeftSon.Count() - RightSon.Count(); + } + public override void Print() + { + if (LeftSon == null || RightSon == null) + { + return; + } + OperatorPrintTemplate("-"); + } } - if (root.Value == "/") + + public class Divide : Operator { - try + public override float Count() { - if (float.Parse(root.RightSon.Value).CompareTo(0) == 0) + if (LeftSon == null || RightSon == null) { - throw new DivideByZeroTreeException("division by 0"); + throw new NullReferenceException(); } + return LeftSon.Count() / RightSon.Count(); } - catch (DivideByZeroTreeException exception) + public override void Print() { - Console.WriteLine($"Ошибка: {exception.Message}"); - throw; + if (LeftSon == null || RightSon == null) + { + return; + } + OperatorPrintTemplate("/"); } - root.Value = (float.Parse(root.LeftSon.Value) / float.Parse(root.RightSon.Value)).ToString(); } - } - return float.Parse(root.Value); - } - /// - /// Function for printing a tree - /// - public string PrintTree() - { - string element = ""; - PrintTreeRecursive(ref element, treeRoot); - return element; - } - - private void PrintTreeRecursive(ref string element, Node? root) - { - if (root == null) - { - return; - } - if (root.Value != null && StringIsOperator(root.Value)) - { - element += "("; - element += root.Value; - PrintTreeRecursive(ref element, root.LeftSon); - PrintTreeRecursive(ref element, root.RightSon); - element += ")"; - - } - else - { - element += root.Value; - element += " "; + public class Multiply : Operator + { + public override float Count() + { + if (LeftSon == null || RightSon == null) + { + throw new NullReferenceException(); + } + return LeftSon.Count() * RightSon.Count(); + } + public override void Print() + { + if (LeftSon == null || RightSon == null) + { + return; + } + OperatorPrintTemplate("*"); + } + } } } - /// - /// Function for building a tree - /// - /// The expression on the basis of which the tree is built + INode? treeRoot; + public void BuildTree(string expression) { int index = 0; - Node? root = null; + INode? root = null; treeRoot = PrivateBuildTree(expression, ref index, root); } - private Node? PrivateBuildTree(string expression, ref int index, Node? node) + private INode? PrivateBuildTree(string expression, ref int index, INode? node) { if (index >= expression.Length) { @@ -154,10 +153,7 @@ public void BuildTree(string expression) if ((CharIsOperator(expression[index]) && index < expression.Length) || (index + 1 < expression.Length && !IsOperand(expression[index + 1]) && CharIsOperator(expression[index]))) { - node = InitializeNode("" + expression[index]); - index++; - node.LeftSon = PrivateBuildTree(expression, ref index, node.LeftSon); - node.RightSon = PrivateBuildTree(expression, ref index, node.RightSon); + InitializeNode(expression, ref index, ref node); return node; } int newIndex = expression[index] == '-' ? index + 1 : index; @@ -168,20 +164,84 @@ public void BuildTree(string expression) nodeValue += expression[newIndex]; newIndex++; } - Node newNode = - expression[index] == '-' ? InitializeNode("-" + nodeValue) : InitializeNode(nodeValue); + INode? newNode = null; + int x = nodeValue.Length - 1; + if (expression[index] == '-') + { + InitializeNode("-" + nodeValue, ref x, ref newNode); + } + else + { + InitializeNode(nodeValue, ref x, ref newNode); + } index = newIndex; return newNode; } - private Node InitializeNode(string element) + private void InitializeNode(string expression, ref int index, ref INode? node) { - Node newNode = new Node(); - newNode.Value = element; - return newNode; + switch (expression[index]) + { + case '+': + { + node = new INode.Operator.Plus(); + index++; + ((INode.Operator.Plus)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Plus)node).LeftSon); + ((INode.Operator.Plus)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Plus)node).RightSon); + return; + } + case '-': + { + node = new INode.Operator.Minus(); + index++; + ((INode.Operator.Minus)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Minus)node).LeftSon); + ((INode.Operator.Minus)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Minus)node).RightSon); + return; + } + case '*': + { + node = new INode.Operator.Multiply(); + index++; + ((INode.Operator.Multiply)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Multiply)node).LeftSon); + ((INode.Operator.Multiply)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Multiply)node).RightSon); + return; + } + case '/': + { + node = new INode.Operator.Divide(); + index++; + ((INode.Operator.Divide)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Divide)node).LeftSon); + ((INode.Operator.Divide)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Divide)node).RightSon); + return; + } + default: + { + node = new INode.Operand(expression); + index++; + return; + } + } } + public void Print() => treeRoot?.Print(); + public float Count() + { + if (treeRoot == null) + throw new NullReferenceException(); + return treeRoot.Count(); + + } private bool CharIsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; - private bool StringIsOperator(string element) => element == "+" || element == "-" || element == "*" || element == "/"; private bool IsOperand(char element) => element <= '9' && element >= '0'; } + +public class Solution +{ + public static void Main(string[] args) + { + ParsingTree tree = new ParsingTree(); + tree.BuildTree("(* (+ 1 1) 2)"); + tree.Print(); + Console.Write(tree.Count()); + } +} diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs index 0210a03..9b48175 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs @@ -5,26 +5,17 @@ namespace Tree; /// /// Parse Tree Interface /// - public interface IParsingTree - { - /// - /// Function for deleting a tree - /// - public void DeleteTree(); +public interface IParsingTree +{ /// /// Function for printing a tree /// - public string PrintTree(); + public void PrintTree(); /// /// Function for building a tree /// /// The expression on the basis of which the tree is built public void BuildTree(string expression); - - /// - /// Function for traversing the tree - /// - public float TreeTraversal(); } \ No newline at end of file diff --git a/Homework4/ParsingTree/ParsingTree/Solution.cs b/Homework4/ParsingTree/ParsingTree/Solution.cs deleted file mode 100644 index 8563792..0000000 --- a/Homework4/ParsingTree/ParsingTree/Solution.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - - -namespace ParsingTree; - -public class Solution -{ - public static void Main(string[] args) - { - } -} \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs index a9a3a48..4038f61 100644 --- a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -16,35 +16,4 @@ public void Setup() { tree = new Tree.ParsingTree(); } - - [Test] - public void PrintExpressionFromSingleElement() - { - tree?.BuildTree("12"); - Assert.AreEqual("12 ", tree?.PrintTree()); - tree?.DeleteTree(); - } - - [Test] - public void PrintExpressionFromZeroElement() - { - tree?.BuildTree(""); - Assert.AreEqual("", tree?.PrintTree()); - tree?.DeleteTree(); - } - - [Test] - public void PrintExpressionFromRandomElement() - { - tree?.BuildTree("(*(-(*(2 7))12)3)"); - Assert.AreEqual("(*(-(*2 7 )12 )3 )", tree?.PrintTree()); - tree?.DeleteTree(); - } - - [Test] - public void DivideByZeroExpression() - { - tree?.BuildTree("/ 2 0"); - Assert.Throws(() => tree?.TreeTraversal()); - } -} +} \ No newline at end of file From 1871fcb8655e877eee6b241172ab5207e58adbf6 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 11 Apr 2022 22:40:31 +0300 Subject: [PATCH 04/12] Add comments and Tests --- .../ParsingTree/ParsingTree/ParsingTree.cs | 135 +++++++++++------- .../ParsingTree/ParsingTree.csproj | 2 +- .../ParsingTree/ParsingTreeInterface.cs | 12 +- .../ParsingTree/TestParsingTree/FirstTest.txt | 1 + .../TestParsingTree/TestParsingTree.cs | 34 ++++- 5 files changed, 126 insertions(+), 58 deletions(-) create mode 100644 Homework4/ParsingTree/TestParsingTree/FirstTest.txt diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index 62b8b8a..81d4b9c 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -1,4 +1,4 @@ -namespace Tree; +namespace ParsingTree; using System; @@ -8,38 +8,53 @@ public class ParsingTree { /// - /// Class for storing tree nodes + /// abstract nested class for dividing node into operators and operands for building a parse tree /// - private interface INode + private abstract class Node { - public float Count(); - public void Print(); + // Abstract method for counting each operator or operand + public abstract float Count(); - public class Operand : INode + // Abstract method for printing each operator or operand + public abstract void Print(); + + /// + /// A class representing operands + /// + public class Operand : Node { public string Value; public Operand(string element) { Value = element; } - public float Count() + + // The operand class calculates the value for them and returns it + public override float Count() { return float.Parse(Value); } - public void Print() + + // The operand class can print the values of operands + public override void Print() { Console.Write(Value); Console.Write(" "); } } - public abstract class Operator : INode + /// + /// A class representing operators + /// + public abstract class Operator : Node { - public INode? LeftSon; - public INode? RightSon; - public abstract float Count(); - public abstract void Print(); + // Each operator , unlike an operand, has a right and a left son + public Node? LeftSon; + public Node? RightSon; + + // There will be only 4 operators, so the value field does not make sense + // Template for printing operators public void OperatorPrintTemplate(string symbol) { Console.Write("("); @@ -49,6 +64,7 @@ public void OperatorPrintTemplate(string symbol) Console.Write(")"); } + // A class representing the operator + public class Plus : Operator { public override float Count() @@ -59,6 +75,7 @@ public override float Count() } return LeftSon.Count() + RightSon.Count(); } + public override void Print() { if (LeftSon == null || RightSon == null) @@ -69,6 +86,7 @@ public override void Print() } } + // A class representing the operator - public class Minus : Operator { public override float Count() @@ -79,6 +97,7 @@ public override float Count() } return LeftSon.Count() - RightSon.Count(); } + public override void Print() { if (LeftSon == null || RightSon == null) @@ -89,6 +108,7 @@ public override void Print() } } + // A class representing the operator / public class Divide : Operator { public override float Count() @@ -99,6 +119,7 @@ public override float Count() } return LeftSon.Count() / RightSon.Count(); } + public override void Print() { if (LeftSon == null || RightSon == null) @@ -109,7 +130,8 @@ public override void Print() } } - public class Multiply : Operator + // A class representing the operator * + public class Multiplication : Operator { public override float Count() { @@ -119,6 +141,7 @@ public override float Count() } return LeftSon.Count() * RightSon.Count(); } + public override void Print() { if (LeftSon == null || RightSon == null) @@ -131,40 +154,53 @@ public override void Print() } } - INode? treeRoot; + Node? treeRoot; + /// + /// Function for building a tree + /// + /// The expression that needs to be calculated public void BuildTree(string expression) { int index = 0; - INode? root = null; + Node? root = null; treeRoot = PrivateBuildTree(expression, ref index, root); } - private INode? PrivateBuildTree(string expression, ref int index, INode? node) + // Auxiliary function for building a tree + private Node? PrivateBuildTree(string expression, ref int index, Node? node) { if (index >= expression.Length) { return node; } + + // Skip the characters we don't need while (expression[index] == '(' || expression[index] == ')' || expression[index] == ' ' && index < expression.Length) { index++; } - if ((CharIsOperator(expression[index]) && index < expression.Length) || (index + 1 < expression.Length - && !IsOperand(expression[index + 1]) && CharIsOperator(expression[index]))) + + // The condition in order to avoid confusion, for example, with 4 -5 and 4 - 5 + if (index < expression.Length - 1 && !IsOperand(expression[index + 1]) && CharIsOperator(expression[index])) { InitializeNode(expression, ref index, ref node); return node; } + + // The number could be negative int newIndex = expression[index] == '-' ? index + 1 : index; string nodeValue = ""; - while (newIndex < expression.Length && - IsOperand(expression[newIndex])) + while (newIndex < expression.Length && IsOperand(expression[newIndex])) { nodeValue += expression[newIndex]; newIndex++; } - INode? newNode = null; + Node? newNode = null; + + //This unused variable x is needed in order to call the function, + //and it is the operand that is initialized, + //because the last character of the number cannot be an operator (the file is considered correct int x = nodeValue.Length - 1; if (expression[index] == '-') { @@ -178,70 +214,71 @@ public void BuildTree(string expression) return newNode; } - private void InitializeNode(string expression, ref int index, ref INode? node) + // A function for initializing nodes depending on which operator or operator is a string + private void InitializeNode(string expression, ref int index, ref Node? node) { switch (expression[index]) { case '+': { - node = new INode.Operator.Plus(); + node = new Node.Operator.Plus(); index++; - ((INode.Operator.Plus)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Plus)node).LeftSon); - ((INode.Operator.Plus)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Plus)node).RightSon); + ((Node.Operator.Plus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Plus)node).LeftSon); + ((Node.Operator.Plus)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Plus)node).RightSon); return; } case '-': { - node = new INode.Operator.Minus(); + node = new Node.Operator.Minus(); index++; - ((INode.Operator.Minus)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Minus)node).LeftSon); - ((INode.Operator.Minus)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Minus)node).RightSon); + ((Node.Operator.Minus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Minus)node).LeftSon); + ((Node.Operator.Minus)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Minus)node).RightSon); return; } case '*': { - node = new INode.Operator.Multiply(); + node = new Node.Operator.Multiplication(); index++; - ((INode.Operator.Multiply)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Multiply)node).LeftSon); - ((INode.Operator.Multiply)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Multiply)node).RightSon); + ((Node.Operator.Multiplication)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Multiplication)node).LeftSon); + ((Node.Operator.Multiplication)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Multiplication)node).RightSon); return; } case '/': { - node = new INode.Operator.Divide(); + node = new Node.Operator.Divide(); index++; - ((INode.Operator.Divide)node).LeftSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Divide)node).LeftSon); - ((INode.Operator.Divide)node).RightSon = PrivateBuildTree(expression, ref index, ((INode.Operator.Divide)node).RightSon); + ((Node.Operator.Divide)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Divide)node).LeftSon); + ((Node.Operator.Divide)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Divide)node).RightSon); return; } default: { - node = new INode.Operand(expression); + node = new Node.Operand(expression); index++; return; } } } + /// + /// Function for printing a tree + /// public void Print() => treeRoot?.Print(); + + /// + /// Function for calculating the value of an expression + /// + /// value of an expression public float Count() { if (treeRoot == null) + { throw new NullReferenceException(); + } return treeRoot.Count(); - } - private bool CharIsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; - private bool IsOperand(char element) => element <= '9' && element >= '0'; -} -public class Solution -{ - public static void Main(string[] args) - { - ParsingTree tree = new ParsingTree(); - tree.BuildTree("(* (+ 1 1) 2)"); - tree.Print(); - Console.Write(tree.Count()); - } + private static bool CharIsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; + + private static bool IsOperand(char element) => element <= '9' && element >= '0'; } diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj index 74abf5c..16e62dd 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj @@ -1,7 +1,7 @@ - Exe + Library net6.0 enable enable diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs index 9b48175..088b99d 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs @@ -1,6 +1,4 @@ -using System; - -namespace Tree; +namespace ParsingTree; /// /// Parse Tree Interface @@ -11,11 +9,17 @@ public interface IParsingTree /// /// Function for printing a tree /// - public void PrintTree(); + public void Print(); /// /// Function for building a tree /// /// The expression on the basis of which the tree is built public void BuildTree(string expression); + + /// + /// Function for calculating the value of an expression + /// + /// value of an expression + public float Count(); } \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/FirstTest.txt b/Homework4/ParsingTree/TestParsingTree/FirstTest.txt new file mode 100644 index 0000000..40914e1 --- /dev/null +++ b/Homework4/ParsingTree/TestParsingTree/FirstTest.txt @@ -0,0 +1 @@ +(* (+ 1 1) 2) \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs index 4038f61..9008d95 100644 --- a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -1,19 +1,45 @@ +namespace TestParsingTree; + using NUnit.Framework; -using Tree; +using ParsingTree; +using System.IO; using System; -namespace TestParsingTree; /// /// A class for testing a parsing tree /// public class TestsrsingTree { - Tree.ParsingTree? tree; + ParsingTree tree = new(); [SetUp] public void Setup() { - tree = new Tree.ParsingTree(); + tree = new (); + } + + [Test] + public void ShouldExpected4WhenExpressionEqual4() + { + string stringToConvert = File.ReadAllText("..//..//..//FirstTest.txt"); + tree.BuildTree(stringToConvert); + Assert.AreEqual(4 , tree.Count()); + } + + [Test] + public void ShouldExpectedOneNumberWhenExpressionContainsOnlyOneNumber() + { + string stringToConvert = "12"; + tree.BuildTree(stringToConvert); + Assert.AreEqual(12, tree.Count()); + } + + [Test] + public void ShouldExpectedThrowsNullReferenceExceptionWhenExpressionIsEmptyString() + { + string stringToConvert = ""; + tree.BuildTree(stringToConvert); + Assert.Throws(() => tree.Count()); } } \ No newline at end of file From 19a2d4661ace7466222e64992ff65810ad38083c Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Thu, 21 Apr 2022 03:08:47 +0300 Subject: [PATCH 05/12] Changing the code by style guide and add test --- .../ParsingTree/ParsingTree/ParsingTree.cs | 24 ++++++++++++++----- .../TestParsingTree/TestParsingTree.cs | 12 ++++++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index 81d4b9c..ed4ca05 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -23,7 +23,7 @@ private abstract class Node /// public class Operand : Node { - public string Value; + private readonly string Value; public Operand(string element) { Value = element; @@ -73,6 +73,7 @@ public override float Count() { throw new NullReferenceException(); } + return LeftSon.Count() + RightSon.Count(); } @@ -82,6 +83,7 @@ public override void Print() { return; } + OperatorPrintTemplate("+"); } } @@ -95,6 +97,7 @@ public override float Count() { throw new NullReferenceException(); } + return LeftSon.Count() - RightSon.Count(); } @@ -104,6 +107,7 @@ public override void Print() { return; } + OperatorPrintTemplate("-"); } } @@ -117,7 +121,14 @@ public override float Count() { throw new NullReferenceException(); } - return LeftSon.Count() / RightSon.Count(); + + float rightSonValue = RightSon.Count(); + if (Math.Abs(rightSonValue - 0) < 0.0000000000000000000000000001) + { + throw new DivideByZeroException(); + } + + return LeftSon.Count() / rightSonValue; } public override void Print() @@ -126,6 +137,7 @@ public override void Print() { return; } + OperatorPrintTemplate("/"); } } @@ -163,8 +175,8 @@ public override void Print() public void BuildTree(string expression) { int index = 0; - Node? root = null; - treeRoot = PrivateBuildTree(expression, ref index, root); + Node? node = null; + treeRoot = PrivateBuildTree(expression, ref index, node); } // Auxiliary function for building a tree @@ -182,7 +194,7 @@ public void BuildTree(string expression) } // The condition in order to avoid confusion, for example, with 4 -5 and 4 - 5 - if (index < expression.Length - 1 && !IsOperand(expression[index + 1]) && CharIsOperator(expression[index])) + if (index < expression.Length - 1 && !IsOperand(expression[index + 1]) && IsOperator(expression[index])) { InitializeNode(expression, ref index, ref node); return node; @@ -278,7 +290,7 @@ public float Count() return treeRoot.Count(); } - private static bool CharIsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; + private static bool IsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; private static bool IsOperand(char element) => element <= '9' && element >= '0'; } diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs index 9008d95..1d72939 100644 --- a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -16,7 +16,7 @@ public class TestsrsingTree [SetUp] public void Setup() { - tree = new (); + tree = new(); } [Test] @@ -36,10 +36,18 @@ public void ShouldExpectedOneNumberWhenExpressionContainsOnlyOneNumber() } [Test] - public void ShouldExpectedThrowsNullReferenceExceptionWhenExpressionIsEmptyString() + public void ShouldThrowsNullReferenceExceptionWhenExpressionIsEmptyString() { string stringToConvert = ""; tree.BuildTree(stringToConvert); Assert.Throws(() => tree.Count()); } + + [Test] + public void ThrowsDivideByZeroExceptionWhenExpressionContainsDivisionByZero() + { + string stringToConvert = "/ 1 (- 2 2)"; + tree.BuildTree(stringToConvert); + Assert.Throws(() => tree.Count()); + } } \ No newline at end of file From ce0ebcf8ee3f2d783225ae283884a5725dc1a375 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 1 Jun 2022 00:17:32 +0300 Subject: [PATCH 06/12] Changed comments, structure of nested classes --- .../ParsingTree/ParsingTree/ParsingTree.cs | 345 +++++++++--------- .../ParsingTree/ParsingTree.csproj | 2 +- .../ParsingTree/ParsingTreeInterface.cs | 25 -- .../TestParsingTree/TestParsingTree.cs | 7 +- 4 files changed, 185 insertions(+), 194 deletions(-) delete mode 100644 Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index ed4ca05..769a73b 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -7,166 +7,175 @@ /// public class ParsingTree { - /// - /// abstract nested class for dividing node into operators and operands for building a parse tree - /// private abstract class Node { - // Abstract method for counting each operator or operand + /// + /// Abstract method for counting each operator or operand + /// + /// Operator or operand value public abstract float Count(); - // Abstract method for printing each operator or operand + /// + /// Abstract method for printing each operator or operand + /// public abstract void Print(); + } + + /// + /// A class representing operands + /// + private class Operand : Node + { + private readonly string Value; + + public Operand(string element) + { + Value = element; + } + + /// + /// The operand class calculates the value for them and returns it + /// + /// Operand value + public override float Count() => float.Parse(Value); /// - /// A class representing operands + /// The operand class can print the values of operands /// - public class Operand : Node + public override void Print() { - private readonly string Value; - public Operand(string element) - { - Value = element; - } + Console.Write(Value); + Console.Write(" "); + } + } - // The operand class calculates the value for them and returns it - public override float Count() - { - return float.Parse(Value); - } + /// + /// A class representing operators + /// + private abstract class Operator : Node + { + public Node? LeftSon; + public Node? RightSon; - // The operand class can print the values of operands - public override void Print() - { - Console.Write(Value); - Console.Write(" "); - } - } /// - /// A class representing operators + /// Void for printing operators /// - public abstract class Operator : Node - { - // Each operator , unlike an operand, has a right and a left son - public Node? LeftSon; - public Node? RightSon; + public abstract void Symbol(); - // There will be only 4 operators, so the value field does not make sense + public override void Print() + { + Symbol(); + } + } - // Template for printing operators - public void OperatorPrintTemplate(string symbol) + /// + /// A class representing the operator + + /// + private class Plus : Operator + { + public override float Count() + { + if (LeftSon == null || RightSon == null) { - Console.Write("("); - Console.Write(symbol); - LeftSon?.Print(); - RightSon?.Print(); - Console.Write(")"); + throw new InvalidOperationException(); } - // A class representing the operator + - public class Plus : Operator - { - public override float Count() - { - if (LeftSon == null || RightSon == null) - { - throw new NullReferenceException(); - } - - return LeftSon.Count() + RightSon.Count(); - } - - public override void Print() - { - if (LeftSon == null || RightSon == null) - { - return; - } + return LeftSon.Count() + RightSon.Count(); + } - OperatorPrintTemplate("+"); - } - } + public override void Symbol() + { + Console.Write("("); + Console.Write("+"); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); + } + } - // A class representing the operator - - public class Minus : Operator + /// + /// A class representing the operator - + /// + private class Minus : Operator + { + public override float Count() + { + // But if we consider the input file to be correct, then such a situation should not arise + if (LeftSon == null || RightSon == null) { - public override float Count() - { - if (LeftSon == null || RightSon == null) - { - throw new NullReferenceException(); - } + throw new InvalidOperationException(); + } - return LeftSon.Count() - RightSon.Count(); - } + return LeftSon.Count() - RightSon.Count(); + } - public override void Print() - { - if (LeftSon == null || RightSon == null) - { - return; - } + public override void Symbol() + { + Console.Write("("); + Console.Write("-"); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); + } + } - OperatorPrintTemplate("-"); - } + /// + /// A class representing the operator / + /// + private class Divide : Operator + { + public override float Count() + { + if (LeftSon == null || RightSon == null) + { + throw new InvalidOperationException(); } - // A class representing the operator / - public class Divide : Operator + float rightSonValue = RightSon.Count(); + if (Math.Abs(rightSonValue - 0) < 0.0000000000000000000000000001) { - public override float Count() - { - if (LeftSon == null || RightSon == null) - { - throw new NullReferenceException(); - } - - float rightSonValue = RightSon.Count(); - if (Math.Abs(rightSonValue - 0) < 0.0000000000000000000000000001) - { - throw new DivideByZeroException(); - } - - return LeftSon.Count() / rightSonValue; - } + throw new DivideByZeroException(); + } - public override void Print() - { - if (LeftSon == null || RightSon == null) - { - return; - } + return LeftSon.Count() / rightSonValue; + } - OperatorPrintTemplate("/"); - } - } + public override void Symbol() + { + Console.Write("("); + Console.Write("/"); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); + } + } - // A class representing the operator * - public class Multiplication : Operator + /// + /// A class representing the operator * + /// + private class Multiplication : Operator + { + public override float Count() + { + if (LeftSon == null || RightSon == null) { - public override float Count() - { - if (LeftSon == null || RightSon == null) - { - throw new NullReferenceException(); - } - return LeftSon.Count() * RightSon.Count(); - } - - public override void Print() - { - if (LeftSon == null || RightSon == null) - { - return; - } - OperatorPrintTemplate("*"); - } + throw new InvalidOperationException(); } + return LeftSon.Count() * RightSon.Count(); + } + + public override void Symbol() + { + Console.Write("("); + Console.Write(""); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); } } - Node? treeRoot; + private Node? treeRoot; /// /// Function for building a tree @@ -179,7 +188,9 @@ public void BuildTree(string expression) treeRoot = PrivateBuildTree(expression, ref index, node); } - // Auxiliary function for building a tree + /// + /// Auxiliary function for building a tree + /// private Node? PrivateBuildTree(string expression, ref int index, Node? node) { if (index >= expression.Length) @@ -210,10 +221,11 @@ public void BuildTree(string expression) } Node? newNode = null; - //This unused variable x is needed in order to call the function, - //and it is the operand that is initialized, - //because the last character of the number cannot be an operator (the file is considered correct + // This unused variable x is needed in order to call the function, + // And it is the operand that is initialized, + // Because the last character of the number cannot be an operator (the file is considered correct int x = nodeValue.Length - 1; + if (expression[index] == '-') { InitializeNode("-" + nodeValue, ref x, ref newNode); @@ -222,56 +234,60 @@ public void BuildTree(string expression) { InitializeNode(nodeValue, ref x, ref newNode); } + index = newIndex; return newNode; } - // A function for initializing nodes depending on which operator or operator is a string + /// + /// A function for initializing nodes depending on which operator or operator is a string + /// private void InitializeNode(string expression, ref int index, ref Node? node) { switch (expression[index]) { case '+': - { - node = new Node.Operator.Plus(); - index++; - ((Node.Operator.Plus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Plus)node).LeftSon); - ((Node.Operator.Plus)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Plus)node).RightSon); - return; - } + { + node = new Plus(); + index++; + ((Plus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Plus)node).LeftSon); + ((Plus)node).RightSon = PrivateBuildTree(expression, ref index, ((Plus)node).RightSon); + return; + } case '-': - { - node = new Node.Operator.Minus(); - index++; - ((Node.Operator.Minus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Minus)node).LeftSon); - ((Node.Operator.Minus)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Minus)node).RightSon); - return; - } + { + node = new Minus(); + index++; + ((Minus)node).LeftSon = PrivateBuildTree(expression, ref index, ((Minus)node).LeftSon); + ((Minus)node).RightSon = PrivateBuildTree(expression, ref index, ((Minus)node).RightSon); + return; + } case '*': - { - node = new Node.Operator.Multiplication(); - index++; - ((Node.Operator.Multiplication)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Multiplication)node).LeftSon); - ((Node.Operator.Multiplication)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Multiplication)node).RightSon); - return; - } + { + node = new Multiplication(); + index++; + ((Multiplication)node).LeftSon = PrivateBuildTree(expression, ref index, ((Multiplication)node).LeftSon); + ((Multiplication)node).RightSon = PrivateBuildTree(expression, ref index, ((Multiplication)node).RightSon); + return; + } case '/': - { - node = new Node.Operator.Divide(); - index++; - ((Node.Operator.Divide)node).LeftSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Divide)node).LeftSon); - ((Node.Operator.Divide)node).RightSon = PrivateBuildTree(expression, ref index, ((Node.Operator.Divide)node).RightSon); - return; - } + { + node = new Divide(); + index++; + ((Divide)node).LeftSon = PrivateBuildTree(expression, ref index, ((Divide)node).LeftSon); + ((Divide)node).RightSon = PrivateBuildTree(expression, ref index, ((Divide)node).RightSon); + return; + } default: - { - node = new Node.Operand(expression); - index++; - return; - } + { + node = new Operand(expression); + index++; + return; + } } } + /// /// Function for printing a tree /// @@ -285,12 +301,13 @@ public float Count() { if (treeRoot == null) { - throw new NullReferenceException(); + throw new InvalidOperationException(); } + return treeRoot.Count(); } private static bool IsOperator(char element) => element == '+' || element == '-' || element == '*' || element == '/'; private static bool IsOperand(char element) => element <= '9' && element >= '0'; -} +} \ No newline at end of file diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj index 16e62dd..644c780 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.csproj @@ -1,4 +1,4 @@ - + Library diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs b/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs deleted file mode 100644 index 088b99d..0000000 --- a/Homework4/ParsingTree/ParsingTree/ParsingTreeInterface.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace ParsingTree; - -/// -/// Parse Tree Interface -/// -public interface IParsingTree -{ - - /// - /// Function for printing a tree - /// - public void Print(); - - /// - /// Function for building a tree - /// - /// The expression on the basis of which the tree is built - public void BuildTree(string expression); - - /// - /// Function for calculating the value of an expression - /// - /// value of an expression - public float Count(); -} \ No newline at end of file diff --git a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs index 1d72939..0e4ed91 100644 --- a/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs +++ b/Homework4/ParsingTree/TestParsingTree/TestParsingTree.cs @@ -5,13 +5,12 @@ namespace TestParsingTree; using System.IO; using System; - /// /// A class for testing a parsing tree /// -public class TestsrsingTree +public class TestParsingTree { - ParsingTree tree = new(); + private ParsingTree tree = new(); [SetUp] public void Setup() @@ -40,7 +39,7 @@ public void ShouldThrowsNullReferenceExceptionWhenExpressionIsEmptyString() { string stringToConvert = ""; tree.BuildTree(stringToConvert); - Assert.Throws(() => tree.Count()); + Assert.Throws(() => tree.Count()); } [Test] From e0ea5c9e3708575853b87350f85a275baf19b93f Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 1 Jun 2022 22:11:36 +0300 Subject: [PATCH 07/12] the Call super antipattern has been eliminated --- .../ParsingTree/ParsingTree/ParsingTree.cs | 67 +++++++++---------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs index 769a73b..362784b 100644 --- a/Homework4/ParsingTree/ParsingTree/ParsingTree.cs +++ b/Homework4/ParsingTree/ParsingTree/ParsingTree.cs @@ -58,14 +58,15 @@ private abstract class Operator : Node public Node? RightSon; - /// - /// Void for printing operators - /// - public abstract void Symbol(); + public abstract char Symbol { get; set; } public override void Print() { - Symbol(); + Console.Write("("); + Console.Write(Symbol); + LeftSon?.Print(); + RightSon?.Print(); + Console.Write(")"); } } @@ -74,6 +75,11 @@ public override void Print() /// private class Plus : Operator { + public Plus() + { + Symbol = '+'; + } + public override float Count() { if (LeftSon == null || RightSon == null) @@ -84,14 +90,7 @@ public override float Count() return LeftSon.Count() + RightSon.Count(); } - public override void Symbol() - { - Console.Write("("); - Console.Write("+"); - LeftSon?.Print(); - RightSon?.Print(); - Console.Write(")"); - } + public override char Symbol { get; set; } } /// @@ -99,6 +98,11 @@ public override void Symbol() /// private class Minus : Operator { + public Minus() + { + Symbol = '-'; + } + public override float Count() { // But if we consider the input file to be correct, then such a situation should not arise @@ -110,14 +114,7 @@ public override float Count() return LeftSon.Count() - RightSon.Count(); } - public override void Symbol() - { - Console.Write("("); - Console.Write("-"); - LeftSon?.Print(); - RightSon?.Print(); - Console.Write(")"); - } + public override char Symbol { get; set; } } /// @@ -125,6 +122,11 @@ public override void Symbol() /// private class Divide : Operator { + public Divide() + { + Symbol = '/'; + } + public override float Count() { if (LeftSon == null || RightSon == null) @@ -141,14 +143,7 @@ public override float Count() return LeftSon.Count() / rightSonValue; } - public override void Symbol() - { - Console.Write("("); - Console.Write("/"); - LeftSon?.Print(); - RightSon?.Print(); - Console.Write(")"); - } + public override char Symbol { get; set; } } /// @@ -156,6 +151,11 @@ public override void Symbol() /// private class Multiplication : Operator { + public Multiplication() + { + Symbol = '*'; + } + public override float Count() { if (LeftSon == null || RightSon == null) @@ -165,14 +165,7 @@ public override float Count() return LeftSon.Count() * RightSon.Count(); } - public override void Symbol() - { - Console.Write("("); - Console.Write(""); - LeftSon?.Print(); - RightSon?.Print(); - Console.Write(")"); - } + public override char Symbol { get; set; } } private Node? treeRoot; From c87b52f05af6c658f2507a580039004d938e93ef Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:18:34 +0300 Subject: [PATCH 08/12] rename yml --- .github/workflows/ParsingTree.yml | 29 +++++++++++++++++++++++++++++ .github/workflows/dotnet.yml | 23 ----------------------- 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/ParsingTree.yml delete mode 100644 .github/workflows/dotnet.yml diff --git a/.github/workflows/ParsingTree.yml b/.github/workflows/ParsingTree.yml new file mode 100644 index 0000000..b027406 --- /dev/null +++ b/.github/workflows/ParsingTree.yml @@ -0,0 +1,29 @@ +name: Build + +on: [push] + +jobs: + build-Windows: + + runs-on: windows-latest + + @@ -20,4 +20,21 @@ jobs: + - name: Test + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} + + build-Ubuntu: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.x' + - name: Restore + run: for f in $(find . -name "*.sln"); do dotnet restore $f; done + - name: Build + run: for f in $(find . -name "*.sln"); do dotnet build $f; done + - name: Test + run: for f in $(find . -name "*.sln"); do dotnet test $f; done \ No newline at end of file diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml deleted file mode 100644 index 1e6a924..0000000 --- a/.github/workflows/dotnet.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Build - -on: [push] - -jobs: - build: - - runs-on: windows-latest - - steps: - - uses: actions/checkout@v2 - - name: Build - uses: actions/setup-dotnet@v1 - with: - dotnet-version: '6.x' - - name: Restore - run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {nuget restore $file.FullName} - - name: Build - run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet build $file.FullName} - - name: Test - run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} - - From 96769103490a1db55e251ebcecccc08dd72d9796 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:19:54 +0300 Subject: [PATCH 09/12] fixed yml --- .github/workflows/ParsingTree.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ParsingTree.yml b/.github/workflows/ParsingTree.yml index b027406..258a5b4 100644 --- a/.github/workflows/ParsingTree.yml +++ b/.github/workflows/ParsingTree.yml @@ -6,8 +6,6 @@ jobs: build-Windows: runs-on: windows-latest - - @@ -20,4 +20,21 @@ jobs: - name: Test run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} From 08705232f3207730fe03ea6ab4c58e58bb93e35a Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:21:51 +0300 Subject: [PATCH 10/12] fixed yml --- .github/workflows/ParsingTree.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ParsingTree.yml b/.github/workflows/ParsingTree.yml index 258a5b4..7feb982 100644 --- a/.github/workflows/ParsingTree.yml +++ b/.github/workflows/ParsingTree.yml @@ -6,6 +6,7 @@ jobs: build-Windows: runs-on: windows-latest + - name: Test run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} From 47eb28e4a75aa5492cc5fa023b071c03bdc48c59 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:24:47 +0300 Subject: [PATCH 11/12] fixed ci --- .github/workflows/ParsingTree.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ParsingTree.yml b/.github/workflows/ParsingTree.yml index 7feb982..94ac0b1 100644 --- a/.github/workflows/ParsingTree.yml +++ b/.github/workflows/ParsingTree.yml @@ -7,6 +7,16 @@ jobs: runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + - name: Build + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.x' + - name: Restore + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {nuget restore $file.FullName} + - name: Build + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet build $file.FullName} - name: Test run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} From d4f226d0deb4a254324a421cce652e7a0e0ea8d8 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 01:16:50 +0300 Subject: [PATCH 12/12] rename yml --- .github/workflows/{ParsingTree.yml => dotnet.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{ParsingTree.yml => dotnet.yml} (100%) diff --git a/.github/workflows/ParsingTree.yml b/.github/workflows/dotnet.yml similarity index 100% rename from .github/workflows/ParsingTree.yml rename to .github/workflows/dotnet.yml