Skip to content

Commit 2781433

Browse files
committed
修改
1 parent de3b716 commit 2781433

File tree

8 files changed

+6846
-6494
lines changed

8 files changed

+6846
-6494
lines changed

csharp/ToolGood.Algorithm2/Internals/LookupAlgorithmEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected override Operand GetParameter(string parameter)
2424
if (v.IsLong) return Operand.Create(double.Parse(v.ToString(), NumberStyles.Any, cultureInfo));
2525
if (v.IsObject) return Operand.Create(v);
2626
if (v.IsArray) return Operand.Create(v);
27+
if (v.IsNull) return Operand.CreateNull();
2728
return Operand.Create(v);
2829
}
2930
return base.GetParameter(parameter);

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ public Operand VisitAddSub_fun([NotNull] mathParser.AddSub_funContext context)
118118
var t = context.op.Text;
119119

120120
if (t == "&") {
121+
if (firstValue.IsNull && secondValue.IsNull) {
122+
return firstValue;
123+
} else if (firstValue.IsNull) {
124+
secondValue = secondValue.ToString($"Function '{t}' parameter 2 is error!");
125+
return secondValue;
126+
} else if (secondValue.IsNull) {
127+
firstValue = firstValue.ToString($"Function '{t}' parameter 1 is error!");
128+
return firstValue;
129+
}
130+
121131
firstValue = firstValue.ToString($"Function '{t}' parameter 1 is error!");
122132
if (firstValue.IsError) { return secondValue; }
123133
secondValue = secondValue.ToString($"Function '{t}' parameter 2 is error!");
@@ -187,6 +197,20 @@ public Operand VisitJudge_fun([NotNull] mathParser.Judge_funContext context)
187197
var secondValue = args[1];
188198
string type = context.op.Text;
189199

200+
if (firstValue.IsNull) {
201+
if (secondValue.IsNull && (type=="==" || type == "=")) {
202+
return Operand.True;
203+
} else if (secondValue.IsNull==false && (type == "<>" || type == "!=")) {
204+
return Operand.True;
205+
}
206+
return Operand.False;
207+
} else if (secondValue.IsNull) {
208+
if (type == "==" || type == "=") {
209+
return Operand.False;
210+
}
211+
return Operand.True;
212+
}
213+
190214
int r;
191215
if (firstValue.Type == secondValue.Type) {
192216
if (firstValue.Type == OperandType.STRING || firstValue.Type == OperandType.JSON) {
@@ -315,7 +339,6 @@ public Operand VisitIFERROR_fun([NotNull] mathParser.IFERROR_funContext context)
315339
return args[2];
316340
}
317341
return Operand.False;
318-
319342
}
320343
public Operand VisitISNUMBER_fun([NotNull] mathParser.ISNUMBER_funContext context)
321344
{
@@ -339,8 +362,50 @@ public Operand VisitISTEXT_fun([NotNull] mathParser.ISTEXT_funContext context)
339362
}
340363
public Operand VisitISERROR_fun([NotNull] mathParser.ISERROR_funContext context)
341364
{
342-
var firstValue = this.Visit(context.expr());
343-
if (firstValue.Type == OperandType.ERROR) {
365+
var args = new List<Operand>();
366+
foreach (var item in context.expr()) { var aa = this.Visit(item); args.Add(aa); }
367+
if (args.Count==2) {
368+
if (args[0].IsError) {
369+
return args[1];
370+
}
371+
return args[0];
372+
}
373+
374+
if (args[0].IsError) {
375+
return Operand.True;
376+
}
377+
return Operand.False;
378+
}
379+
380+
public Operand VisitISNULL_fun([NotNull] mathParser.ISNULL_funContext context)
381+
{
382+
var args = new List<Operand>();
383+
foreach (var item in context.expr()) { var aa = this.Visit(item); args.Add(aa); }
384+
385+
if (args.Count == 2) {
386+
if (args[0].IsNull) {
387+
return args[1];
388+
}
389+
return args[0];
390+
}
391+
if (args[0].IsNull) {
392+
return Operand.True;
393+
}
394+
return Operand.False;
395+
}
396+
397+
public Operand VisitISNULLORERROR_fun([NotNull] mathParser.ISNULLORERROR_funContext context)
398+
{
399+
var args = new List<Operand>();
400+
foreach (var item in context.expr()) { var aa = this.Visit(item); args.Add(aa); }
401+
402+
if (args.Count == 2) {
403+
if (args[0].IsNullOrError) {
404+
return args[1];
405+
}
406+
return args[0];
407+
}
408+
if (args[0].IsNullOrError) {
344409
return Operand.True;
345410
}
346411
return Operand.False;
@@ -2744,17 +2809,17 @@ private double F_base_sumif(List<double> dbs, string s, List<double> sumdbs)
27442809
private bool F_base_compare(double a, double b, string ss)
27452810
{
27462811
if (ss == "<") {
2747-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) < Math.Round(b, 12, MidpointRounding.AwayFromZero);
2812+
return Math.Round(a-b, 12, MidpointRounding.AwayFromZero) < 0;
27482813
} else if (ss == "<=") {
2749-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) <= Math.Round(b, 12, MidpointRounding.AwayFromZero);
2814+
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) <= 0;
27502815
} else if (ss == ">") {
2751-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) > Math.Round(b, 12, MidpointRounding.AwayFromZero);
2816+
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) > 0;
27522817
} else if (ss == ">=") {
2753-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) >= Math.Round(b, 12, MidpointRounding.AwayFromZero);
2818+
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) >= 0;
27542819
} else if (ss == "=" || ss == "==") {
2755-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) == Math.Round(b, 12, MidpointRounding.AwayFromZero);
2820+
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) == 0;
27562821
}
2757-
return Math.Round(a, 12, MidpointRounding.AwayFromZero) != Math.Round(b, 12, MidpointRounding.AwayFromZero);
2822+
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) !=0;
27582823
}
27592824

27602825
private bool F_base_GetList(List<Operand> args, List<double> list)
@@ -3602,6 +3667,10 @@ public Operand VisitSTRING_fun([NotNull] mathParser.STRING_funContext context)
36023667
}
36033668
return Operand.Create(sb.ToString());
36043669
}
3670+
public Operand VisitNULL_fun([NotNull] mathParser.NULL_funContext context)
3671+
{
3672+
return Operand.CreateNull();
3673+
}
36053674
public Operand VisitPARAMETER_fun([NotNull] mathParser.PARAMETER_funContext context)
36063675
{
36073676
var p = this.Visit(context.parameter()).ToString("Function PARAMETER first parameter is error!");
@@ -3664,6 +3733,7 @@ public Operand VisitGetJsonValue_fun([NotNull] mathParser.GetJsonValue_funContex
36643733
if (v.IsLong) return Operand.Create(double.Parse(v.ToString(), NumberStyles.Any, cultureInfo));
36653734
if (v.IsObject) return Operand.Create(v);
36663735
if (v.IsArray) return Operand.Create(v);
3736+
if (v.IsNull) return Operand.CreateNull();
36673737
return Operand.Create(v);
36683738
}
36693739
return Operand.Error($"JSON index {index} greater than maximum length!");
@@ -3680,6 +3750,7 @@ public Operand VisitGetJsonValue_fun([NotNull] mathParser.GetJsonValue_funContex
36803750
if (v.IsLong) return Operand.Create(double.Parse(v.ToString(), NumberStyles.Any, cultureInfo));
36813751
if (v.IsObject) return Operand.Create(v);
36823752
if (v.IsArray) return Operand.Create(v);
3753+
if (v.IsNull) return Operand.CreateNull();
36833754
return Operand.Create(v);
36843755
}
36853756
}
@@ -3695,6 +3766,7 @@ public Operand VisitExpr2_fun([NotNull] mathParser.Expr2_funContext context)
36953766

36963767

36973768

3769+
36983770
#endregion
36993771
}
37003772
}

csharp/ToolGood.Algorithm2/Operand.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public abstract class Operand : IDisposable
1414
public static readonly Operand True = Operand.Create(true);
1515
public static readonly Operand False = Operand.Create(false);
1616

17+
public virtual bool IsNull => false;
18+
public virtual bool IsNullOrError => false;
1719
public virtual bool IsError => false;
1820
public virtual string ErrorMsg => null;
1921
public abstract OperandType Type { get; }
@@ -45,14 +47,11 @@ public static Operand Create(string obj)
4547
}
4648
public static Operand CreateJson(string txt)
4749
{
48-
if ((txt.StartsWith("{") && txt.EndsWith("}")) || (txt.StartsWith("[") && txt.EndsWith("]")))
49-
{
50-
try
51-
{
50+
if ((txt.StartsWith("{") && txt.EndsWith("}")) || (txt.StartsWith("[") && txt.EndsWith("]"))) {
51+
try {
5252
var json = JsonMapper.ToObject(txt);
5353
return Operand.Create(json);
54-
}
55-
catch (Exception) { }
54+
} catch (Exception) { }
5655
}
5756
return Operand.Error("string to json is error!");
5857
}
@@ -115,6 +114,10 @@ public static Operand Error(string msg)
115114
return new OperandError(msg);
116115
}
117116

117+
public static Operand CreateNull()
118+
{
119+
return new OperandNull();
120+
}
118121

119122
#endregion
120123
public Operand ToNumber(string errorMessage)
@@ -172,14 +175,11 @@ public Operand ToJson(string errorMessage)
172175
if (IsError) { return this; }
173176
if (Type == OperandType.STRING) {
174177
var txt = StringValue;
175-
if ((txt.StartsWith("{") && txt.EndsWith("}")) || (txt.StartsWith("[") && txt.EndsWith("]")))
176-
{
177-
try
178-
{
178+
if ((txt.StartsWith("{") && txt.EndsWith("}")) || (txt.StartsWith("[") && txt.EndsWith("]"))) {
179+
try {
179180
var json = JsonMapper.ToObject(txt);
180181
return Operand.Create(json);
181-
}
182-
catch (Exception) { }
182+
} catch (Exception) { }
183183
}
184184
}
185185
return Error(errorMessage);
@@ -210,6 +210,7 @@ public Operand ToArray(string errorMessage)
210210
}
211211
return Error(errorMessage);
212212
}
213+
213214

214215
public void Dispose() { }
215216
}
@@ -222,47 +223,48 @@ public Operand(T obj)
222223
}
223224
}
224225

225-
public class OperandNumber : Operand<double>
226+
class OperandNumber : Operand<double>
226227
{
227228
public OperandNumber(double obj) : base(obj) { }
228229
public override OperandType Type => OperandType.NUMBER;
229230
public override int IntValue => (int)Value;
230231
public override double NumberValue => Value;
231232
}
232-
public class OperandBoolean : Operand<bool>
233+
class OperandBoolean : Operand<bool>
233234
{
234235
public OperandBoolean(bool obj) : base(obj) { }
235236
public override OperandType Type => OperandType.BOOLEAN;
236237
public override bool BooleanValue => Value;
237238
}
238-
public class OperandString : Operand<string>
239+
class OperandString : Operand<string>
239240
{
240241
public OperandString(string obj) : base(obj) { }
241242
public override OperandType Type => OperandType.STRING;
242243
public override string StringValue => Value;
243244
}
244-
public class OperandDate : Operand<Date>
245+
class OperandDate : Operand<Date>
245246
{
246247
public OperandDate(Date obj) : base(obj) { }
247248
public override OperandType Type => OperandType.DATE;
248249
public override Date DateValue => Value;
249250
}
250-
internal class OperandJson : Operand<JsonData>
251+
class OperandJson : Operand<JsonData>
251252
{
252253
public OperandJson(JsonData obj) : base(obj) { }
253254
public override OperandType Type => OperandType.JSON;
254255
internal override JsonData JsonValue => Value;
255256
}
256-
public class OperandArray : Operand<List<Operand>>
257+
class OperandArray : Operand<List<Operand>>
257258
{
258259
public OperandArray(List<Operand> obj) : base(obj) { }
259260
public override OperandType Type => OperandType.ARRARY;
260261
public override List<Operand> ArrayValue => Value;
261262
}
262-
public class OperandError : Operand
263+
class OperandError : Operand
263264
{
264265
public override OperandType Type => OperandType.ERROR;
265266
public override bool IsError => true;
267+
public override bool IsNullOrError => true;
266268
private string _errorMsg;
267269
public override string ErrorMsg => _errorMsg;
268270
public OperandError(string msg)
@@ -271,4 +273,12 @@ public OperandError(string msg)
271273
}
272274
}
273275

276+
class OperandNull : Operand
277+
{
278+
public override OperandType Type => OperandType.NULL;
279+
public override bool IsNullOrError => true;
280+
public override bool IsNull => true;
281+
282+
}
283+
274284
}

csharp/ToolGood.Algorithm2/OperandType.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
public enum OperandType
77
{
88
/// <summary>
9-
/// 函数
9+
/// NULL
10+
/// </summary>
11+
NULL,
12+
/// <summary>
13+
/// 错误
1014
/// </summary>
1115
ERROR,
1216

0 commit comments

Comments
 (0)