Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions appveyor.yml
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
19 changes: 19 additions & 0 deletions hw4ParseTree/hw4ParseTree.Test/hw4ParseTree.Test.csproj
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>
87 changes: 87 additions & 0 deletions hw4ParseTree/hw4ParseTree.Test/hw4ParseTreeTest.cs
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());
}
}
}
31 changes: 31 additions & 0 deletions hw4ParseTree/hw4ParseTree.sln
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
23 changes: 23 additions & 0 deletions hw4ParseTree/hw4ParseTree/Addition.cs
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>
/// класс для сложения
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы написал, что это узел синтаксического дерева, который представляет оператор сложения, но так тоже ок :)

/// </summary>
public class Addition : Operator

This comment was marked as resolved.

{
public override char Sign => '+';

public Addition(INode leftChild, INode rightChild)
{
LeftChild = leftChild;
RightChild = rightChild;
}

public override double Calculate()
=> LeftChild.Calculate() + RightChild.Calculate();
}
}
29 changes: 29 additions & 0 deletions hw4ParseTree/hw4ParseTree/Division.cs
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();
}
}
}
1 change: 1 addition & 0 deletions hw4ParseTree/hw4ParseTree/Expression.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* (+ 1 1) 2)
22 changes: 22 additions & 0 deletions hw4ParseTree/hw4ParseTree/INode.cs
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.

{
/// <summary>
/// печатает, что находится в узле
/// </summary>
public void Print();

/// <summary>
/// возвращает значение элемента, лежащего в узле
/// </summary>
public double Calculate();
}
}
21 changes: 21 additions & 0 deletions hw4ParseTree/hw4ParseTree/InvalidExpressionException.cs
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.

{
public InvalidExpressionException()
{
}

public InvalidExpressionException(string message)
: base(message)
{
}
}
}
23 changes: 23 additions & 0 deletions hw4ParseTree/hw4ParseTree/Multiplication.cs
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();
}
}
29 changes: 29 additions & 0 deletions hw4ParseTree/hw4ParseTree/Operand.cs
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А тут и ниже комментариев всё равно нет

{
private double Number;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
}
35 changes: 35 additions & 0 deletions hw4ParseTree/hw4ParseTree/Operator.cs
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(")");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо перевод строки :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Напоминаю


/// <summary>
/// считает значение
/// </summary>
public abstract double Calculate();
}
}
Loading