Skip to content

Commit 4c9e014

Browse files
committed
1.1.12版本,支持常量pi、e、true、false
1 parent 014b8dd commit 4c9e014

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ToolGood.Algorithm֧
1111
}
1212
var c = engine.TryEvaluate("2+3", 0);
1313
```
14+
支持常量`pi`,`e`,`true`,`false`
1415

1516
## 自定义参数
1617
``` csharp

ToolGood.Algorithm.Test/AlgorithmEngine/AlgorithmEngineTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public void Test()
3232
b = engine.TryEvaluate("false", false);
3333
Assert.AreEqual(false, b);
3434

35+
var b1 = engine.TryEvaluate("if(true,1,2)", 0);
36+
Assert.AreEqual(1, b1);
37+
38+
b1 = engine.TryEvaluate("if(false,1,2)", 0);
39+
Assert.AreEqual(2, b1);
40+
41+
var b2 = engine.TryEvaluate("pi*4", 0.0);
42+
Assert.AreEqual(Math.PI * 4, b2);
43+
b2 = engine.TryEvaluate("e*4", 0.0);
44+
Assert.AreEqual(Math.E * 4, b2);
3545
}
3646

3747
[Test]

ToolGood.Algorithm/AlgorithmEngine.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public bool Parse(string exp)
9292
}
9393
#endregion
9494

95-
optType = Operator.ConvertOperator(curOpt);
95+
optType = Operator.ConvertOperator(curOpt, texts[curPos]);
96+
if (optType== OperatorType.PARAMETER) {
97+
operands.Push(new Operand(curOpt));
98+
continue;
99+
}
96100
funcCount = optType != OperatorType.FUNC ? 0 : getFunctionCount(texts, curPos);
97101

98102
//若运算符堆栈为空,或者若运算符堆栈栈顶为左括号,则将当前运算符直接存入运算符堆栈.

ToolGood.Algorithm/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
3333
// 方法是按如下所示使用“*”: :
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.1.11.0")]
36-
[assembly: AssemblyFileVersion("1.1.11.0")]
35+
[assembly: AssemblyVersion("1.1.12.0")]
36+
[assembly: AssemblyFileVersion("1.1.12.0")]

ToolGood.Algorithm/_base/Operator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace ToolGood.Algorithm
88
/// <summary>
99
/// 运算符
1010
/// </summary>
11-
public class Operator
11+
internal class Operator
1212
{
1313
public Operator(OperatorType type, string value)
1414
{
1515
this.Type = type;
1616
this.Value = value;
1717
}
1818

19-
public Operator(OperatorType type, string value,int argsCount)
19+
public Operator(OperatorType type, string value, int argsCount)
2020
{
2121
this.Type = type;
2222
this.Value = value;
@@ -115,7 +115,7 @@ public Operator(OperatorType type, string value,int argsCount)
115115
/// </summary>
116116
/// <param name="opt">运算符</param>
117117
/// <returns>返回指定的运算符类型</returns>
118-
public static OperatorType ConvertOperator(string opt)
118+
public static OperatorType ConvertOperator(string opt, string next)
119119
{
120120
switch (opt) {
121121
//case "!": return OperatorType.NOT;
@@ -135,8 +135,19 @@ public static OperatorType ConvertOperator(string opt)
135135
case "&": return OperatorType.StringADD;
136136
case "|": return OperatorType.OR;
137137
case ",": return OperatorType.CA;
138-
default: return OperatorType.FUNC;
138+
default:break; // return OperatorType.FUNC;
139139
}
140+
switch (opt) {
141+
case "pi":
142+
case "e":
143+
case "true":
144+
case "false":
145+
if (next == "(") {
146+
return OperatorType.FUNC;
147+
}
148+
return OperatorType.PARAMETER;
149+
}
150+
return OperatorType.FUNC;
140151
}
141152

142153
/// <summary>

ToolGood.Algorithm/_base/OperatorType.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ToolGood.Algorithm
88
/// <summary>
99
/// 运算符类型(从上到下优先级依次递减),数值越大,优先级越低
1010
/// </summary>
11-
public enum OperatorType
11+
internal enum OperatorType
1212
{
1313
/// <summary>
1414
/// 左括号:(,left bracket
@@ -30,7 +30,10 @@ public enum OperatorType
3030
/// </summary>
3131
FUNC,
3232

33-
33+
/// <summary>
34+
/// 参数
35+
/// </summary>
36+
PARAMETER,
3437
///// <summary>
3538
///// 逻辑非,!,NOT
3639
///// </summary>

0 commit comments

Comments
 (0)