-
Notifications
You must be signed in to change notification settings - Fork 0
Hw4 parse tree #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5ecef5f
405e26b
a2af7bf
d31b3e5
5bf6254
ac9a649
7768ced
22908a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| image: Visual Studio 2019 | ||
|
|
||
| build_script: | ||
| - For /R %%I in (*.sln) do dotnet test %%I | ||
|
|
||
| test: off |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NUnit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\hw4ParseTree\Hw4ParseTree.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using NUnit.Framework; | ||
| using System; | ||
|
|
||
| namespace Hw4ParseTree.Test | ||
| { | ||
| public class Tests | ||
| { | ||
| private ParseTree tree; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| tree = new ParseTree(); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestAddition() | ||
| { | ||
| var str = "( + 2 3 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(5, tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestSubtraction() | ||
| { | ||
| var str = "( - 2 3 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(-1, tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestMultiplication() | ||
| { | ||
| var str = "( * 2 3 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(6, tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestDivision() | ||
| { | ||
| var str = "( / 8 4 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(2, tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestDoubleDivision() | ||
| { | ||
| var str = "( / 3 2 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(1.5, tree.Calculate(), 0.000001); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestWithNegativeNumber() | ||
| { | ||
| var str = "( + 3 -2 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(1, tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestDivisionByZero() | ||
| { | ||
| var str = "( / 8 0 )"; | ||
| tree.BuildTree(str); | ||
| Assert.Throws<DivideByZeroException>(() => tree.Calculate()); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestNotCorrectExpression() | ||
| { | ||
| var str = "( / 8 )"; | ||
| Assert.Throws<InvalidExpressionException>(() => tree.BuildTree(str)); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TestTaskExpression() | ||
| { | ||
| var str = "( * ( + 1 1 ) 2 )"; | ||
| tree.BuildTree(str); | ||
| Assert.AreEqual(4, tree.Calculate()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.31005.135 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw4ParseTree", "hw4ParseTree\Hw4ParseTree.csproj", "{70C27B8B-C63D-4F52-B8C7-B9561C911D17}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw4ParseTree.Test", "hw4ParseTree.Test\hw4ParseTree.Test.csproj", "{E5953ED4-DAAC-461D-AF86-3DE600136A14}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {70C27B8B-C63D-4F52-B8C7-B9561C911D17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {70C27B8B-C63D-4F52-B8C7-B9561C911D17}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {70C27B8B-C63D-4F52-B8C7-B9561C911D17}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {70C27B8B-C63D-4F52-B8C7-B9561C911D17}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {E5953ED4-DAAC-461D-AF86-3DE600136A14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E5953ED4-DAAC-461D-AF86-3DE600136A14}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E5953ED4-DAAC-461D-AF86-3DE600136A14}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E5953ED4-DAAC-461D-AF86-3DE600136A14}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {E315057C-F86D-4CC5-95B3-11FF0F5A2F77} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// класс для сложения | ||
| /// </summary> | ||
| public class Addition : Operator | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| { | ||
| public override char Sign => '+'; | ||
|
|
||
| public Addition(INode leftChild, INode rightChild) | ||
| { | ||
| LeftChild = leftChild; | ||
| RightChild = rightChild; | ||
| } | ||
|
|
||
| public override double Calculate() | ||
| => LeftChild.Calculate() + RightChild.Calculate(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// класс для деления | ||
| /// </summary> | ||
| public class Division : Operator | ||
| { | ||
| public override char Sign => '/'; | ||
|
|
||
| public Division(INode leftChild, INode rightChild) | ||
| { | ||
| LeftChild = leftChild; | ||
| RightChild = rightChild; | ||
| } | ||
|
|
||
| public override double Calculate() | ||
| { | ||
| if (Math.Abs(RightChild.Calculate()) < 0.000001) | ||
| { | ||
| throw new DivideByZeroException(); | ||
| } | ||
| return LeftChild.Calculate() / RightChild.Calculate(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (* (+ 1 1) 2) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// интерфейс узла в дереве разбора | ||
| /// </summary> | ||
| public interface INode | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| { | ||
| /// <summary> | ||
| /// печатает, что находится в узле | ||
| /// </summary> | ||
| public void Print(); | ||
|
|
||
| /// <summary> | ||
| /// возвращает значение элемента, лежащего в узле | ||
| /// </summary> | ||
| public double Calculate(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// исключение для неправильных выражений. | ||
| /// </summary> | ||
| public class InvalidExpressionException : Exception | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| { | ||
| public InvalidExpressionException() | ||
| { | ||
| } | ||
|
|
||
| public InvalidExpressionException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// класс для умножения | ||
| /// </summary> | ||
| public class Multiplication : Operator | ||
| { | ||
| public override char Sign => '*'; | ||
|
|
||
| public Multiplication(INode leftChild, INode rightChild) | ||
| { | ||
| LeftChild = leftChild; | ||
| RightChild = rightChild; | ||
| } | ||
|
|
||
| public override double Calculate() | ||
| => LeftChild.Calculate() * RightChild.Calculate(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// класс синтаксического дерева, который представляет число | ||
| /// </summary> | ||
| class Operand : INode | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А тут и ниже комментариев всё равно нет |
||
| { | ||
| private double Number; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поля, как известно, именуются со строчной. Если Вам сложно запомнить такие мелочи, используйте StyleCop. |
||
|
|
||
| public Operand(double number) | ||
| => Number = number; | ||
|
|
||
| /// <summary> | ||
| /// выводит число | ||
| /// </summary> | ||
| public void Print() | ||
| => Console.Write($" {Number} "); | ||
|
|
||
| /// <summary> | ||
| /// считает значение | ||
| /// </summary> | ||
| public double Calculate() | ||
| => Number; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw4ParseTree | ||
| { | ||
| /// <summary> | ||
| /// Класс синтаксического дерва который представляет оператор | ||
| /// </summary> | ||
| public abstract class Operator : INode | ||
| { | ||
| public INode LeftChild { get; set; } | ||
|
|
||
| public INode RightChild { get; set; } | ||
|
|
||
| public virtual char Sign { get; } | ||
|
|
||
| /// <summary> | ||
| /// выводит участок выражения | ||
| /// </summary> | ||
| public void Print() | ||
| { | ||
| Console.Write("("); | ||
| LeftChild.Print(); | ||
| Console.Write(Sign); | ||
| RightChild.Print(); | ||
| Console.Write(")"); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо перевод строки :)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Напоминаю |
||
|
|
||
| /// <summary> | ||
| /// считает значение | ||
| /// </summary> | ||
| public abstract double Calculate(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы написал, что это узел синтаксического дерева, который представляет оператор сложения, но так тоже ок :)