Skip to content

Commit bed8c2d

Browse files
committed
1.3.1.6版本
1 parent 4916a8c commit bed8c2d

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,4 +816,7 @@ ToolGood.Algorithm֧
816816
<tr>
817817
<td>RemoveBoth</td><td>RemoveBoth(文本,左边文本,右边文本,同时匹配:0/1(默认0))<br>匹配方式, 匹配左边,成功则去除左边字符串。匹配右边,成功则去除右边字符串。</td> <td></td>
818818
</tr>
819+
<tr>
820+
<td>P<br>param</td><td>P(文本)<br>动态查询参数。</td> <td></td>
821+
</tr>
819822
</table>

ToolGood.Algorithm.Test/AlgorithmEngine/AlgorithmEngineTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ public void Cylinder_Test()
121121
c.TryEvaluate("[直径]*pi()", 0.0); //圆的长
122122
c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //圆的体积
123123

124-
var t= c.TryEvaluate("log([半径])", 0.0);
124+
if (c.Parse("[直径1]*pi()")==false) {
125+
Assert.AreEqual("参数[直径1]无效!", c.LastError);
126+
}
127+
128+
c.TryEvaluate("p('半径')*[半径]*pi()*[高]", 0.0); //圆的体积
129+
130+
131+
125132
}
126133

127134
}

ToolGood.Algorithm/AlgorithmEngine.cs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ public partial class AlgorithmEngine
1414
private Dictionary<string, Func<List<Operand>, Operand>> funcDict = new Dictionary<string, Func<List<Operand>, Operand>>();
1515
private bool _useExcelIndex = true;
1616
private int excelIndex = 1;
17+
/// <summary>
18+
/// 使用EXCEL索引
19+
/// </summary>
1720
public bool UseExcelIndex { get { return _useExcelIndex; } set { _useExcelIndex = value; excelIndex = value ? 1 : 0; } }
21+
/// <summary>
22+
/// 最后一个错误
23+
/// </summary>
24+
public string LastError { get; private set; }
1825

1926
public AlgorithmEngine()
2027
{
@@ -47,22 +54,63 @@ protected void AddFunction(string funName, Func<List<Operand>, Operand> function
4754

4855
public bool Parse(string exp)
4956
{
50-
m_tokens.Clear();//清空语法单元堆栈
5157
if (string.IsNullOrEmpty(exp) || exp.Trim() == "" || !this.isMatching(exp)) {
58+
LastError = "exp无效";
5259
return false;
5360
}
54-
m_tokens = parse(exp);
61+
string error;
62+
m_tokens = parse(exp, out error);
5563
return true;
5664
}
65+
public bool Parse(string exp ,out string error)
66+
{
67+
if (string.IsNullOrEmpty(exp) || exp.Trim() == "" || !this.isMatching(exp)) {
68+
error = "exp无效";
69+
LastError = "exp无效";
70+
return false;
71+
}
72+
m_tokens = parse(exp, out error);
73+
return true;
74+
}
75+
5776
public bool Parse(string name, string exp)
5877
{
5978
if (string.IsNullOrEmpty(exp) || exp.Trim() == "" || !this.isMatching(exp)) {
79+
LastError = "exp无效";
80+
return false;
81+
}
82+
string error;
83+
tokenDict[name] = parse(exp,out error);
84+
LastError = error;
85+
return true;
86+
}
87+
public bool Parse(string name, string exp, out string error)
88+
{
89+
if (string.IsNullOrEmpty(exp) || exp.Trim() == "" || !this.isMatching(exp)) {
90+
error = "exp无效";
91+
LastError = "exp无效";
6092
return false;
6193
}
62-
tokenDict[name] = parse(exp);
94+
tokenDict[name] = parse(exp, out error);
95+
LastError = error;
6396
return true;
6497
}
6598

99+
100+
private Stack<object> parse(string exp,out string error)
101+
{
102+
error = null;
103+
var tmp = parse(exp);
104+
var names = GetParameterNames(tmp);
105+
foreach (var item in names) {
106+
if (GetParameter(new Operand(item)) == null) {
107+
error = $"参数{item}无效!";
108+
return null;
109+
}
110+
}
111+
return tmp;
112+
}
113+
66114
private Stack<object> parse(string exp)
67115
{
68116
Stack<object> operands = new Stack<object>(); //操作数堆栈
@@ -350,9 +398,13 @@ private List<string> splitText(string exp)
350398
#region GetParameterNames
351399

352400
public List<string> GetParameterNames()
401+
{
402+
return GetParameterNames(m_tokens);
403+
}
404+
private List<string> GetParameterNames(Stack<object> tokens )
353405
{
354406
List<string> list = new List<string>();
355-
foreach (var item in m_tokens) {
407+
foreach (var item in tokens) {
356408
var curOpd = item as Operand;
357409
if (curOpd != null) {
358410
if (curOpd.Type == OperandType.PARAMETER) {
@@ -364,6 +416,7 @@ public List<string> GetParameterNames()
364416
return list;
365417
}
366418

419+
367420
#endregion
368421

369422
#region Evaluate
@@ -491,6 +544,7 @@ private object evaluate(Stack<object> tokens)
491544
if (opds.Count == 1) {
492545
var outopd = opds.Pop();
493546
if (outopd.Type == OperandType.ERROR) {
547+
LastError = outopd.StringValue;
494548
throw new Exception(outopd.StringValue);
495549
}
496550
value = outopd.Value;

ToolGood.Algorithm/AlgorithmEngine.csharp.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ private void AddCSharp()
6262

6363
addFunc("ToUpper", Func_Upper); //将文本转换为大写形式
6464
addFunc("ToLower", Func_Lower); //将文本参数转换为数字
65+
66+
addFunc("P", Func_P);
67+
addFunc("param", Func_P);
68+
}
69+
70+
private Operand Func_P(List<Operand> arg)
71+
{
72+
CheckArgsCount("P", arg, new OperandType[][] { new OperandType[] { OperandType.STRING } });
73+
if (arg[0].StringValue.StartsWith("[")) {
74+
return GetParameter(new Operand(OperandType.Any, arg[0].StringValue));
75+
}
76+
return GetParameter(new Operand(OperandType.Any, "[" + arg[0].StringValue + "]"));
6577
}
6678

6779
private Operand Func_UrlDecode(List<Operand> arg)

ToolGood.Algorithm/ToolGood.Algorithm.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
<RootNamespace>ToolGood.Algorithm</RootNamespace>
2424
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2525
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
26-
<FileVersion>1.3.1.5</FileVersion>
27-
<Version>1.3.1.5</Version>
26+
<FileVersion>1.3.1.6</FileVersion>
27+
<Version>1.3.1.6</Version>
2828
<PackageReleaseNotes></PackageReleaseNotes>
2929
<Product>ToolGood.Algorithm</Product>
30-
<AssemblyVersion>1.3.1.5</AssemblyVersion>
30+
<AssemblyVersion>1.3.1.6</AssemblyVersion>
3131
</PropertyGroup>
3232

3333
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">

0 commit comments

Comments
 (0)