From f6cce941635ff841e4ce24b7ff6e85a3f895ec4d Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 20:25:21 +0300 Subject: [PATCH 01/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B4=D0=B5=D1=80=D0=B5=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree.sln | 31 +++ ParsingTree/ParsingTree/Divisioncs.cs | 21 ++ .../ParsingTree/InvalidExpressionException.cs | 3 + ParsingTree/ParsingTree/Minus.cs | 20 ++ ParsingTree/ParsingTree/Multiplication.cs | 15 ++ ParsingTree/ParsingTree/Operand.cs | 22 ++ ParsingTree/ParsingTree/Operator.cs | 15 ++ ParsingTree/ParsingTree/ParsingTree.csproj | 10 + ParsingTree/ParsingTree/PartOfExpression.cs | 8 + ParsingTree/ParsingTree/Tree.cs | 223 ++++++++++++++++++ 10 files changed, 368 insertions(+) create mode 100644 ParsingTree/ParsingTree.sln create mode 100644 ParsingTree/ParsingTree/Divisioncs.cs create mode 100644 ParsingTree/ParsingTree/InvalidExpressionException.cs create mode 100644 ParsingTree/ParsingTree/Minus.cs create mode 100644 ParsingTree/ParsingTree/Multiplication.cs create mode 100644 ParsingTree/ParsingTree/Operand.cs create mode 100644 ParsingTree/ParsingTree/Operator.cs create mode 100644 ParsingTree/ParsingTree/ParsingTree.csproj create mode 100644 ParsingTree/ParsingTree/PartOfExpression.cs create mode 100644 ParsingTree/ParsingTree/Tree.cs diff --git a/ParsingTree/ParsingTree.sln b/ParsingTree/ParsingTree.sln new file mode 100644 index 0000000..65be4ae --- /dev/null +++ b/ParsingTree/ParsingTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{E7D3B03D-F74E-4781-9DF2-0CB24880162A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsParsingTree", "TestsParsingTree\TestsParsingTree.csproj", "{B09F32EB-3DF8-4E88-B230-9229ECC98E73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Release|Any CPU.Build.0 = Release|Any CPU + {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4AF94C07-C87B-41B8-9B20-62E8562E599A} + EndGlobalSection +EndGlobal diff --git a/ParsingTree/ParsingTree/Divisioncs.cs b/ParsingTree/ParsingTree/Divisioncs.cs new file mode 100644 index 0000000..1a24394 --- /dev/null +++ b/ParsingTree/ParsingTree/Divisioncs.cs @@ -0,0 +1,21 @@ +namespace ParsingTree; +public class Divisioncs : Operator +{ + double delta = 0.0000001; + + public Divisioncs(char symbol) : base(symbol) {} + + public override double Calcuate(double firstValue, double secondValue) + { + if (secondValue - Math.Abs(secondValue) < delta) + { + throw new ArgumentException(); + } + return firstValue / secondValue; + } + + public override void Print() + { + Console.Write(" / "); + } +} diff --git a/ParsingTree/ParsingTree/InvalidExpressionException.cs b/ParsingTree/ParsingTree/InvalidExpressionException.cs new file mode 100644 index 0000000..9136f55 --- /dev/null +++ b/ParsingTree/ParsingTree/InvalidExpressionException.cs @@ -0,0 +1,3 @@ +namespace ParsingTree; + +public class InvalidExpressionException : Exception {} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Minus.cs b/ParsingTree/ParsingTree/Minus.cs new file mode 100644 index 0000000..12f1ba2 --- /dev/null +++ b/ParsingTree/ParsingTree/Minus.cs @@ -0,0 +1,20 @@ +namespace ParsingTree; + +// A class that implements subtraction +public class Minus : Operator +{ + // Keeps a minus in itself + public Minus(char symbol) : base(symbol) {} + + // Counts the difference of two numbers + public override double Calcuate(double firstValue, double secondValue) + { + return secondValue - firstValue; + } + + // Prints a minus sign with spaces + public override void Print() + { + Console.Write(" - "); + } +} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Multiplication.cs b/ParsingTree/ParsingTree/Multiplication.cs new file mode 100644 index 0000000..32eabab --- /dev/null +++ b/ParsingTree/ParsingTree/Multiplication.cs @@ -0,0 +1,15 @@ +namespace ParsingTree; +public class Multiplication : Operator +{ + public Multiplication(char symbol) : base(symbol) {} + + public override double Calcuate(double firstValue, double secondValue) + { + return firstValue * secondValue; + } + + public override void Print() + { + Console.Write(" * "); + } +} diff --git a/ParsingTree/ParsingTree/Operand.cs b/ParsingTree/ParsingTree/Operand.cs new file mode 100644 index 0000000..f2dc190 --- /dev/null +++ b/ParsingTree/ParsingTree/Operand.cs @@ -0,0 +1,22 @@ +namespace ParsingTree; + +public class Operand : PartOfExpression +{ + public double Calcuate(double firstValue, double secondValue) + { + return Number; + } + + public void Print() + { + Console.Write(Number); + Console.Write(' '); + } + + public Operand(double number) + { + Number = number; + } + + public double Number { get; set; } +} diff --git a/ParsingTree/ParsingTree/Operator.cs b/ParsingTree/ParsingTree/Operator.cs new file mode 100644 index 0000000..7e606fd --- /dev/null +++ b/ParsingTree/ParsingTree/Operator.cs @@ -0,0 +1,15 @@ +namespace ParsingTree; + +abstract public class Operator : PartOfExpression +{ + public abstract double Calcuate(double firstValue, double secondValue); + + public abstract void Print(); + + public Operator(char symbol) + { + Symbol = symbol; + } + + public char Symbol { get; set; } +} diff --git a/ParsingTree/ParsingTree/ParsingTree.csproj b/ParsingTree/ParsingTree/ParsingTree.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/ParsingTree/ParsingTree/ParsingTree.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/ParsingTree/ParsingTree/PartOfExpression.cs b/ParsingTree/ParsingTree/PartOfExpression.cs new file mode 100644 index 0000000..e85e020 --- /dev/null +++ b/ParsingTree/ParsingTree/PartOfExpression.cs @@ -0,0 +1,8 @@ +namespace ParsingTree; + +public interface PartOfExpression +{ + public double Calcuate(double firstValue, double secondValue); + + public void Print(); +} diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs new file mode 100644 index 0000000..5e5867b --- /dev/null +++ b/ParsingTree/ParsingTree/Tree.cs @@ -0,0 +1,223 @@ +namespace ParsingTree; + +// The container is a "tree" with a hierarchical structure +public class Tree +{ + private Node Root { get; set; } + + private void AddSymbolToTree(char symbol) + { + if (Root == null) + { + Root = new Node(); + switch (symbol) + { + case '-': + Root.Symbol = new Minus(symbol); + break; + case '+': + Root.Symbol = new Plus(symbol); + break; + case '*': + Root.Symbol = new Multiplication(symbol); + break; + case '/': + Root.Symbol = new Divisioncs(symbol); + break; + } + Root.Symbol.Symbol = symbol; + Root.Value = new Operand(0); + Root.Left = new Node(); + Root.Right = new Node(); + Root.Right.Value = new Operand(0); + Root.Left.Value = new Operand(0); + Root.IsEmpty = false; + Root.Size += 2; + return; + } + var walker = Root; + while (walker.Left != null) + { + walker = walker.Left; + } + switch (symbol) + { + case '-': + walker.Symbol = new Minus(symbol); + break; + case '+': + walker.Symbol = new Plus(symbol); + break; + case '*': + walker.Symbol = new Multiplication(symbol); + break; + case '/': + walker.Symbol = new Divisioncs(symbol); + break; + } + walker.Symbol.Symbol = symbol; + walker.IsEmpty = false; + walker.Left = new Node(); + walker.Right = new Node(); + walker.Left.Value = new Operand(0); + walker.Right.Value = new Operand(0); + ++Root.Size; + } + + private void InfixOrder(Node root, double number, ref bool isAdded) + { + if (root != null) + { + InfixOrder(root.Left, number, ref isAdded); + if (root.IsEmpty && !isAdded) + { + root.Value.Number = number; + root.IsEmpty = false; + isAdded = true; + } + InfixOrder(root.Right, number, ref isAdded); + } + } + + private void AddNumberToTree(double number) + { + if (Root == null) + { + throw new InvalidExpressionException(); + } + bool isAdded = false; + InfixOrder(Root, number, ref isAdded); + if (isAdded == false) + { + throw new InvalidExpressionException(); + } + --Root.Size; + } + + private bool isSymbolOperation(char symbol) + { + return symbol == '+' + || symbol == '-' + || symbol == '*' + || symbol == '/'; + } + + // Builds an expression tree by expression + public void TreeExpression(string stringExpression) + { + for (int i = 0; i < stringExpression.Length; i++) + { + if(Char.IsNumber(stringExpression[i]) || (i < stringExpression.Length - 1 && stringExpression[i] == '-' && Char.IsNumber(stringExpression[i + 1]))) + { + double number = 0; + bool isMinus = false; + if (stringExpression[i] == '-') + { + ++i; + isMinus = true; + } + while (i < stringExpression.Length && Char.IsNumber(stringExpression[i])) + { + number += stringExpression[i] - 48; + number *= 10; + ++i; + } + number /= 10; + if (isMinus) + { + number *= -1; + } + AddNumberToTree(number); + } + else if (isSymbolOperation(stringExpression[i])) + { + AddSymbolToTree(stringExpression[i]); + } + else if (stringExpression[i] != ' ' && stringExpression[i] != ')' && stringExpression[i] != '(') + { + throw new InvalidExpressionException(); + } + } + if (Root.Size != 0 || Root == null) + { + throw new InvalidExpressionException(); + } + } + + private void Order(Node root) + { + if (root.Symbol != null) + { + Order(root.Left); + if (root.Left.Symbol == null || root.Left.IsEmpty) + { + root.Value.Number = root.Symbol.Calcuate(root.Left.Value.Number, root.Right.Value.Number); + if (root.Left.Symbol != null) + { + root.Left.IsEmpty = false; + } + root.IsEmpty = true; + } + } + } + + // Counts an expression in the tree + public double Calcuate() + { + if (Root == null) + { + throw new NullReferenceException(); + } + Order(Root); + Root.IsEmpty = false; + return Root.Value.Number; + } + + private void PostOrderPrint(Node root, ref bool isFirstNumber) + { + if (root != null) + { + if (root.Symbol == null) + { + root.Value.Print(); + if (!isFirstNumber) + { + Console.Write(") "); + } + isFirstNumber = false; + } + else + { + Console.Write('('); + Root.Symbol.Print(); + } + PostOrderPrint(root.Left, ref isFirstNumber); + PostOrderPrint(root.Right, ref isFirstNumber); + } + } + + // Outputs the expression stored in the tree to the screen + public void PrintExpression() + { + bool isFirstNumber = true; + PostOrderPrint(Root, ref isFirstNumber); + } + + private class Node + { + public Node() + { + IsEmpty = true; + } + + public Operand Value { get; set; } + + public Operator Symbol { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + + public bool IsEmpty { get; set; } + + public int Size { get; set; } + } +} From 10d6f2bb06c710c101127ef3492efe7daa4f73af Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 20:39:49 +0300 Subject: [PATCH 02/11] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D0=B8=D0=B8=20+=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Divisioncs.cs | 21 ----- .../ParsingTree/InvalidExpressionException.cs | 1 + ParsingTree/ParsingTree/Multiplication.cs | 5 ++ ParsingTree/ParsingTree/Operand.cs | 3 + ParsingTree/ParsingTree/Operator.cs | 3 + ParsingTree/ParsingTree/PartOfExpression.cs | 3 + ParsingTree/ParsingTree/Tree.cs | 4 +- ParsingTree/TestsParsingTree/TestsTree.cs | 83 +++++++++++++++++++ 8 files changed, 100 insertions(+), 23 deletions(-) delete mode 100644 ParsingTree/ParsingTree/Divisioncs.cs create mode 100644 ParsingTree/TestsParsingTree/TestsTree.cs diff --git a/ParsingTree/ParsingTree/Divisioncs.cs b/ParsingTree/ParsingTree/Divisioncs.cs deleted file mode 100644 index 1a24394..0000000 --- a/ParsingTree/ParsingTree/Divisioncs.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace ParsingTree; -public class Divisioncs : Operator -{ - double delta = 0.0000001; - - public Divisioncs(char symbol) : base(symbol) {} - - public override double Calcuate(double firstValue, double secondValue) - { - if (secondValue - Math.Abs(secondValue) < delta) - { - throw new ArgumentException(); - } - return firstValue / secondValue; - } - - public override void Print() - { - Console.Write(" / "); - } -} diff --git a/ParsingTree/ParsingTree/InvalidExpressionException.cs b/ParsingTree/ParsingTree/InvalidExpressionException.cs index 9136f55..374ede6 100644 --- a/ParsingTree/ParsingTree/InvalidExpressionException.cs +++ b/ParsingTree/ParsingTree/InvalidExpressionException.cs @@ -1,3 +1,4 @@ namespace ParsingTree; +// Class exceptions are thrown when the expression is incorrect public class InvalidExpressionException : Exception {} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Multiplication.cs b/ParsingTree/ParsingTree/Multiplication.cs index 32eabab..ae2d7c6 100644 --- a/ParsingTree/ParsingTree/Multiplication.cs +++ b/ParsingTree/ParsingTree/Multiplication.cs @@ -1,13 +1,18 @@ namespace ParsingTree; + +// A class that implements multiplication public class Multiplication : Operator { + // Keeps a multiplication sign in itself public Multiplication(char symbol) : base(symbol) {} + // Counts multiplication of two numbers public override double Calcuate(double firstValue, double secondValue) { return firstValue * secondValue; } + // Displays the multiplication sign with two spaces public override void Print() { Console.Write(" * "); diff --git a/ParsingTree/ParsingTree/Operand.cs b/ParsingTree/ParsingTree/Operand.cs index f2dc190..dfa98be 100644 --- a/ParsingTree/ParsingTree/Operand.cs +++ b/ParsingTree/ParsingTree/Operand.cs @@ -1,12 +1,15 @@ namespace ParsingTree; +// A class of numbers for the expression tree public class Operand : PartOfExpression { + // Returns the passed number public double Calcuate(double firstValue, double secondValue) { return Number; } + // Prints a number public void Print() { Console.Write(Number); diff --git a/ParsingTree/ParsingTree/Operator.cs b/ParsingTree/ParsingTree/Operator.cs index 7e606fd..8f45f28 100644 --- a/ParsingTree/ParsingTree/Operator.cs +++ b/ParsingTree/ParsingTree/Operator.cs @@ -1,9 +1,12 @@ namespace ParsingTree; +// A class that includes multiply divide add and subtract abstract public class Operator : PartOfExpression { + // Abstract type for an action account public abstract double Calcuate(double firstValue, double secondValue); + // Abstract type for printing characters public abstract void Print(); public Operator(char symbol) diff --git a/ParsingTree/ParsingTree/PartOfExpression.cs b/ParsingTree/ParsingTree/PartOfExpression.cs index e85e020..651ec5b 100644 --- a/ParsingTree/ParsingTree/PartOfExpression.cs +++ b/ParsingTree/ParsingTree/PartOfExpression.cs @@ -1,8 +1,11 @@ namespace ParsingTree; +// Interface for implementing different parts of expressions public interface PartOfExpression { + // Counts two numbers public double Calcuate(double firstValue, double secondValue); + // Prints a character or number public void Print(); } diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs index 5e5867b..76bc8e7 100644 --- a/ParsingTree/ParsingTree/Tree.cs +++ b/ParsingTree/ParsingTree/Tree.cs @@ -22,7 +22,7 @@ private void AddSymbolToTree(char symbol) Root.Symbol = new Multiplication(symbol); break; case '/': - Root.Symbol = new Divisioncs(symbol); + Root.Symbol = new Divider(symbol); break; } Root.Symbol.Symbol = symbol; @@ -52,7 +52,7 @@ private void AddSymbolToTree(char symbol) walker.Symbol = new Multiplication(symbol); break; case '/': - walker.Symbol = new Divisioncs(symbol); + walker.Symbol = new Divider(symbol); break; } walker.Symbol.Symbol = symbol; diff --git a/ParsingTree/TestsParsingTree/TestsTree.cs b/ParsingTree/TestsParsingTree/TestsTree.cs new file mode 100644 index 0000000..176da05 --- /dev/null +++ b/ParsingTree/TestsParsingTree/TestsTree.cs @@ -0,0 +1,83 @@ +namespace TestsParsingTree; + +using ParsingTree; + +public class Tests +{ + Tree tree; + [SetUp] + public void Setup() + { + tree = new Tree(); + } + + [TestCaseSource(nameof(TreeForTest))] + public void InTheUsualExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) + { + tree.TreeExpression("+ 2 3"); + Assert.True(tree.Calcuate() == 5); + } + + [TestCaseSource(nameof(TreeForTest))] + public void InTheNormalExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) + { + tree.TreeExpression("(* (+ 2 3) 2)"); + Assert.True(tree.Calcuate() == 10); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenAnEmptyStrinIsReceivedTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression("")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenReceivingAnIncorrectStringWithTheAbsenceOfASignTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression(" 1 2")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenReceivingAnIncorrectMoreDifficultStringWithTheAbsenceOfASignTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression("(* (4 5) 2)")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenReceivingAnIncorrectStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression("+ 2")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenReceivingAnIncorrectMoreDifficultStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression("(* (+ 2 3) )")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenReceivingAnDifficultStringWithInvalidCharactersTheTreeShouldThrowAnException(Tree tree) + { + Assert.Throws(() => tree.TreeExpression("(* (+ 2 3) p 2)")); + } + + [TestCaseSource(nameof(TreeForTest))] + public void WhenTryingToDivideByZeroTheTreeShouldThrowAnException(Tree tree) + { + tree.TreeExpression("/ 2 0"); + Assert.Throws(() => tree.Calcuate()); + } + + [TestCaseSource(nameof(TreeForTest))] + public void TheTreeShouldWorkCorrectlyWithNegativeNumbers(Tree tree) + { + tree.TreeExpression("+ 2 -3"); + Assert.True(tree.Calcuate() == -1); + } + + private static IEnumerable TreeForTest + => new TestCaseData[] + { + new TestCaseData(new Tree()), + }; +} \ No newline at end of file From b2c51d1e1c809a5d49a3e715850bfa754ada9d28 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 20:41:05 +0300 Subject: [PATCH 03/11] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=D0=B7=D0=B2=D0=B0=D0=BD=D0=BD=D0=BE=D0=B5=20=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Divider.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ParsingTree/ParsingTree/Divider.cs diff --git a/ParsingTree/ParsingTree/Divider.cs b/ParsingTree/ParsingTree/Divider.cs new file mode 100644 index 0000000..2261d76 --- /dev/null +++ b/ParsingTree/ParsingTree/Divider.cs @@ -0,0 +1,26 @@ +namespace ParsingTree; + +// A class that implements division +public class Divider : Operator +{ + double delta = 0.0000001; + + // Keeps the division sign in itself + public Divider(char symbol) : base(symbol) {} + + // Counts the division of two numbers on each other + public override double Calcuate(double firstValue, double secondValue) + { + if (secondValue - Math.Abs(secondValue) < delta) + { + throw new ArgumentException(); + } + return firstValue / secondValue; + } + + // Displays a division sign with two spaces on the screen + public override void Print() + { + Console.Write(" / "); + } +} From 1f014e5af71fd3a05a7d53325cd5683fd1ee65b9 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 20:41:58 +0300 Subject: [PATCH 04/11] =?UTF-8?q?=D0=97=D0=B0=D0=B1=D1=8B=D0=BB=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=20=D1=81=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Plus.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ParsingTree/ParsingTree/Plus.cs diff --git a/ParsingTree/ParsingTree/Plus.cs b/ParsingTree/ParsingTree/Plus.cs new file mode 100644 index 0000000..a18665b --- /dev/null +++ b/ParsingTree/ParsingTree/Plus.cs @@ -0,0 +1,20 @@ +namespace ParsingTree; + +// A class that implements amount +public class Plus : Operator +{ + // Keeps a plus in itself + public Plus(char symbol) : base(symbol) {} + + // Counts the Amount of two numbers + public override double Calcuate(double firstValue, double secondValue) + { + return secondValue + firstValue; + } + + // Prints a plus sign with spaces + public override void Print() + { + Console.Write(" + "); + } +} From ad12396f0fe9f2c62652f171a334600cbf70c956 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 30 Mar 2023 23:31:04 +0300 Subject: [PATCH 05/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D1=8B=D0=BB=20.csproj?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree.sln | 13 +++++------ .../TestsForParsingTree.csproj | 23 +++++++++++++++++++ .../TestsTree.cs | 0 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 ParsingTree/TestsForParsingTree/TestsForParsingTree.csproj rename ParsingTree/{TestsParsingTree => TestsForParsingTree}/TestsTree.cs (100%) diff --git a/ParsingTree/ParsingTree.sln b/ParsingTree/ParsingTree.sln index 65be4ae..413afc6 100644 --- a/ParsingTree/ParsingTree.sln +++ b/ParsingTree/ParsingTree.sln @@ -1,11 +1,10 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{E7D3B03D-F74E-4781-9DF2-0CB24880162A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{E7D3B03D-F74E-4781-9DF2-0CB24880162A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsParsingTree", "TestsParsingTree\TestsParsingTree.csproj", "{B09F32EB-3DF8-4E88-B230-9229ECC98E73}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsForParsingTree", "TestsForParsingTree\TestsForParsingTree.csproj", "{92559259-B410-4FB3-B4A7-0A8AE15F68B1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +16,10 @@ Global {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7D3B03D-F74E-4781-9DF2-0CB24880162A}.Release|Any CPU.Build.0 = Release|Any CPU - {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B09F32EB-3DF8-4E88-B230-9229ECC98E73}.Release|Any CPU.Build.0 = Release|Any CPU + {92559259-B410-4FB3-B4A7-0A8AE15F68B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {92559259-B410-4FB3-B4A7-0A8AE15F68B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {92559259-B410-4FB3-B4A7-0A8AE15F68B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {92559259-B410-4FB3-B4A7-0A8AE15F68B1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ParsingTree/TestsForParsingTree/TestsForParsingTree.csproj b/ParsingTree/TestsForParsingTree/TestsForParsingTree.csproj new file mode 100644 index 0000000..82281fa --- /dev/null +++ b/ParsingTree/TestsForParsingTree/TestsForParsingTree.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/ParsingTree/TestsParsingTree/TestsTree.cs b/ParsingTree/TestsForParsingTree/TestsTree.cs similarity index 100% rename from ParsingTree/TestsParsingTree/TestsTree.cs rename to ParsingTree/TestsForParsingTree/TestsTree.cs From 20b57ce3320c3d927377ce55dc62c22120087249 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 00:00:59 +0300 Subject: [PATCH 06/11] +main --- ParsingTree/ParsingTree/Program.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ParsingTree/ParsingTree/Program.cs diff --git a/ParsingTree/ParsingTree/Program.cs b/ParsingTree/ParsingTree/Program.cs new file mode 100644 index 0000000..10c34c3 --- /dev/null +++ b/ParsingTree/ParsingTree/Program.cs @@ -0,0 +1,11 @@ +using System; + +namespace ListAndUniqueList; + +class Program +{ + public static void Main(string[] args) + { + + } +} \ No newline at end of file From 2dfbfd0aa2d949eef0ba647157b5118036ba0fea Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 00:06:21 +0300 Subject: [PATCH 07/11] +Usings --- ParsingTree/TestsForParsingTree/Usings.cs | 1 + 1 file changed, 1 insertion(+) create mode 100644 ParsingTree/TestsForParsingTree/Usings.cs diff --git a/ParsingTree/TestsForParsingTree/Usings.cs b/ParsingTree/TestsForParsingTree/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/ParsingTree/TestsForParsingTree/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From d79ae9e73d3c74fa9c9a34d3ee39cee1cf694b9c Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 13:43:25 +0300 Subject: [PATCH 08/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Tree.cs | 124 +++++++++++++++++--------------- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs index 76bc8e7..8b80186 100644 --- a/ParsingTree/ParsingTree/Tree.cs +++ b/ParsingTree/ParsingTree/Tree.cs @@ -1,11 +1,72 @@ -namespace ParsingTree; +using System.Linq.Expressions; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace ParsingTree; // The container is a "tree" with a hierarchical structure public class Tree { private Node Root { get; set; } - private void AddSymbolToTree(char symbol) + private void AddToTree(Node root, ref bool isAdded, double value = 0, char symbol = '\0') + { + if (root != null) + { + if (root.IsEmpty && !isAdded) + { + if (symbol != '\0') + { + switch (symbol) + { + case '-': + root.Symbol = new Minus(symbol); + break; + case '+': + root.Symbol = new Plus(symbol); + break; + case '*': + root.Symbol = new Multiplication(symbol); + break; + case '/': + root.Symbol = new Divider(symbol); + break; + } + root.IsEmpty = false; + root.Left = new Node(); + root.Right = new Node(); + root.Left.Value = new Operand(0); + root.Right.Value = new Operand(0); + ++Root.Size; + } + else + { + root.Value.Number = value; + root.IsEmpty = false; + --Root.Size; + + } + isAdded = true; + } + AddToTree(root.Left, ref isAdded, value, symbol); + AddToTree(root.Right, ref isAdded, value, symbol); + } + } + + private void AddToTreeNumber(double value) + { + if (Root == null) + { + throw new InvalidExpressionException(); + } + bool isAdded = false; + AddToTree(Root, ref isAdded, value); + if (!isAdded) + { + throw new InvalidExpressionException(); + } + } + + private void AddToTreeSymbol(char symbol) { if (Root == null) { @@ -35,63 +96,12 @@ private void AddSymbolToTree(char symbol) Root.Size += 2; return; } - var walker = Root; - while (walker.Left != null) - { - walker = walker.Left; - } - switch (symbol) - { - case '-': - walker.Symbol = new Minus(symbol); - break; - case '+': - walker.Symbol = new Plus(symbol); - break; - case '*': - walker.Symbol = new Multiplication(symbol); - break; - case '/': - walker.Symbol = new Divider(symbol); - break; - } - walker.Symbol.Symbol = symbol; - walker.IsEmpty = false; - walker.Left = new Node(); - walker.Right = new Node(); - walker.Left.Value = new Operand(0); - walker.Right.Value = new Operand(0); - ++Root.Size; - } - - private void InfixOrder(Node root, double number, ref bool isAdded) - { - if (root != null) - { - InfixOrder(root.Left, number, ref isAdded); - if (root.IsEmpty && !isAdded) - { - root.Value.Number = number; - root.IsEmpty = false; - isAdded = true; - } - InfixOrder(root.Right, number, ref isAdded); - } - } - - private void AddNumberToTree(double number) - { - if (Root == null) - { - throw new InvalidExpressionException(); - } bool isAdded = false; - InfixOrder(Root, number, ref isAdded); - if (isAdded == false) + AddToTree(Root, ref isAdded, 0, symbol); + if (!isAdded) { throw new InvalidExpressionException(); } - --Root.Size; } private bool isSymbolOperation(char symbol) @@ -127,11 +137,11 @@ public void TreeExpression(string stringExpression) { number *= -1; } - AddNumberToTree(number); + AddToTreeNumber(number); } else if (isSymbolOperation(stringExpression[i])) { - AddSymbolToTree(stringExpression[i]); + AddToTreeSymbol(stringExpression[i]); } else if (stringExpression[i] != ' ' && stringExpression[i] != ')' && stringExpression[i] != '(') { From 964a4b0dd94f47cf4491d27e0c01c174de86ba34 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 18:49:19 +0300 Subject: [PATCH 09/11] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Program.cs | 25 +++++++++++-- ParsingTree/ParsingTree/Tree.cs | 38 +++++++++++++------- ParsingTree/TestsForParsingTree/TestsTree.cs | 4 +-- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/ParsingTree/ParsingTree/Program.cs b/ParsingTree/ParsingTree/Program.cs index 10c34c3..4c7bd81 100644 --- a/ParsingTree/ParsingTree/Program.cs +++ b/ParsingTree/ParsingTree/Program.cs @@ -1,4 +1,5 @@ -using System; +using ParsingTree; +using System; namespace ListAndUniqueList; @@ -6,6 +7,26 @@ class Program { public static void Main(string[] args) { - + var tree = new Tree(); + Console.WriteLine("Input your string"); + string? stringExpression = Console.ReadLine(); + try + { + tree.TreeExpression(stringExpression); + Console.WriteLine(tree.Calcuate()); + } + catch (InvalidExpressionException) + { + Console.WriteLine("Incorrect input"); + } + catch(ArgumentException) + { + Console.WriteLine("Try to divide by zero"); + } + catch (NullReferenceException) + { + Console.WriteLine("Try to Calcuate without tree"); + } + tree.PrintExpression(); } } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs index 8b80186..1a096a9 100644 --- a/ParsingTree/ParsingTree/Tree.cs +++ b/ParsingTree/ParsingTree/Tree.cs @@ -159,15 +159,18 @@ private void Order(Node root) if (root.Symbol != null) { Order(root.Left); - if (root.Left.Symbol == null || root.Left.IsEmpty) + Order(root.Right); + if (root.Left.Symbol == null && root.Right.Symbol == null) { root.Value.Number = root.Symbol.Calcuate(root.Left.Value.Number, root.Right.Value.Number); - if (root.Left.Symbol != null) - { - root.Left.IsEmpty = false; - } root.IsEmpty = true; } + else + { + root.Value.Number = root.Symbol.Calcuate(root.Left.Value.Number, root.Right.Value.Number); + root.Left.IsEmpty = false; + root.Right.IsEmpty = false; + } } } @@ -183,34 +186,43 @@ public double Calcuate() return Root.Value.Number; } - private void PostOrderPrint(Node root, ref bool isFirstNumber) + private void PostOrderPrint(Node root, ref int isPreviousNumber, ref int sizeBackStaples) { if (root != null) { if (root.Symbol == null) { + ++isPreviousNumber; root.Value.Print(); - if (!isFirstNumber) + if (isPreviousNumber % 2 == 0 && isPreviousNumber != 0) { Console.Write(") "); + --sizeBackStaples; + isPreviousNumber = 0; } - isFirstNumber = false; } else { + isPreviousNumber = 0; Console.Write('('); - Root.Symbol.Print(); + ++sizeBackStaples; + root.Symbol.Print(); } - PostOrderPrint(root.Left, ref isFirstNumber); - PostOrderPrint(root.Right, ref isFirstNumber); + PostOrderPrint(root.Left, ref isPreviousNumber, ref sizeBackStaples); + PostOrderPrint(root.Right, ref isPreviousNumber, ref sizeBackStaples); } } // Outputs the expression stored in the tree to the screen public void PrintExpression() { - bool isFirstNumber = true; - PostOrderPrint(Root, ref isFirstNumber); + int isPreviousNumber = 0; + int sizeBackStaples = 0; + PostOrderPrint(Root, ref isPreviousNumber, ref sizeBackStaples); + for (int i = 0; i < sizeBackStaples; ++i) + { + Console.Write(')'); + } } private class Node diff --git a/ParsingTree/TestsForParsingTree/TestsTree.cs b/ParsingTree/TestsForParsingTree/TestsTree.cs index 176da05..65a989b 100644 --- a/ParsingTree/TestsForParsingTree/TestsTree.cs +++ b/ParsingTree/TestsForParsingTree/TestsTree.cs @@ -21,8 +21,8 @@ public void InTheUsualExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) [TestCaseSource(nameof(TreeForTest))] public void InTheNormalExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) { - tree.TreeExpression("(* (+ 2 3) 2)"); - Assert.True(tree.Calcuate() == 10); + tree.TreeExpression("(* (+ 2 3) (+ 5 7)"); + Assert.True(tree.Calcuate() == 60); } [TestCaseSource(nameof(TreeForTest))] From 277625867e658052f139038acd4a258df322071b Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 25 May 2023 20:15:59 +0300 Subject: [PATCH 10/11] =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Divider.cs | 7 ++----- ParsingTree/ParsingTree/Program.cs | 4 ++++ ParsingTree/ParsingTree/Tree.cs | 5 +---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ParsingTree/ParsingTree/Divider.cs b/ParsingTree/ParsingTree/Divider.cs index 2261d76..0154cd0 100644 --- a/ParsingTree/ParsingTree/Divider.cs +++ b/ParsingTree/ParsingTree/Divider.cs @@ -3,7 +3,7 @@ // A class that implements division public class Divider : Operator { - double delta = 0.0000001; + private double delta = 0.0000001; // Keeps the division sign in itself public Divider(char symbol) : base(symbol) {} @@ -19,8 +19,5 @@ public override double Calcuate(double firstValue, double secondValue) } // Displays a division sign with two spaces on the screen - public override void Print() - { - Console.Write(" / "); - } + public override void Print() => Console.Write(" / "); } diff --git a/ParsingTree/ParsingTree/Program.cs b/ParsingTree/ParsingTree/Program.cs index 4c7bd81..9323950 100644 --- a/ParsingTree/ParsingTree/Program.cs +++ b/ParsingTree/ParsingTree/Program.cs @@ -12,6 +12,10 @@ public static void Main(string[] args) string? stringExpression = Console.ReadLine(); try { + if (stringExpression == null) + { + throw new ArgumentNullException(nameof(stringExpression)); + } tree.TreeExpression(stringExpression); Console.WriteLine(tree.Calcuate()); } diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs index 1a096a9..4f8223b 100644 --- a/ParsingTree/ParsingTree/Tree.cs +++ b/ParsingTree/ParsingTree/Tree.cs @@ -1,7 +1,4 @@ -using System.Linq.Expressions; -using static System.Runtime.InteropServices.JavaScript.JSType; - -namespace ParsingTree; +namespace ParsingTree; // The container is a "tree" with a hierarchical structure public class Tree From a78e1bd2b205a961bce557f678e71883aaff1d59 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 12:17:57 +0300 Subject: [PATCH 11/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParsingTree/ParsingTree/Divider.cs | 18 +++++-- .../ParsingTree/InvalidExpressionException.cs | 4 +- ParsingTree/ParsingTree/Minus.cs | 21 +++++--- ParsingTree/ParsingTree/Multiplication.cs | 21 +++++--- ParsingTree/ParsingTree/Operand.cs | 23 ++++---- ParsingTree/ParsingTree/Operator.cs | 15 ++++-- ParsingTree/ParsingTree/PartOfExpression.cs | 12 +++-- ParsingTree/ParsingTree/Plus.cs | 27 +++++----- ParsingTree/ParsingTree/Program.cs | 53 ++++++++----------- ParsingTree/ParsingTree/Tree.cs | 19 +++++-- ParsingTree/TestsForParsingTree/TestsTree.cs | 46 +++++++--------- 11 files changed, 150 insertions(+), 109 deletions(-) diff --git a/ParsingTree/ParsingTree/Divider.cs b/ParsingTree/ParsingTree/Divider.cs index 0154cd0..7537d5e 100644 --- a/ParsingTree/ParsingTree/Divider.cs +++ b/ParsingTree/ParsingTree/Divider.cs @@ -1,14 +1,22 @@ namespace ParsingTree; -// A class that implements division +/// +/// A class for dividing numbers +/// public class Divider : Operator { private double delta = 0.0000001; - // Keeps the division sign in itself + /// + /// Inherits the ancestor's method + /// + /// Operator public Divider(char symbol) : base(symbol) {} - // Counts the division of two numbers on each other + /// + /// Counts the division of two numbers + /// + /// Throws an exception when dividing by zero public override double Calcuate(double firstValue, double secondValue) { if (secondValue - Math.Abs(secondValue) < delta) @@ -18,6 +26,8 @@ public override double Calcuate(double firstValue, double secondValue) return firstValue / secondValue; } - // Displays a division sign with two spaces on the screen + /// + /// Prints the division sign in the console + /// public override void Print() => Console.Write(" / "); } diff --git a/ParsingTree/ParsingTree/InvalidExpressionException.cs b/ParsingTree/ParsingTree/InvalidExpressionException.cs index 374ede6..0a544e0 100644 --- a/ParsingTree/ParsingTree/InvalidExpressionException.cs +++ b/ParsingTree/ParsingTree/InvalidExpressionException.cs @@ -1,4 +1,6 @@ namespace ParsingTree; -// Class exceptions are thrown when the expression is incorrect +/// +/// Throws an exception in case of an invalid expression +/// public class InvalidExpressionException : Exception {} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Minus.cs b/ParsingTree/ParsingTree/Minus.cs index 12f1ba2..82ab853 100644 --- a/ParsingTree/ParsingTree/Minus.cs +++ b/ParsingTree/ParsingTree/Minus.cs @@ -1,20 +1,25 @@ namespace ParsingTree; -// A class that implements subtraction +/// +/// Subtracts from one number another +/// public class Minus : Operator { - // Keeps a minus in itself + /// + /// Inherits the method of the ancestor operator + /// public Minus(char symbol) : base(symbol) {} - // Counts the difference of two numbers + /// + /// Calculates the difference + /// public override double Calcuate(double firstValue, double secondValue) { return secondValue - firstValue; } - // Prints a minus sign with spaces - public override void Print() - { - Console.Write(" - "); - } + /// + /// Prints the minus sign + /// + public override void Print() => Console.Write(" - "); } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Multiplication.cs b/ParsingTree/ParsingTree/Multiplication.cs index ae2d7c6..5a32f08 100644 --- a/ParsingTree/ParsingTree/Multiplication.cs +++ b/ParsingTree/ParsingTree/Multiplication.cs @@ -1,20 +1,25 @@ namespace ParsingTree; -// A class that implements multiplication +/// +/// Counts the multiplication of two numbers +/// public class Multiplication : Operator { - // Keeps a multiplication sign in itself + /// + /// Inherits the method of the ancestor operator + /// public Multiplication(char symbol) : base(symbol) {} - // Counts multiplication of two numbers + /// + /// Multiplies two numbers by each other + /// public override double Calcuate(double firstValue, double secondValue) { return firstValue * secondValue; } - // Displays the multiplication sign with two spaces - public override void Print() - { - Console.Write(" * "); - } + /// + /// Prints the multiply sign + /// + public override void Print() => Console.Write(" * "); } diff --git a/ParsingTree/ParsingTree/Operand.cs b/ParsingTree/ParsingTree/Operand.cs index dfa98be..7571f53 100644 --- a/ParsingTree/ParsingTree/Operand.cs +++ b/ParsingTree/ParsingTree/Operand.cs @@ -1,21 +1,26 @@ namespace ParsingTree; -// A class of numbers for the expression tree +/// +/// A class based on numbers +/// public class Operand : PartOfExpression { - // Returns the passed number + /// + /// Returns a stored number + /// public double Calcuate(double firstValue, double secondValue) { return Number; } - // Prints a number - public void Print() - { - Console.Write(Number); - Console.Write(' '); - } - + /// + /// Prints a number + /// + public void Print() => Console.Write(Number + ' '); + + /// + /// saves a number + /// public Operand(double number) { Number = number; diff --git a/ParsingTree/ParsingTree/Operator.cs b/ParsingTree/ParsingTree/Operator.cs index 8f45f28..ae33c5c 100644 --- a/ParsingTree/ParsingTree/Operator.cs +++ b/ParsingTree/ParsingTree/Operator.cs @@ -1,14 +1,23 @@ namespace ParsingTree; -// A class that includes multiply divide add and subtract +/// +/// A class that includes multiply divide add and subtract +/// abstract public class Operator : PartOfExpression { - // Abstract type for an action account + /// + /// Abstract type for an action account + /// public abstract double Calcuate(double firstValue, double secondValue); - // Abstract type for printing characters + /// + /// Abstract type for printing characters + /// public abstract void Print(); + /// + /// Stores a symbol in itself + /// public Operator(char symbol) { Symbol = symbol; diff --git a/ParsingTree/ParsingTree/PartOfExpression.cs b/ParsingTree/ParsingTree/PartOfExpression.cs index 651ec5b..37d97e4 100644 --- a/ParsingTree/ParsingTree/PartOfExpression.cs +++ b/ParsingTree/ParsingTree/PartOfExpression.cs @@ -1,11 +1,17 @@ namespace ParsingTree; -// Interface for implementing different parts of expressions +/// +/// Interface for implementing different parts of expressions +/// public interface PartOfExpression { - // Counts two numbers + /// + /// Counts two numbers + /// public double Calcuate(double firstValue, double secondValue); - // Prints a character or number + /// + /// Prints a character or number + /// public void Print(); } diff --git a/ParsingTree/ParsingTree/Plus.cs b/ParsingTree/ParsingTree/Plus.cs index a18665b..3a07efb 100644 --- a/ParsingTree/ParsingTree/Plus.cs +++ b/ParsingTree/ParsingTree/Plus.cs @@ -1,20 +1,23 @@ namespace ParsingTree; -// A class that implements amount +/// +/// A class that implements amount +/// public class Plus : Operator { - // Keeps a plus in itself + /// + /// Inherits the method of the ancestor of the operator + /// + /// public Plus(char symbol) : base(symbol) {} - // Counts the Amount of two numbers - public override double Calcuate(double firstValue, double secondValue) - { - return secondValue + firstValue; - } + /// + /// Counts the Amount of two numbers + /// + public override double Calcuate(double firstValue, double secondValue) => secondValue + firstValue; - // Prints a plus sign with spaces - public override void Print() - { - Console.Write(" + "); - } + /// + /// Prints a plus sign with spaces + /// + public override void Print() => Console.Write(" + "); } diff --git a/ParsingTree/ParsingTree/Program.cs b/ParsingTree/ParsingTree/Program.cs index 9323950..e953f27 100644 --- a/ParsingTree/ParsingTree/Program.cs +++ b/ParsingTree/ParsingTree/Program.cs @@ -1,36 +1,27 @@ using ParsingTree; -using System; -namespace ListAndUniqueList; - -class Program +Tree tree = new Tree(); +Console.WriteLine("Input your string"); +string? stringExpression = Console.ReadLine(); +try { - public static void Main(string[] args) + if (stringExpression == null) { - var tree = new Tree(); - Console.WriteLine("Input your string"); - string? stringExpression = Console.ReadLine(); - try - { - if (stringExpression == null) - { - throw new ArgumentNullException(nameof(stringExpression)); - } - tree.TreeExpression(stringExpression); - Console.WriteLine(tree.Calcuate()); - } - catch (InvalidExpressionException) - { - Console.WriteLine("Incorrect input"); - } - catch(ArgumentException) - { - Console.WriteLine("Try to divide by zero"); - } - catch (NullReferenceException) - { - Console.WriteLine("Try to Calcuate without tree"); - } - tree.PrintExpression(); + throw new ArgumentNullException(nameof(stringExpression)); } -} \ No newline at end of file + tree.TreeExpression(stringExpression); + Console.WriteLine(tree.Calcuate()); +} +catch (InvalidExpressionException) +{ + Console.WriteLine("Incorrect input"); +} +catch (ArgumentException) +{ + Console.WriteLine("Try to divide by zero"); +} +catch (NullReferenceException) +{ + Console.WriteLine("Try to Calcuate without tree"); +} +tree.PrintExpression(); \ No newline at end of file diff --git a/ParsingTree/ParsingTree/Tree.cs b/ParsingTree/ParsingTree/Tree.cs index 4f8223b..3d4e0af 100644 --- a/ParsingTree/ParsingTree/Tree.cs +++ b/ParsingTree/ParsingTree/Tree.cs @@ -1,6 +1,8 @@ namespace ParsingTree; -// The container is a "tree" with a hierarchical structure +/// +/// The container is a "tree" with a hierarchical structure +/// public class Tree { private Node Root { get; set; } @@ -109,7 +111,10 @@ private bool isSymbolOperation(char symbol) || symbol == '/'; } - // Builds an expression tree by expression + /// + /// Builds an expression tree by expression + /// + /// Throws an exception if the string is not correct public void TreeExpression(string stringExpression) { for (int i = 0; i < stringExpression.Length; i++) @@ -171,7 +176,11 @@ private void Order(Node root) } } - // Counts an expression in the tree + /// + /// Counts an expression in the tree + /// + /// + /// Throws an exception if the tree is empty public double Calcuate() { if (Root == null) @@ -210,7 +219,9 @@ private void PostOrderPrint(Node root, ref int isPreviousNumber, ref int sizeBac } } - // Outputs the expression stored in the tree to the screen + /// + /// Outputs the expression stored in the tree to the screen + /// public void PrintExpression() { int isPreviousNumber = 0; diff --git a/ParsingTree/TestsForParsingTree/TestsTree.cs b/ParsingTree/TestsForParsingTree/TestsTree.cs index 65a989b..2e73f7b 100644 --- a/ParsingTree/TestsForParsingTree/TestsTree.cs +++ b/ParsingTree/TestsForParsingTree/TestsTree.cs @@ -11,73 +11,67 @@ public void Setup() tree = new Tree(); } - [TestCaseSource(nameof(TreeForTest))] - public void InTheUsualExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) + [Test] + public void InTheUsualExampleTheTreeShouldCorrectlyCalculateTheValue() { tree.TreeExpression("+ 2 3"); Assert.True(tree.Calcuate() == 5); } - [TestCaseSource(nameof(TreeForTest))] - public void InTheNormalExampleTheTreeShouldCorrectlyCalculateTheValue(Tree tree) + [Test] + public void InTheNormalExampleTheTreeShouldCorrectlyCalculateTheValue() { tree.TreeExpression("(* (+ 2 3) (+ 5 7)"); Assert.True(tree.Calcuate() == 60); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenAnEmptyStrinIsReceivedTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenAnEmptyStrinIsReceivedTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression("")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenReceivingAnIncorrectStringWithTheAbsenceOfASignTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenReceivingAnIncorrectStringWithTheAbsenceOfASignTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression(" 1 2")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenReceivingAnIncorrectMoreDifficultStringWithTheAbsenceOfASignTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenReceivingAnIncorrectMoreDifficultStringWithTheAbsenceOfASignTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression("(* (4 5) 2)")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenReceivingAnIncorrectStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenReceivingAnIncorrectStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression("+ 2")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenReceivingAnIncorrectMoreDifficultStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenReceivingAnIncorrectMoreDifficultStringWithTheTheAbsenceOfANumberTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression("(* (+ 2 3) )")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenReceivingAnDifficultStringWithInvalidCharactersTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenReceivingAnDifficultStringWithInvalidCharactersTheTreeShouldThrowAnException() { Assert.Throws(() => tree.TreeExpression("(* (+ 2 3) p 2)")); } - [TestCaseSource(nameof(TreeForTest))] - public void WhenTryingToDivideByZeroTheTreeShouldThrowAnException(Tree tree) + [Test] + public void WhenTryingToDivideByZeroTheTreeShouldThrowAnException() { tree.TreeExpression("/ 2 0"); Assert.Throws(() => tree.Calcuate()); } - [TestCaseSource(nameof(TreeForTest))] - public void TheTreeShouldWorkCorrectlyWithNegativeNumbers(Tree tree) + [Test] + public void TheTreeShouldWorkCorrectlyWithNegativeNumbers() { tree.TreeExpression("+ 2 -3"); Assert.True(tree.Calcuate() == -1); } - - private static IEnumerable TreeForTest - => new TestCaseData[] - { - new TestCaseData(new Tree()), - }; } \ No newline at end of file