Skip to content

Commit b70455f

Browse files
committed
添加 LongValue
1 parent 6dffd56 commit b70455f

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class MathVisitor : AbstractParseTreeVisitor<Operand>, ImathVisitor<Operand>
1818
private static readonly Regex bit_8 = new Regex("^[0-8]+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
1919
private static readonly Regex bit_16 = new Regex("^[0-9a-f]+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2020
private static readonly Regex clearRegex = new Regex(@"[\f\n\r\t\v]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
21-
private static readonly Regex numberRegex = new Regex(@"^-?(0|[1-9])\d*(\.\d+)?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2221
private static readonly CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US");
2322
public event Func<string, Operand> GetParameter;
2423
public event Func<string, List<Operand>, Operand> DiyFunction;
@@ -190,13 +189,13 @@ public virtual Operand VisitJudge_fun(mathParser.Judge_funContext context)
190189
int r;
191190
if (args1.Type == args2.Type) {
192191
if (args1.Type == OperandType.NUMBER) {
193-
return Compare(args1, args2, type);
192+
return MathVisitor.Compare(args1, args2, type);
194193
} else if (args1.Type == OperandType.TEXT) {
195194
r = string.CompareOrdinal(args1.TextValue, args2.TextValue);
196195
} else if (args1.Type == OperandType.DATE || args1.Type == OperandType.BOOLEAN) {
197196
args1 = args1.ToNumber();
198197
args2 = args2.ToNumber();
199-
return Compare(args1, args2, type);
198+
return MathVisitor.Compare(args1, args2, type);
200199
} else if (args1.Type == OperandType.JSON) {
201200
args1 = args1.ToText();
202201
args2 = args2.ToText();
@@ -252,7 +251,7 @@ public virtual Operand VisitJudge_fun(mathParser.Judge_funContext context)
252251
if (args1.Type != OperandType.NUMBER) { args1 = args1.ToNumber($"Function '{type}' parameter 1 is error!"); if (args1.IsError) { return args1; } }
253252
if (args2.Type != OperandType.NUMBER) { args2 = args2.ToNumber($"Function '{type}' parameter 2 is error!"); if (args2.IsError) { return args2; } }
254253

255-
return Compare(args1, args2, type);
254+
return MathVisitor.Compare(args1, args2, type);
256255
}
257256
if (type.Length == 1) {
258257
if (CharUtil.Equals(type, '<')) {
@@ -271,7 +270,7 @@ public virtual Operand VisitJudge_fun(mathParser.Judge_funContext context)
271270
}
272271
return Operand.Create(r != 0);
273272
}
274-
private Operand Compare(Operand op1, Operand op2, string type)
273+
private static Operand Compare(Operand op1, Operand op2, string type)
275274
{
276275
double t1 = op1.NumberValue;
277276
double t2 = op2.NumberValue;
@@ -295,7 +294,7 @@ private Operand Compare(Operand op1, Operand op2, string type)
295294
return Operand.Create(r != 0);
296295
}
297296

298-
private int Compare(double t1, double t2)
297+
private static int Compare(double t1, double t2)
299298
{
300299
var b = Math.Round(t1 - t2, 12, MidpointRounding.AwayFromZero);
301300
if (b == 0) {
@@ -1131,7 +1130,7 @@ public virtual Operand VisitEVEN_fun(mathParser.EVEN_funContext context)
11311130
if (z % 2 == 0) {
11321131
return Operand.Create(z);
11331132
}
1134-
z = z + 1;
1133+
z++;
11351134
return Operand.Create(z);
11361135
}
11371136
public virtual Operand VisitODD_fun(mathParser.ODD_funContext context)
@@ -1147,7 +1146,7 @@ public virtual Operand VisitODD_fun(mathParser.ODD_funContext context)
11471146
if (z % 2 == 1) {
11481147
return Operand.Create(z);
11491148
}
1150-
z = z + 1;
1149+
z++;
11511150
return Operand.Create(z);
11521151
}
11531152
public virtual Operand VisitMROUND_fun(mathParser.MROUND_funContext context)
@@ -1283,11 +1282,11 @@ public virtual Operand VisitMULTINOMIAL_fun(mathParser.MULTINOMIAL_funContext co
12831282
int sum = 0;
12841283
int n = 1;
12851284
foreach (var a in list) {
1286-
n *= F_base_Factorial((int)a);
1285+
n *= MathVisitor.F_base_Factorial((int)a);
12871286
sum += (int)a;
12881287
}
12891288

1290-
var r = F_base_Factorial(sum) / n;
1289+
var r = MathVisitor.F_base_Factorial(sum) / n;
12911290
return Operand.Create(r);
12921291
}
12931292
public virtual Operand VisitPRODUCT_fun(mathParser.PRODUCT_funContext context)
@@ -1328,7 +1327,7 @@ public virtual Operand VisitSUMSQ_fun(mathParser.SUMSQ_funContext context)
13281327

13291328
return Operand.Create(d);
13301329
}
1331-
private int F_base_Factorial(int a)
1330+
private static int F_base_Factorial(int a)
13321331
{
13331332
if (a == 0) {
13341333
return 1;
@@ -1351,14 +1350,14 @@ public virtual Operand VisitASC_fun(mathParser.ASC_funContext context)
13511350
var firstValue = context.expr().Accept(this).ToText("Function ASC parameter is error!");
13521351
if (firstValue.IsError) { return firstValue; }
13531352

1354-
return Operand.Create(F_base_ToDBC(firstValue.TextValue));
1353+
return Operand.Create(MathVisitor.F_base_ToDBC(firstValue.TextValue));
13551354
}
13561355
public virtual Operand VisitJIS_fun(mathParser.JIS_funContext context)
13571356
{
13581357
var firstValue = context.expr().Accept(this).ToText("Function JIS parameter is error!");
13591358
if (firstValue.IsError) { return firstValue; }
13601359

1361-
return Operand.Create(F_base_ToSBC(firstValue.TextValue));
1360+
return Operand.Create(MathVisitor.F_base_ToSBC(firstValue.TextValue));
13621361
}
13631362
public virtual Operand VisitCHAR_fun(mathParser.CHAR_funContext context)
13641363
{
@@ -1572,7 +1571,7 @@ public virtual Operand VisitRMB_fun(mathParser.RMB_funContext context)
15721571
var firstValue = context.expr().Accept(this).ToNumber("Function RMB parameter is error!");
15731572
if (firstValue.IsError) { return firstValue; }
15741573

1575-
return Operand.Create(F_base_ToChineseRMB(firstValue.NumberValue));
1574+
return Operand.Create(MathVisitor.F_base_ToChineseRMB(firstValue.NumberValue));
15761575
}
15771576
public virtual Operand VisitSEARCH_fun(mathParser.SEARCH_funContext context)
15781577
{
@@ -1691,7 +1690,7 @@ public virtual Operand VisitVALUE_fun(mathParser.VALUE_funContext context)
16911690
return Operand.Error("Function VALUE parameter is error!");
16921691
}
16931692

1694-
private String F_base_ToSBC(String input)
1693+
private static String F_base_ToSBC(String input)
16951694
{
16961695
StringBuilder sb = new StringBuilder(input);
16971696
for (int i = 0; i < input.Length; i++) {
@@ -1704,7 +1703,7 @@ private String F_base_ToSBC(String input)
17041703
}
17051704
return sb.ToString();
17061705
}
1707-
private String F_base_ToDBC(String input)
1706+
private static String F_base_ToDBC(String input)
17081707
{
17091708
StringBuilder sb = new StringBuilder(input);
17101709
for (int i = 0; i < input.Length; i++) {
@@ -1718,7 +1717,7 @@ private String F_base_ToDBC(String input)
17181717
}
17191718
return sb.ToString();
17201719
}
1721-
private string F_base_ToChineseRMB(double x)
1720+
private static string F_base_ToChineseRMB(double x)
17221721
{
17231722
string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A", cultureInfo);
17241723
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}", RegexOptions.Compiled);
@@ -1930,7 +1929,7 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19301929
} else if (CharUtil.Equals(t, "YM")) {
19311930
#region ym
19321931
var mo = endMyDate.Month - startMyDate.Month;
1933-
if (endMyDate.Day < startMyDate.Day) mo = mo - 1;
1932+
if (endMyDate.Day < startMyDate.Day) mo--;
19341933
if (mo < 0) mo += 12;
19351934
return Operand.Create((mo));
19361935
#endregion
@@ -2291,17 +2290,17 @@ public virtual Operand VisitAVERAGEIF_fun(mathParser.AVERAGEIF_funContext contex
22912290
double sum;
22922291
int count;
22932292
if (args[1].Type == OperandType.NUMBER) {
2294-
count = F_base_countif(list, args[1].NumberValue);
2293+
count = MathVisitor.F_base_countif(list, args[1].NumberValue);
22952294
sum = count * args[1].NumberValue;
22962295
} else {
22972296
if (double.TryParse(args[1].TextValue.Trim(), NumberStyles.Any, cultureInfo, out double d)) {
2298-
count = F_base_countif(list, d);
2299-
sum = F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
2297+
count = MathVisitor.F_base_countif(list, d);
2298+
sum = MathVisitor.F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
23002299
} else {
23012300
var sunif = args[1].TextValue.Trim();
23022301
if (sumifRegex.IsMatch(sunif)) {
2303-
count = F_base_countif(list, sunif);
2304-
sum = F_base_sumif(list, sunif, sumdbs);
2302+
count = MathVisitor.F_base_countif(list, sunif);
2303+
sum = MathVisitor.F_base_sumif(list, sunif, sumdbs);
23052304
} else {
23062305
return Operand.Error("Function AVERAGE parameter 2 error!");
23072306
}
@@ -2368,14 +2367,14 @@ public virtual Operand VisitCOUNTIF_fun(mathParser.COUNTIF_funContext context)
23682367

23692368
int count;
23702369
if (args[1].Type == OperandType.NUMBER) {
2371-
count = F_base_countif(list, args[1].NumberValue);
2370+
count = MathVisitor.F_base_countif(list, args[1].NumberValue);
23722371
} else {
23732372
if (double.TryParse(args[1].TextValue.Trim(), NumberStyles.Any, cultureInfo, out double d)) {
2374-
count = F_base_countif(list, d);
2373+
count = MathVisitor.F_base_countif(list, d);
23752374
} else {
23762375
var sunif = args[1].TextValue.Trim();
23772376
if (sumifRegex.IsMatch(sunif)) {
2378-
count = F_base_countif(list, sunif);
2377+
count = MathVisitor.F_base_countif(list, sunif);
23792378
} else {
23802379
return Operand.Error("Function COUNTIF parameter 2 error!");
23812380
}
@@ -2417,14 +2416,14 @@ public virtual Operand VisitSUMIF_fun(mathParser.SUMIF_funContext context)
24172416

24182417
double sum;
24192418
if (args[1].Type == OperandType.NUMBER) {
2420-
sum = F_base_countif(list, args[1].NumberValue) * args[1].NumberValue;
2419+
sum = MathVisitor.F_base_countif(list, args[1].NumberValue) * args[1].NumberValue;
24212420
} else {
24222421
if (double.TryParse(args[1].TextValue.Trim(), NumberStyles.Any, cultureInfo, out _)) {
2423-
sum = F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
2422+
sum = MathVisitor.F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
24242423
} else {
24252424
var sunif = args[1].TextValue.Trim();
24262425
if (sumifRegex.IsMatch(sunif)) {
2427-
sum = F_base_sumif(list, sunif, sumdbs);
2426+
sum = MathVisitor.F_base_sumif(list, sunif, sumdbs);
24282427
} else {
24292428
return Operand.Error("Function SUMIF parameter 2 error!");
24302429
}
@@ -2861,7 +2860,7 @@ public virtual Operand VisitWEIBULL_fun(mathParser.WEIBULL_funContext context)
28612860
return Operand.Create(ExcelFunctions.WEIBULL(x, shape, scale, state));
28622861
}
28632862

2864-
private int F_base_countif(List<double> dbs, double d)
2863+
private static int F_base_countif(List<double> dbs, double d)
28652864
{
28662865
int count = 0;
28672866
d = Math.Round(d, 12, MidpointRounding.AwayFromZero);
@@ -2872,35 +2871,35 @@ private int F_base_countif(List<double> dbs, double d)
28722871
}
28732872
return count;
28742873
}
2875-
private int F_base_countif(List<double> dbs, string s)
2874+
private static int F_base_countif(List<double> dbs, string s)
28762875
{
28772876
var m = sumifRegex.Match(s);
28782877
var d = double.Parse(m.Groups[2].Value, NumberStyles.Any, cultureInfo);
28792878
int count = 0;
28802879

28812880
foreach (var item in dbs) {
2882-
if (F_base_compare(item, d, s)) {
2881+
if (MathVisitor.F_base_compare(item, d, s)) {
28832882
count++;
28842883
}
28852884
}
28862885
return count;
28872886
}
28882887

2889-
private double F_base_sumif(List<double> dbs, string s, List<double> sumdbs)
2888+
private static double F_base_sumif(List<double> dbs, string s, List<double> sumdbs)
28902889
{
28912890
var m = sumifRegex.Match(s);
28922891
var d = double.Parse(m.Groups[2].Value, NumberStyles.Any, cultureInfo);
28932892
//var ss = m.Groups[1].Value;
28942893
double sum = 0;
28952894

28962895
for (int i = 0; i < dbs.Count; i++) {
2897-
if (F_base_compare(dbs[i], d, s)) {
2896+
if (MathVisitor.F_base_compare(dbs[i], d, s)) {
28982897
sum += sumdbs[i];
28992898
}
29002899
}
29012900
return sum;
29022901
}
2903-
private bool F_base_compare(double a, double b, string ss)
2902+
private static bool F_base_compare(double a, double b, string ss)
29042903
{
29052904
if (CharUtil.Equals(ss, '<')) {
29062905
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) < 0;
@@ -3563,7 +3562,7 @@ public virtual Operand VisitVLOOKUP_fun(mathParser.VLOOKUP_funContext context)
35633562
if (secondValue.Type == OperandType.NUMBER) {
35643563
var o2 = o1.ToNumber(null);
35653564
if (o2.IsError == false) {
3566-
b = Compare(o2.NumberValue, secondValue.NumberValue);
3565+
b = MathVisitor.Compare(o2.NumberValue, secondValue.NumberValue);
35673566
}
35683567
} else {
35693568
var o2 = o1.ToText(null);
@@ -3593,7 +3592,7 @@ public virtual Operand VisitVLOOKUP_fun(mathParser.VLOOKUP_funContext context)
35933592
if (secondValue.Type == OperandType.NUMBER) {
35943593
var o2 = o1.ToNumber(null);
35953594
if (o2.IsError == false) {
3596-
b = Compare(o2.NumberValue, secondValue.NumberValue);
3595+
b = MathVisitor.Compare(o2.NumberValue, secondValue.NumberValue);
35973596
}
35983597
} else {
35993598
var o2 = o1.ToText(null);

csharp/ToolGood.Algorithm2/Operand.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public abstract class Operand : IDisposable
4444
/// 操作数类型
4545
/// </summary>
4646
public abstract OperandType Type { get; }
47+
48+
/// <summary>
49+
/// 值
50+
/// </summary>
51+
public virtual object Value => throw new NotImplementedException();
4752
/// <summary>
4853
/// 数字值
4954
/// </summary>
@@ -52,6 +57,11 @@ public abstract class Operand : IDisposable
5257
/// int值
5358
/// </summary>
5459
public virtual int IntValue => throw new NotImplementedException();
60+
/// <summary>
61+
/// long值
62+
/// </summary>
63+
public virtual long LongValue => throw new NotImplementedException();
64+
5565
/// <summary>
5666
/// 字符串值
5767
/// </summary>
@@ -492,19 +502,22 @@ public static implicit operator Operand(List<double> obj)
492502
}
493503
abstract class Operand<T> : Operand
494504
{
495-
public T Value { get; private set; }
505+
protected T _value { get; private set; }
506+
507+
public override object Value => _value;
496508
public Operand(T obj)
497509
{
498-
Value = obj;
510+
_value = obj;
499511
}
500512
}
501513

502514
sealed class OperandNumber : Operand<double>
503515
{
504516
public OperandNumber(double obj) : base(obj) { }
505517
public override OperandType Type => OperandType.NUMBER;
506-
public override int IntValue => (int)Value;
507-
public override double NumberValue => Value;
518+
public override int IntValue => (int)_value;
519+
public override double NumberValue => _value;
520+
public override long LongValue => (long)_value;
508521

509522
public override Operand ToNumber(string errorMessage = null) { return this; }
510523
public override Operand ToBoolean(string errorMessage = null) { return NumberValue != 0 ? True : False; }
@@ -524,7 +537,7 @@ sealed class OperandBoolean : Operand<bool>
524537
{
525538
public OperandBoolean(bool obj) : base(obj) { }
526539
public override OperandType Type => OperandType.BOOLEAN;
527-
public override bool BooleanValue => Value;
540+
public override bool BooleanValue => _value;
528541
public override Operand ToNumber(string errorMessage = null) { return BooleanValue ? One : Zero; }
529542
public override Operand ToBoolean(string errorMessage = null) { return this; }
530543
public override Operand ToText(string errorMessage = null) { return Create(BooleanValue ? "TRUE" : "FALSE"); }
@@ -546,7 +559,7 @@ sealed class OperandString : Operand<string>
546559
{
547560
public OperandString(string obj) : base(obj) { }
548561
public override OperandType Type => OperandType.TEXT;
549-
public override string TextValue => Value;
562+
public override string TextValue => _value;
550563

551564
public override Operand ToNumber(string errorMessage = null)
552565
{
@@ -608,7 +621,7 @@ sealed class OperandMyDate : Operand<MyDate>
608621
{
609622
public OperandMyDate(MyDate obj) : base(obj) { }
610623
public override OperandType Type => OperandType.DATE;
611-
public override MyDate DateValue => Value;
624+
public override MyDate DateValue => _value;
612625
public override Operand ToNumber(string errorMessage = null)
613626
{
614627
return Create((double)DateValue);
@@ -636,7 +649,7 @@ sealed class OperandJson : Operand<JsonData>
636649
{
637650
public OperandJson(JsonData obj) : base(obj) { }
638651
public override OperandType Type => OperandType.JSON;
639-
internal override JsonData JsonValue => Value;
652+
internal override JsonData JsonValue => _value;
640653

641654
public override Operand ToJson(string errorMessage = null) { return this; }
642655
public override Operand ToArray(string errorMessage = null)
@@ -680,7 +693,7 @@ sealed class OperandArray : Operand<List<Operand>>
680693
{
681694
public OperandArray(List<Operand> obj) : base(obj) { }
682695
public override OperandType Type => OperandType.ARRARY;
683-
public override List<Operand> ArrayValue => Value;
696+
public override List<Operand> ArrayValue => _value;
684697
public override Operand ToArray(string errorMessage = null) { return this; }
685698

686699
}

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 2 additions & 2 deletions
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.2.0.2</Version>
24+
<Version>3.2.0.3</Version>
2525
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2626
<DelaySign>false</DelaySign>
2727
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>
@@ -30,7 +30,7 @@
3030

3131

3232
<ItemGroup>
33-
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.11.1" />
33+
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.12.0" />
3434
</ItemGroup>
3535

3636

0 commit comments

Comments
 (0)