-
Notifications
You must be signed in to change notification settings - Fork 0
Parsing tree #9
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?
Parsing tree #9
Changes from all commits
f6cce94
10d6f2b
b2c51d1
1f014e5
c46052e
b015c18
648de94
ad12396
20b57ce
2dfbfd0
d79ae9e
964a4b0
2776258
a78e1bd
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,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 |
| 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) {} | ||
|
|
||
| /// <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) | ||
|
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. А вот свои операнды он вполне мог бы хранить. Всё наоборот, в общем :) |
||
| { | ||
| 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(" / "); | ||
| } | ||
| 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 {} |
| 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(" - "); | ||
| } |
| 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(" * "); | ||
| } |
| 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) | ||
|
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. У операнда ведь не может быть операндов, 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; } | ||
|
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. Сеттер не уверен, что нужен |
||
| } | ||
| 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; } | ||
| } |
| 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> |
| 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(); | ||
| } |
| 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(" + "); | ||
| } |
| 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(); |
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.
Зачем ему принимать извне division sign? Он и так знает, что он Divider