Skip to content

Commit b75d3c5

Browse files
committed
简化公式
1 parent 58ecdd8 commit b75d3c5

File tree

11 files changed

+4221
-233
lines changed

11 files changed

+4221
-233
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,23 @@ public void Cylinder_Test()
260260

261261
String tt2 = c.EvaluateFormula("'圆'-[半径]-高", '-');
262262
Assert.AreEqual("圆-3-10", tt2);
263+
}
263264

265+
[Test]
266+
public void Test5555()
267+
{
268+
Cylinder c = new Cylinder(3, 10);
269+
String t = c.GetSimplifiedFormula("[半径]*[半径]*pi()"); // 圆底面积
270+
Assert.AreEqual("3 * 3 * 3.141592653589793", t);
271+
272+
String t2 = c.GetSimplifiedFormula("半径*if(半径>2,1,3)");
273+
Assert.AreEqual("3 * 1", t2);
274+
275+
String t24 = c.GetSimplifiedFormula("半径*if(半径>2,1+4,3)");
276+
Assert.AreEqual("3 * 5", t24);
264277

265278
}
266279

280+
267281
}
268282
}

csharp/ToolGood.Algorithm2/AlgorithmEngine.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public AlgorithmEngine(bool ignoreCase)
5555
} else {
5656
_tempdict = new Dictionary<string, Operand>();
5757
}
58-
}
58+
}
5959
#endregion
6060

6161

@@ -353,7 +353,7 @@ public bool Parse(string exp)
353353
_context = context;
354354
return true;
355355
}
356-
356+
357357
/// <summary>
358358
/// 执行函数
359359
/// </summary>
@@ -710,6 +710,37 @@ public MyDate TryEvaluate_MyDate(string exp, MyDate def)
710710
}
711711
#endregion
712712

713+
#region GetSimplifiedFormula
714+
/// <summary>
715+
/// 获取简化公式
716+
/// </summary>
717+
/// <param name="String"></param>
718+
/// <param name=""></param>
719+
/// <returns></returns>
720+
public String GetSimplifiedFormula(String formula)
721+
{
722+
try {
723+
if (Parse(formula)) {
724+
MathSimplifiedFormulaVisitor visitor = new MathSimplifiedFormulaVisitor();
725+
visitor.GetParameter += GetDiyParameterInside;
726+
visitor.excelIndex = UseExcelIndex ? 1 : 0;
727+
visitor.DiyFunction += ExecuteDiyFunction;
728+
729+
Operand obj = visitor.Visit(_context);
730+
obj = obj.ToText("It can't be converted to String!");
731+
if (obj.IsError) {
732+
LastError = obj.ErrorMsg;
733+
return null;
734+
}
735+
return obj.TextValue;
736+
}
737+
} catch (Exception ex) {
738+
LastError = ex.Message;
739+
}
740+
return null;
741+
}
742+
#endregion
743+
713744
#region EvaluateFormula
714745
/// <summary>
715746
/// 计算公式

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,36 @@ public MyDate TryEvaluate_MyDate(string exp, MyDate def)
10661066

10671067

10681068

1069+
#endregion
1070+
1071+
#region GetSimplifiedFormula
1072+
/// <summary>
1073+
/// 获取简化公式
1074+
/// </summary>
1075+
/// <param name="formula">公式</param>
1076+
/// <returns></returns>
1077+
public String GetSimplifiedFormula(String formula)
1078+
{
1079+
try {
1080+
ProgContext _context = Parse(formula);
1081+
MathSimplifiedFormulaVisitor visitor = new MathSimplifiedFormulaVisitor();
1082+
visitor.GetParameter += GetDiyParameterInside;
1083+
visitor.excelIndex = UseExcelIndex ? 1 : 0;
1084+
visitor.DiyFunction += ExecuteDiyFunction;
1085+
1086+
Operand obj = visitor.Visit(_context);
1087+
obj = obj.ToText("It can't be converted to String!");
1088+
if (obj.IsError) {
1089+
LastError = obj.ErrorMsg;
1090+
return null;
1091+
}
1092+
return obj.TextValue;
1093+
1094+
} catch (Exception ex) {
1095+
LastError = ex.Message;
1096+
}
1097+
return null;
1098+
}
10691099
#endregion
10701100

10711101
#region EvaluateFormula

0 commit comments

Comments
 (0)