Skip to content

Commit 0798e79

Browse files
committed
更新 整理 and or
程序 && and || or 与 excel的 AND(x,y) OR(x,y) 有区别 在excel内 AND(x,y) OR(x,y) 先报错, 在程序中,&& and 有true 直接返回true 就不会检测下一个会不会报错 在程序中,|| or 有false 直接返回false 就不会检测下一个会不会报错
1 parent cbc7205 commit 0798e79

File tree

6 files changed

+62
-102
lines changed

6 files changed

+62
-102
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public void Test()
124124
Assert.AreEqual(t1, 2);
125125
t1 = engine.TryEvaluate("1>2?1:2", 0);
126126
Assert.AreEqual(t1, 2);
127+
128+
129+
130+
var t2 = engine.TryEvaluate("Asc('abcABC123')", "");
131+
Assert.AreEqual(t2, "abcABC123");
127132
}
128133

129134
[Test]

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,21 @@ private int Compare(double t1, double t2)
291291

292292
public Operand VisitAndOr_fun(mathParser.AndOr_funContext context)
293293
{
294+
// 程序 && and || or 与 excel的 AND(x,y) OR(x,y) 有区别
295+
// 在excel内 AND(x,y) OR(x,y) 先报错,
296+
// 在程序中,&& and 有true 直接返回true 就不会检测下一个会不会报错
297+
// 在程序中,|| or 有false 直接返回false 就不会检测下一个会不会报错
294298
var t = context.op.Text;
295-
var args = new List<Operand>();
296-
var index = 1;
297-
foreach (var item in context.expr()) { var aa = this.Visit(item).ToBoolean($"Function '{t}' parameter {index++} is error!"); ; if (aa.IsError) { return aa; } args.Add(aa); }
298-
299-
var firstValue = args[0];
300-
var secondValue = args[1];
299+
var first = this.Visit(context.expr(0)).ToBoolean($"Function '{t}' parameter 1 is error!");
300+
if (first.IsError) { return first; }
301301
if (t == "&&" || t.Equals("and", StringComparison.OrdinalIgnoreCase)) {
302-
if (firstValue.BooleanValue && secondValue.BooleanValue) {
303-
return Operand.True;
304-
}
305-
return Operand.False;
306-
}
307-
if (firstValue.BooleanValue || secondValue.BooleanValue) {
308-
return Operand.True;
302+
if (first.BooleanValue == false) return Operand.False;
303+
} else {
304+
if (first.BooleanValue) return Operand.True;
309305
}
310-
return Operand.False;
306+
return this.Visit(context.expr(1)).ToBoolean($"Function '{t}' parameter 2 is error!");
311307
}
312308

313-
314309
#endregion
315310

316311
#region flow
@@ -460,41 +455,25 @@ public Operand VisitISNONTEXT_fun(mathParser.ISNONTEXT_funContext context)
460455

461456
public Operand VisitAND_fun(mathParser.AND_funContext context)
462457
{
463-
var args = new List<Operand>();
464458
var index = 1;
459+
bool b = true;
465460
foreach (var item in context.expr()) {
466461
var a = this.Visit(item).ToBoolean($"Function AND parameter {index++} is error!");
467462
if (a.IsError) { return a; }
468-
args.Add(a);
463+
if (a.BooleanValue == false) b = false;
469464
}
470-
471-
var b = true;
472-
foreach (var item in args) {
473-
if (item.BooleanValue == false) {
474-
b = false;
475-
break;
476-
}
477-
}
478-
return Operand.Create(b);
465+
return b ? Operand.True : Operand.False;
479466
}
480467
public Operand VisitOR_fun(mathParser.OR_funContext context)
481468
{
482-
var args = new List<Operand>();
483469
var index = 1;
470+
bool b = false;
484471
foreach (var item in context.expr()) {
485472
var a = this.Visit(item).ToBoolean($"Function OR parameter {index++} is error!");
486473
if (a.IsError) { return a; }
487-
args.Add(a);
474+
if (a.BooleanValue) b = true;
488475
}
489-
490-
var b = false;
491-
foreach (var item in args) {
492-
if (item.BooleanValue == true) {
493-
b = true;
494-
break;
495-
}
496-
}
497-
return Operand.Create(b);
476+
return b ? Operand.True : Operand.False;
498477
}
499478
public Operand VisitNOT_fun(mathParser.NOT_funContext context)
500479
{
@@ -3835,7 +3814,7 @@ public Operand VisitPARAMETER_fun(mathParser.PARAMETER_funContext context)
38353814
return GetParameter(str.Substring(1, str.Length - 2));
38363815
}
38373816
//防止 多重引用
3838-
if (context.expr()!=null) {
3817+
if (context.expr() != null) {
38393818
var p = this.Visit(context.expr()).ToText("Function PARAMETER first parameter is error!");
38403819
if (p.IsError) return p;
38413820

@@ -3867,7 +3846,7 @@ public Operand VisitGetJsonValue_fun(mathParser.GetJsonValue_funContext context)
38673846

38683847
var obj = firstValue;
38693848
Operand op;
3870-
if (context.parameter2()!=null) {
3849+
if (context.parameter2() != null) {
38713850
op = this.Visit(context.parameter2());
38723851
} else {
38733852
op = this.Visit(context.expr(1));

csharp/ToolGood.Algorithm2/math/mathParser.2.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,11 @@ public partial class AndOr_funContext : ExprContext
15511551
{
15521552
public IToken op;
15531553
[System.Diagnostics.DebuggerNonUserCode]
1554+
public ExprContext expr(int i)
1555+
{
1556+
return GetRuleContext<ExprContext>(i);
1557+
}
1558+
[System.Diagnostics.DebuggerNonUserCode]
15541559
public ExprContext[] expr()
15551560
{
15561561
return GetRuleContexts<ExprContext>();

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/MathVisitor.java

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.math.BigDecimal;
44
import java.net.URLDecoder;
55
import java.net.URLEncoder;
6+
import java.security.cert.TrustAnchor;
67
import java.text.DecimalFormat;
78
import java.util.ArrayList;
89
import java.util.HashMap;
@@ -383,30 +384,22 @@ private int Compare(final double t1, final double t2) {
383384
}
384385

385386
public Operand visitAndOr_fun(final AndOr_funContext context) {
387+
// 程序 && and || or 与 excel的 AND(x,y) OR(x,y) 有区别
388+
// 在excel内 AND(x,y) OR(x,y) 先报错,
389+
// 在程序中,&& and 有true 直接返回true 就不会检测下一个会不会报错
390+
// 在程序中,|| or 有false 直接返回false 就不会检测下一个会不会报错
386391
final String t = context.op.getText();
387-
final List<Operand> args = new ArrayList<Operand>();
388-
int index = 1;
389-
for (final ExprContext item : context.expr()) {
390-
final Operand aa = visit(item).ToBoolean("Function '" + t + "' parameter " + (index++) + " is error!");
391-
;
392-
if (aa.IsError()) {
393-
return aa;
394-
}
395-
args.add(aa);
396-
}
397-
398-
final Operand firstValue = args.get(0);
399-
final Operand secondValue = args.get(1);
392+
final Operand first = this.visit(context.expr(0)).ToBoolean("Function '" + t + "' parameter 1 is error!");
393+
if (first.IsError())
394+
return first;
400395
if (t.equals("&&") || t.toLowerCase().equals("and")) {
401-
if (firstValue.BooleanValue() && secondValue.BooleanValue()) {
396+
if (first.BooleanValue() == false)
397+
return Operand.False;
398+
} else {
399+
if (first.BooleanValue())
402400
return Operand.True;
403-
}
404-
return Operand.False;
405401
}
406-
if (firstValue.BooleanValue() || secondValue.BooleanValue()) {
407-
return Operand.True;
408-
}
409-
return Operand.False;
402+
return this.visit(context.expr(1)).ToBoolean("Function '" + t + "' parameter 2 is error!");
410403
}
411404

412405
public Operand visitIF_fun(final IF_funContext context) {
@@ -570,45 +563,29 @@ public Operand visitISNONTEXT_fun(final ISNONTEXT_funContext context) {
570563
}
571564

572565
public Operand visitAND_fun(final AND_funContext context) {
573-
final List<Operand> args = new ArrayList<Operand>();
574566
int index = 1;
567+
Boolean b = true;
575568
for (final ExprContext item : context.expr()) {
576569
final Operand a = visit(item).ToBoolean("Function AND parameter " + (index++) + " is error!");
577-
if (a.IsError()) {
570+
if (a.IsError())
578571
return a;
579-
}
580-
args.add(a);
581-
}
582-
583-
boolean b = true;
584-
for (final Operand item : args) {
585-
if (item.BooleanValue() == false) {
572+
if (a.BooleanValue() == false)
586573
b = false;
587-
break;
588-
}
589574
}
590-
return Operand.Create(b);
575+
return b ? Operand.True : Operand.False;
591576
}
592577

593578
public Operand visitOR_fun(final OR_funContext context) {
594-
final List<Operand> args = new ArrayList<Operand>();
595579
int index = 1;
580+
Boolean b = false;
596581
for (final ExprContext item : context.expr()) {
597582
final Operand a = visit(item).ToBoolean("Function OR parameter " + (index++) + " is error!");
598-
if (a.IsError()) {
583+
if (a.IsError())
599584
return a;
600-
}
601-
args.add(a);
602-
}
603-
604-
boolean b = false;
605-
for (final Operand item : args) {
606-
if (item.BooleanValue() == true) {
585+
if (a.BooleanValue())
607586
b = true;
608-
break;
609-
}
610587
}
611-
return Operand.Create(b);
588+
return b ? Operand.True : Operand.False;
612589
}
613590

614591
public Operand visitNOT_fun(final NOT_funContext context) {
@@ -808,7 +785,9 @@ public Operand visitPERMUT_fun(final PERMUT_funContext context) {
808785

809786
public Operand visitPercentage_fun(Percentage_funContext context) {
810787
Operand firstValue = this.visit(context.expr()).ToNumber("Function Percentage parameter is error!");
811-
if (firstValue.IsError()) { return firstValue; }
788+
if (firstValue.IsError()) {
789+
return firstValue;
790+
}
812791

813792
return Operand.Create(firstValue.NumberValue() / 100.0);
814793
}
@@ -5343,12 +5322,13 @@ public Operand visitPARAMETER_fun(final PARAMETER_funContext context) {
53435322
if (str.startsWith("@")) {
53445323
return GetParameter.apply(str.substring(1));
53455324
}
5346-
return GetParameter.apply(str.substring(1, str.length()- 1));
5325+
return GetParameter.apply(str.substring(1, str.length() - 1));
53475326
}
5348-
//防止 多重引用
5349-
if (context.expr()!=null) {
5327+
// 防止 多重引用
5328+
if (context.expr() != null) {
53505329
Operand p = this.visit(context.expr()).ToText("Function PARAMETER first parameter is error!");
5351-
if (p.IsError()) return p;
5330+
if (p.IsError())
5331+
return p;
53525332

53535333
if (GetParameter != null) {
53545334
return GetParameter.apply(p.TextValue());
@@ -5377,7 +5357,7 @@ public Operand visitGetJsonValue_fun(final GetJsonValue_funContext context) {
53775357

53785358
final Operand obj = firstValue;
53795359
Operand op;
5380-
if (context.parameter2()!=null) {
5360+
if (context.parameter2() != null) {
53815361
op = this.visit(context.parameter2());
53825362
} else {
53835363
op = this.visit(context.expr(1));
@@ -5788,7 +5768,4 @@ private static int removeWhiteSpace(char[] data) {
57885768
}
57895769
}
57905770

5791-
5792-
5793-
57945771
}

java/toolgood.algorithm/src/main/java/toolgood/algorithm/math/mathParser2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,9 @@ public <T> T accept(final ParseTreeVisitor<? extends T> visitor) {
11361136
}
11371137
public static class AndOr_funContext extends ExprContext {
11381138
public Token op;
1139-
public List<ExprContext> expr() {
1140-
return getRuleContexts(ExprContext.class);
1141-
}
1139+
public ExprContext expr(int i) {
1140+
return getRuleContext(ExprContext.class,i);
1141+
}
11421142
public AndOr_funContext(final ExprContext ctx) {
11431143
copyFrom(ctx);
11441144
}

java/toolgood.algorithm/src/test/java/toolgood/algorithm/Tests/AlgorithmEngineTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotEquals;
55

6-
import java.math.BigDecimal;
76

87
import org.joda.time.DateTime;
98
import org.joda.time.DateTimeZone;
109
import org.junit.Test;
1110

1211
import toolgood.algorithm.AlgorithmEngine;
1312

14-
@SuppressWarnings("deprecation")
1513
public class AlgorithmEngineTest {
1614

1715
@Test
@@ -73,7 +71,7 @@ public void Test() throws Exception
7371
s = engine.TryEvaluate("'3'+2", "");
7472
assertEquals("5", s);
7573

76-
int r = engine.TryEvaluate("count({ 1,2,3,4})", 0);
74+
int r = engine.TryEvaluate("count(array(1,2,3,4))", 0);
7775
assertEquals(4, r);
7876

7977

@@ -201,8 +199,4 @@ public void Cylinder_Test() throws Exception
201199
}
202200

203201

204-
private double round(final double value, final int p) {
205-
final BigDecimal bigD = new BigDecimal(value);
206-
return bigD.setScale(p, BigDecimal.ROUND_HALF_UP).doubleValue();
207-
}
208202
}

0 commit comments

Comments
 (0)