Skip to content

Commit d8893fc

Browse files
authored
Merge pull request #15 from Crequency/dev=main
[Pull Request] Added `expression` class for building expression tree
2 parents 69f747e + 85d5a32 commit d8893fc

8 files changed

Lines changed: 299 additions & 5 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace Common.BasicHelper.Math;
2+
3+
[TestClass()]
4+
public class Calculator_Tests
5+
{
6+
[TestMethod()]
7+
public void Test_Calculate()
8+
{
9+
// 15 = 5 + 10
10+
Assert.AreEqual(
11+
15,
12+
Calculator.Calculate(
13+
Expression.FromValue(5),
14+
Expression.FromValue(10),
15+
CalculationType.Add
16+
)
17+
);
18+
19+
// 5 = 10 - 5
20+
Assert.AreEqual(
21+
5,
22+
Calculator.Calculate(
23+
Expression.FromValue(10),
24+
Expression.FromValue(5),
25+
CalculationType.Substraction
26+
)
27+
);
28+
29+
// 50 = 5 * 10
30+
Assert.AreEqual(
31+
50,
32+
Calculator.Calculate(
33+
Expression.FromValue(5),
34+
Expression.FromValue(10),
35+
CalculationType.Multiply
36+
)
37+
);
38+
39+
// 0.5 = 5 / 10
40+
Assert.AreEqual(
41+
0.5,
42+
Calculator.Calculate(
43+
Expression.FromValue(5),
44+
Expression.FromValue(10),
45+
CalculationType.Division
46+
)
47+
);
48+
49+
// 1024 = 2 ^ 10
50+
Assert.AreEqual(
51+
1024,
52+
Calculator.Calculate(
53+
Expression.FromValue(2),
54+
Expression.FromValue(10),
55+
CalculationType.Power
56+
)
57+
);
58+
}
59+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Common.BasicHelper.Math;
2+
3+
[TestClass()]
4+
public class Expression_Tests
5+
{
6+
[TestMethod()]
7+
public void Test_Expression()
8+
{
9+
var expr = (39 / 3) + (16 * 5) - ((24 * 24) + 5 + 7);
10+
11+
var tree = new Expression()
12+
{
13+
Type = CalculationType.Add,
14+
Left = new()
15+
{
16+
Type = CalculationType.Division,
17+
Left = Expression.FromValue(39),
18+
Right = Expression.FromValue(3)
19+
},
20+
Right = new()
21+
{
22+
Type = CalculationType.Substraction,
23+
Left = new()
24+
{
25+
Type = CalculationType.Multiply,
26+
Left = Expression.FromValue(16),
27+
Right = Expression.FromValue(5),
28+
},
29+
Right = new()
30+
{
31+
Type = CalculationType.Add,
32+
Left = new()
33+
{
34+
Type = CalculationType.Power,
35+
Left = Expression.FromValue(24),
36+
Right = Expression.FromValue(2)
37+
},
38+
Right = new()
39+
{
40+
Type = CalculationType.Add,
41+
Left = Expression.FromValue(5),
42+
Right = Expression.FromValue(7)
43+
}
44+
}
45+
}
46+
};
47+
48+
Assert.AreEqual(expr, tree.Result);
49+
}
50+
}

Common.BasicHelper/Common.BasicHelper.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
<TargetFramework>netstandard2.1</TargetFramework>
66
<ImplicitUsings>disable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<PropertyGroup>
811
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
9-
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
1012
<Authors>Dynesshely</Authors>
1113
<Company>Crequency</Company>
1214
<PackageLicenseExpression>AGPL-3.0-only</PackageLicenseExpression>
@@ -16,7 +18,9 @@
1618
<RepositoryUrl>https://github.com/Crequency/Common.BasicHelper/</RepositoryUrl>
1719
<Copyright>Copyright © Crequency 2022-present</Copyright>
1820
<PackageIcon>icon.png</PackageIcon>
21+
</PropertyGroup>
1922

23+
<PropertyGroup>
2024
<AssemblyVersion>$(Version)</AssemblyVersion>
2125
<FileVersion>$(Version)</FileVersion>
2226
<Version>1.2.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2005-06-06"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))</Version>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# CommandsExecutor
2+
3+
This is a class to help to run commands in system shell.
4+
5+
It provides 2 static methods with each have a extension method.
6+
7+
## Static Methods
8+
9+
### Methods and arguments with description
10+
11+
- `GetExecutionResult` -> `string`
12+
13+
This method execute command in system shell, and return standard output content.
14+
15+
`string` **command** - command name, when **findInPath** is true, file path will find from `Path`.
16+
17+
`string` **args** - start up arguments.
18+
19+
`[bool]` **findInPath** = false - indecate whether get file path in `Path`.
20+
21+
`[Action<ProcessStartInfo>?]` **action** = null - user can user this argument to operate `ProcessStartInfo`.
22+
23+
- `GetExecutionResultAsync` -> `Task<string>`
24+
25+
This method has same function and 4 same parameters as `GetExecutionResult`, but execute outside program asynchronously,
26+
you can await this method.
27+
28+
`string`, `string`, `[bool]`, `[Action<ProcessStartInfo>?]`
29+
30+
`[CancellationToken?]` **token** = null - you can use this object to cancel this task, once **token**!.`IsCancellationRequested` is true, the outside process will be killed.
31+
32+
### Usage
33+
34+
```csharp
35+
using Common.BasicHelper.Core.Shell;
36+
37+
var output = CommandsExecutor.GetExecutionResult(
38+
"help", // command, use `help` for example
39+
"",
40+
true
41+
);
42+
43+
var output_async - CommandsExecutor.GetExecutionResultAsync(
44+
"help",
45+
"",
46+
true
47+
);
48+
49+
Console.WriteLine(output);
50+
Console.WriteLine(output_async);
51+
```
52+
53+
## Extension Methods
54+
55+
### Methods and arguments with description
56+
57+
- `ExecuteAsCommand` -> `string`
58+
59+
This extension method is a wrapping method for `GetExecutionResult`.
60+
61+
*this* `string` **command**
62+
63+
`[string?]` **args** = null
64+
65+
`[bool]` **findInPath** = true
66+
67+
`[Action<ProcessStartInfo>?]` **action** = null
68+
69+
- `ExecuteAsCommandAsync` -> `Task<string>`
70+
71+
This extension method is a wrapping method for `GetExecutionResultAsync`.
72+
73+
*this* `string` **command**
74+
75+
`[string?]` **args** = null
76+
77+
`[bool]` **findInPath** = true
78+
79+
`[Action<ProcessStartInfo>?]` **action** = null
80+
81+
`[CancellationToken?]` **token** = default
82+
83+
### Usage
84+
85+
```csharp
86+
using Common.BasicHelper.Core.Shell;
87+
88+
Console.WriteLine("help".ExecuteAsCommand());
89+
```
90+
91+
## OOP
92+
93+
This is a static class.
94+
95+
## Examples
96+
97+
- In [KitX Dashboard](https://github.com/Crequency/KitX-Dashboard/blob/03e4d3b127de69a34df00f6ac34db7ec6bd1a0d4/Network/NetworkHelper.cs#L139) project, we use these functions to get system version info.
98+

Common.BasicHelper/Graphics/Screen/Resolution.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public class Resolution
5757
/// </summary>
5858
public Resolution Integerization()
5959
{
60-
if (Width != null)
60+
if (Width is not null)
6161
Width = System.Math.Round(Width.Value, 0);
62-
if (Height != null)
62+
if (Height is not null)
6363
Height = System.Math.Round(Height.Value, 0);
6464
return this;
6565
}
@@ -132,7 +132,7 @@ public static Resolution Suggest(Resolution screen, Resolution content, Resoluti
132132

133133
var yy = tararea / content.AspectRatio;
134134

135-
if (yy != null)
135+
if (yy is not null)
136136
{
137137
suggest.Height = System.Math.Sqrt((double)yy);
138138
suggest.Width = content.AspectRatio * suggest.Height;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace Common.BasicHelper.Math;
4+
5+
public static class Calculator
6+
{
7+
public static double Calculate(Expression? a, Expression? b, CalculationType type)
8+
{
9+
a = a ?? throw new ArgumentNullException(nameof(a));
10+
b = b ?? throw new ArgumentNullException(nameof(b));
11+
12+
switch (type)
13+
{
14+
case CalculationType.Unknown:
15+
throw new NotImplementedException(
16+
$"Unknown type can't calculate for `{a?.Result} {type} {b?.Result}`."
17+
);
18+
19+
case CalculationType.Add:
20+
return a!.Result + b!.Result;
21+
22+
case CalculationType.Substraction:
23+
return a!.Result - b!.Result;
24+
25+
case CalculationType.Multiply:
26+
return a!.Result * b!.Result;
27+
28+
case CalculationType.Division:
29+
return a!.Result / b!.Result; // Here couldn't throw divide by zero exception.
30+
31+
case CalculationType.Power:
32+
return System.Math.Pow(a!.Result, b!.Result);
33+
34+
default:
35+
throw new NotImplementedException(
36+
$"Please provide argument {nameof(type)} ({nameof(CalculationType)})"
37+
);
38+
}
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace Common.BasicHelper.Math;
2+
3+
public enum CalculationType
4+
{
5+
Unknown = 0,
6+
Add = 1,
7+
Substraction = 2,
8+
Multiply = 3,
9+
Division = 4,
10+
Power = 5,
11+
}
12+
13+
public class Expression
14+
{
15+
private CalculationType type = CalculationType.Unknown;
16+
17+
public CalculationType Type { get => type; set => type = value; }
18+
19+
public Expression? Left { get; set; }
20+
21+
public Expression? Right { get; set; }
22+
23+
private double? givenValue = null;
24+
25+
public double Result
26+
{
27+
get => givenValue ?? Calculator.Calculate(Left, Right, type);
28+
set
29+
{
30+
givenValue = value;
31+
}
32+
}
33+
34+
public static Expression FromValue(double value)
35+
{
36+
return new()
37+
{
38+
Result = value
39+
};
40+
}
41+
}
42+
43+

Common.BasicHelper/Math/Standard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static int GetPosition(int number, int bit)
6666
var pow = 10;
6767
for (var i = 0; i < bit - 2; ++i)
6868
pow *= pow;
69-
return (number % pow) / (pow / 10);
69+
return number % pow / (pow / 10);
7070
}
7171
}
7272

0 commit comments

Comments
 (0)