Skip to content

Commit 38af0c6

Browse files
committed
修改 a?b:c 排序
1 parent 97473e5 commit 38af0c6

File tree

9 files changed

+708
-414
lines changed

9 files changed

+708
-414
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public void Test()
122122

123123
t1 = engine.TryEvaluate("(!(-7 < -2))?1:2", 0);
124124
Assert.AreEqual(t1, 2);
125+
t1 = engine.TryEvaluate("1>2?1:2", 0);
126+
Assert.AreEqual(t1, 2);
125127
}
126128

127129
[Test]

csharp/ToolGood.Algorithm2.Test/AlgorithmEngineEx/AlgorithmEngineExTest.cs

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,64 @@ public void Test()
6666
var p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
6767

6868
}
69-
7069
[Test]
7170
public void Test2()
71+
{
72+
ConditionCache multiConditionCache = new ConditionCache();
73+
multiConditionCache.LazyLoad = true;
74+
multiConditionCache.AddFormula("桌面积", "[圆桌]", "[半径]*[半径]*pi()");
75+
multiConditionCache.AddFormula("桌面积", "[方桌]", "[长]*[宽]");
76+
77+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<2.5", "[桌面积]*1.3");
78+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<5", "[桌面积]*1.5");
79+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<7", "[桌面积]*2");
80+
multiConditionCache.AddFormula("价格", "[圆桌]", "[桌面积]*2.5");
81+
82+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<1.3", "[桌面积]*1.3+[高]*1.1");
83+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<2", "[桌面积]*1.5+[高]*1.1");
84+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<5", "[桌面积]*2+[高]*1.1");
85+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<7", "[桌面积]*2.5");
86+
87+
88+
multiConditionCache.AddFormula("出错了", "[方桌]&& [长]<11", "[出错]*2.5");
89+
90+
91+
Desk desk = new Desk() {
92+
IsRoundTable = true,
93+
Radius = 3
94+
};
95+
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
96+
var p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
97+
Assert.AreEqual(3 * 3 * Math.PI * 1.5, p1, 0.0001);
98+
99+
Desk desk2 = new Desk() {
100+
IsRoundTable = false,
101+
Length = 3,
102+
Width = 1.3,
103+
Heigth = 1
104+
};
105+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
106+
var p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
107+
Assert.AreEqual(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
108+
109+
110+
Desk desk3 = new Desk() {
111+
IsRoundTable = false,
112+
Length = 9,
113+
Width = 1.3,
114+
Heigth = 1
115+
};
116+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
117+
var p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
118+
Assert.AreEqual(0, p3, 0.001);
119+
Assert.AreEqual("CategoryName [价格] is missing.", priceAlgorithm.LastError);
120+
121+
122+
var p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
123+
124+
}
125+
[Test]
126+
public void Test3()
72127
{
73128
ConditionCache multiConditionCache = new ConditionCache();
74129
multiConditionCache.AddCondition("类型", "[方桌]&& [长]<1.3", "1");
@@ -91,46 +146,4 @@ public void Test2()
91146

92147
}
93148

94-
public class Desk
95-
{
96-
public bool IsRoundTable { get; set; }
97-
public double Heigth { get; set; }
98-
public double Length { get; set; }
99-
public double Width { get; set; }
100-
public double Radius { get; set; }
101-
}
102-
103-
public class PriceAlgorithm : ToolGood.Algorithm.AlgorithmEngineEx
104-
{
105-
private Desk _disk;
106-
public PriceAlgorithm(ConditionCache multiConditionCache, Desk desk) : base(multiConditionCache)
107-
{
108-
_disk = desk;
109-
}
110-
111-
protected override Operand GetParameter(string parameter)
112-
{
113-
if (parameter == "长") {
114-
return Operand.Create(_disk.Length);
115-
}
116-
if (parameter == "宽") {
117-
return Operand.Create(_disk.Width);
118-
}
119-
if (parameter == "高") {
120-
return Operand.Create(_disk.Heigth);
121-
}
122-
if (parameter == "半径") {
123-
return Operand.Create(_disk.Radius);
124-
}
125-
if (parameter == "方桌") {
126-
return Operand.Create(_disk.IsRoundTable == false);
127-
}
128-
if (parameter == "圆桌") {
129-
return Operand.Create(_disk.IsRoundTable);
130-
}
131-
return base.GetParameter(parameter);
132-
}
133-
134-
}
135-
136149
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ToolGood.Algorithm2.Test.AlgorithmEngineEx
2+
{
3+
public class Desk
4+
{
5+
public bool IsRoundTable { get; set; }
6+
public double Heigth { get; set; }
7+
public double Length { get; set; }
8+
public double Width { get; set; }
9+
public double Radius { get; set; }
10+
}
11+
12+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using ToolGood.Algorithm;
2+
3+
namespace ToolGood.Algorithm2.Test.AlgorithmEngineEx
4+
{
5+
public class PriceAlgorithm : ToolGood.Algorithm.AlgorithmEngineEx
6+
{
7+
private Desk _disk;
8+
public PriceAlgorithm(ConditionCache multiConditionCache, Desk desk) : base(multiConditionCache)
9+
{
10+
_disk = desk;
11+
}
12+
13+
protected override Operand GetParameter(string parameter)
14+
{
15+
if (parameter == "长") {
16+
return Operand.Create(_disk.Length);
17+
}
18+
if (parameter == "宽") {
19+
return Operand.Create(_disk.Width);
20+
}
21+
if (parameter == "高") {
22+
return Operand.Create(_disk.Heigth);
23+
}
24+
if (parameter == "半径") {
25+
return Operand.Create(_disk.Radius);
26+
}
27+
if (parameter == "方桌") {
28+
return Operand.Create(_disk.IsRoundTable == false);
29+
}
30+
if (parameter == "圆桌") {
31+
return Operand.Create(_disk.IsRoundTable);
32+
}
33+
return base.GetParameter(parameter);
34+
}
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)