Skip to content

Commit e29dff2

Browse files
committed
添加EvaluateFormula
1 parent 0ac6883 commit e29dff2

File tree

14 files changed

+210
-13
lines changed

14 files changed

+210
-13
lines changed

README-EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Note: `find` is an Excel formula , find (the string to be searched, the string t
9090
c.TryEvaluate("[直径]*pi()", 0.0); //The length of the circle 圆的长
9191
c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //Volume of circle 圆的体积
9292
c.TryEvaluate("['半径']*[半径]*pi()*[高]", 0.0); //Volume of circle 圆的体积
93+
c.EvaluateFormula("'圆'-[半径]-高", '-'); // Return: 圆-3-10
94+
9395
```
9496
Parameters are defined in square brackets, such as `[parameter name]`.
9597

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ bool转数值,假为`0`,真为`1`。bool转字符串,假为`FALSE`,真
8989
c.TryEvaluate("[直径]*pi()", 0.0); //The length of the circle 圆的长
9090
c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //Volume of circle 圆的体积
9191
c.TryEvaluate("['半径']*[半径]*pi()*[高]", 0.0); //Volume of circle 圆的体积
92+
c.EvaluateFormula("'圆'-[半径]-高", '-'); // Return: 圆-3-10
93+
9294
```
9395

9496
参数以方括号定义,如 `[参数名]`

csharp/ToolGood.Algorithm2.Test/AlgorithmEngine/AlgorithmEngineTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ public void Cylinder_Test()
221221
var tt = c.TryEvaluate("['灰色']", ""); //圆的体积
222222
Assert.AreEqual("L", tt);
223223

224+
225+
String tt2 = c.EvaluateFormula("'圆'-[半径]-高", '-');
226+
Assert.AreEqual("圆-3-10", tt2);
227+
228+
224229
}
225230

226231
}

csharp/ToolGood.Algorithm2.Test/AlgorithmEngineEx/PriceAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public PriceAlgorithm(ConditionCache multiConditionCache, Desk desk) : base(mult
1010
_disk = desk;
1111
}
1212

13-
public override Operand GetParameter(string parameter)
13+
protected override Operand GetParameter(string parameter)
1414
{
1515
if (parameter == "长") {
1616
return Operand.Create(_disk.Length);

csharp/ToolGood.Algorithm2/AlgorithmEngine.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
5+
using System.Text;
46
using Antlr4.Runtime;
57
using Antlr4.Runtime.Misc;
68
using ToolGood.Algorithm.Internals;
@@ -23,7 +25,7 @@ public class AlgorithmEngine
2325
/// </summary>
2426
public string LastError { get; private set; }
2527
private ProgContext _context;
26-
private Dictionary<string, Operand> _dict = new Dictionary<string, Operand>();
28+
protected Dictionary<string, Operand> _dict = new Dictionary<string, Operand>();
2729
/// <summary>
2830
/// 自定义 函数
2931
/// </summary>
@@ -698,5 +700,44 @@ public MyDate TryEvaluate_MyDate(string exp, MyDate def)
698700
}
699701
#endregion
700702

703+
#region EvaluateFormula
704+
/// <summary>
705+
/// 计算公式
706+
/// </summary>
707+
/// <param name="formula">公式</param>
708+
/// <param name="splitChars">分隔符</param>
709+
/// <returns></returns>
710+
public String EvaluateFormula(String formula, params char[] splitChars)
711+
{
712+
if (string.IsNullOrEmpty(formula)) return "";
713+
return EvaluateFormula(formula, splitChars.ToList());
714+
}
715+
/// <summary>
716+
/// 计算公式
717+
/// </summary>
718+
/// <param name="formula">公式</param>
719+
/// <param name="splitChars">分隔符</param>
720+
/// <returns></returns>
721+
public String EvaluateFormula(String formula, List<char> splitChars)
722+
{
723+
if (string.IsNullOrEmpty(formula)) return "";
724+
725+
List<String> sp = CharUtil.SplitFormula(formula, splitChars);
726+
727+
StringBuilder stringBuilder = new StringBuilder();
728+
for (int i = 0; i < sp.Count; i++) {
729+
String s = sp[i];
730+
if (s.Length == 1 && splitChars.Contains(s[0])) {
731+
stringBuilder.Append(s);
732+
} else {
733+
// TODO 替换此处
734+
String d = TryEvaluate(s, "");
735+
stringBuilder.Append(d.ToString());
736+
}
737+
}
738+
return stringBuilder.ToString();
739+
}
740+
#endregion
741+
701742
}
702743
}

csharp/ToolGood.Algorithm2/Internals/CharUtil.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,60 @@ public static bool Equals(string left, string arg1, string arg2, string arg3)
7373
return false;
7474
}
7575

76+
public static List<String> SplitFormula(String formula, List<char> splitChars)
77+
{
78+
List<String> result = new List<String>();
79+
bool inSquareBrackets = false;
80+
bool inBraceBrackets = false;
81+
int inBracketsCount = 0;
82+
bool inText = false;
83+
char textChar = (char)0;
84+
85+
StringBuilder str = new StringBuilder();
86+
int i = 0;
87+
while (i < formula.Length) {
88+
char c = formula[i];
89+
if (inSquareBrackets) {
90+
str.Append(c);
91+
if (c == ']') inSquareBrackets = false;
92+
} else if (inBraceBrackets) {
93+
str.Append(c);
94+
if (c == '}') inBraceBrackets = false;
95+
} else if (inText) {
96+
str.Append(c);
97+
if (c == '\\') {
98+
i++;
99+
if (i < formula.Length) {
100+
str.Append(formula[i]);
101+
}
102+
} else if (c == textChar) {
103+
inText = false;
104+
}
105+
} else if (splitChars.Contains(c) && inBracketsCount == 0) {
106+
result.Add(str.ToString());
107+
result.Add(c.ToString());
108+
str = new StringBuilder();
109+
} else {
110+
str.Append(c);
111+
if (c == '\'' || c == '"' || c == '`') {
112+
textChar = c;
113+
inText = true;
114+
} else if (c == '[') {
115+
inSquareBrackets = true;
116+
} else if (c == '{') {
117+
inBraceBrackets = true;
118+
} else if (c == '(') {
119+
inBracketsCount++;
120+
} else if (c == ')') {
121+
inBracketsCount--;
122+
}
123+
}
124+
i++;
125+
}
126+
if (str.Length > 0)
127+
result.Add(str.ToString());
128+
return result;
129+
130+
}
76131
}
77132
}

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Product>ToolGood.Algorithm</Product>
2222
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2323
<SignAssembly>true</SignAssembly>
24-
<Version>3.0.0.1</Version>
24+
<Version>3.0.1.0</Version>
2525
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2626
<DelaySign>false</DelaySign>
2727
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

java/toolgood.algorithm/README-EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class Cylinder extends AlgorithmEngine {
8585
c.TryEvaluate("[直径]*pi()", 0.0); //圆的长
8686
c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //圆的体积
8787
c.TryEvaluate("['半径']*[半径]*pi()*[高]", 0.0); //圆的体积
88+
c.EvaluateFormula("'圆'-[半径]-高", '-'); // Return: 圆-3-10
8889
```
8990
Parameters are defined in square brackets, such as `[parameter name]`.
9091

java/toolgood.algorithm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class Cylinder extends AlgorithmEngine {
8282
c.TryEvaluate("[直径]*pi()", 0.0); //圆的长
8383
c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //圆的体积
8484
c.TryEvaluate("['半径']*[半径]*pi()*[高]", 0.0); //圆的体积
85+
c.EvaluateFormula("'圆'-[半径]-高", '-'); // Return: 圆-3-10
8586
```
8687
参数以方括号定义,如 `[参数名]`
8788

java/toolgood.algorithm/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>toolgood</groupId>
88
<artifactId>toolgood-algorithm</artifactId>
9-
<version>3.0.0.1</version>
9+
<version>3.0.1.0</version>
1010

1111
<name>toolgood.algorithm</name>
1212
<url>https://github.com/toolgood/ToolGood.Algorithm</url>

0 commit comments

Comments
 (0)