Skip to content
30 changes: 30 additions & 0 deletions ParsingTree/ParsingTree.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParsingTree", "ParsingTree\ParsingTree.csproj", "{E7D3B03D-F74E-4781-9DF2-0CB24880162A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsForParsingTree", "TestsForParsingTree\TestsForParsingTree.csproj", "{92559259-B410-4FB3-B4A7-0A8AE15F68B1}"
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
{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
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4AF94C07-C87B-41B8-9B20-62E8562E599A}
EndGlobalSection
EndGlobal
33 changes: 33 additions & 0 deletions ParsingTree/ParsingTree/Divider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace ParsingTree;

/// <summary>
/// A class for dividing numbers
/// </summary>
public class Divider : Operator
{
private double delta = 0.0000001;

/// <summary>
/// Inherits the ancestor's method
/// </summary>
/// <param name="symbol">Operator</param>
public Divider(char symbol) : base(symbol) {}

Choose a reason for hiding this comment

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

Зачем ему принимать извне division sign? Он и так знает, что он Divider


/// <summary>
/// Counts the division of two numbers
/// </summary>
/// <exception cref="ArgumentException">Throws an exception when dividing by zero</exception>
public override double Calcuate(double firstValue, double secondValue)

Choose a reason for hiding this comment

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

А вот свои операнды он вполне мог бы хранить. Всё наоборот, в общем :)

{
if (secondValue - Math.Abs(secondValue) < delta)
{
throw new ArgumentException();
}
return firstValue / secondValue;
}

/// <summary>
/// Prints the division sign in the console
/// </summary>
public override void Print() => Console.Write(" / ");
}
6 changes: 6 additions & 0 deletions ParsingTree/ParsingTree/InvalidExpressionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ParsingTree;

/// <summary>
/// Throws an exception in case of an invalid expression
/// </summary>
public class InvalidExpressionException : Exception {}
25 changes: 25 additions & 0 deletions ParsingTree/ParsingTree/Minus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ParsingTree;

/// <summary>
/// Subtracts from one number another
/// </summary>
public class Minus : Operator
{
/// <summary>
/// Inherits the method of the ancestor operator
/// </summary>
public Minus(char symbol) : base(symbol) {}

/// <summary>
/// Calculates the difference
/// </summary>
public override double Calcuate(double firstValue, double secondValue)
{
return secondValue - firstValue;
}

/// <summary>
/// Prints the minus sign
/// </summary>
public override void Print() => Console.Write(" - ");
}
25 changes: 25 additions & 0 deletions ParsingTree/ParsingTree/Multiplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ParsingTree;

/// <summary>
/// Counts the multiplication of two numbers
/// </summary>
public class Multiplication : Operator
{
/// <summary>
/// Inherits the method of the ancestor operator
/// </summary>
public Multiplication(char symbol) : base(symbol) {}

/// <summary>
/// Multiplies two numbers by each other
/// </summary>
public override double Calcuate(double firstValue, double secondValue)
{
return firstValue * secondValue;
}

/// <summary>
/// Prints the multiply sign
/// </summary>
public override void Print() => Console.Write(" * ");
}
30 changes: 30 additions & 0 deletions ParsingTree/ParsingTree/Operand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace ParsingTree;

/// <summary>
/// A class based on numbers
/// </summary>
public class Operand : PartOfExpression
{
/// <summary>
/// Returns a stored number
/// </summary>
public double Calcuate(double firstValue, double secondValue)

Choose a reason for hiding this comment

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

У операнда ведь не может быть операндов, firstValue и secondValue бессмысленны. Но, собственно, это всё равно надо бы переделать, потому что операнды можно хранить в Operator

{
return Number;
}

/// <summary>
/// Prints a number
/// </summary>
public void Print() => Console.Write(Number + ' ');

/// <summary>
/// saves a number
/// </summary>
public Operand(double number)
{
Number = number;
}

public double Number { get; set; }

Choose a reason for hiding this comment

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

Сеттер не уверен, что нужен

}
27 changes: 27 additions & 0 deletions ParsingTree/ParsingTree/Operator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ParsingTree;

/// <summary>
/// A class that includes multiply divide add and subtract
/// </summary>
abstract public class Operator : PartOfExpression
{
/// <summary>
/// Abstract type for an action account
/// </summary>
public abstract double Calcuate(double firstValue, double secondValue);

/// <summary>
/// Abstract type for printing characters
/// </summary>
public abstract void Print();

/// <summary>
/// Stores a symbol in itself
/// </summary>
public Operator(char symbol)
{
Symbol = symbol;
}

public char Symbol { get; set; }
}
10 changes: 10 additions & 0 deletions ParsingTree/ParsingTree/ParsingTree.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
17 changes: 17 additions & 0 deletions ParsingTree/ParsingTree/PartOfExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ParsingTree;

/// <summary>
/// Interface for implementing different parts of expressions
/// </summary>
public interface PartOfExpression
{
/// <summary>
/// Counts two numbers
/// </summary>
public double Calcuate(double firstValue, double secondValue);

/// <summary>
/// Prints a character or number
/// </summary>
public void Print();
}
23 changes: 23 additions & 0 deletions ParsingTree/ParsingTree/Plus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace ParsingTree;

/// <summary>
/// A class that implements amount
/// </summary>
public class Plus : Operator
{
/// <summary>
/// Inherits the method of the ancestor of the operator
/// </summary>
/// <param name="symbol"></param>
public Plus(char symbol) : base(symbol) {}

/// <summary>
/// Counts the Amount of two numbers
/// </summary>
public override double Calcuate(double firstValue, double secondValue) => secondValue + firstValue;

/// <summary>
/// Prints a plus sign with spaces
/// </summary>
public override void Print() => Console.Write(" + ");
}
27 changes: 27 additions & 0 deletions ParsingTree/ParsingTree/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using ParsingTree;

Tree 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();
Loading