Skip to content

Commit fe62005

Browse files
committed
1.1.10版本,所有支持的函数都有单元测试、修正了部分统计函数
1 parent 5e79162 commit fe62005

File tree

5 files changed

+151
-95
lines changed

5 files changed

+151
-95
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,20 +507,20 @@ ToolGood.Algorithm
507507
<td>AVERAGE(1,2,3,4,2,2,1,4) >>2.375</td>
508508
</tr>
509509
<tr>
510-
<td>AVERAGEIF</td><td></td>
511-
<td></td>
510+
<td>AVERAGEIF</td><td>返回参数的平均值</td>
511+
<td>AVERAGEIF({1,2,3,4,2,2,1,4},'>1') >>2.833333333</td>
512512
</tr>
513513
<tr>
514514
<td>COUNT</td><td>计算参数列表中数字的个数</td>
515515
<td>COUNT(1,2,3,4,2,2,1,4) >>8</td>
516516
</tr>
517517
<tr>
518518
<td>COUNTIF</td><td>计算参数列表中数字的个数</td>
519-
<td></td>
519+
<td>COUNTIF({1,2,3,4,2,2,1,4},'>1') >>6</td>
520520
</tr>
521521
<tr>
522522
<td>SUMIF</td><td></td>
523-
<td></td>
523+
<td>SUMIF({1,2,3,4,2,2,1,4},'>1') >>17</td>
524524
</tr>
525525
<tr>
526526
<td>AVEDEV</td><td>返回数据点与其平均值的绝对偏差的平均值</td>

ToolGood.Algorithm.Test/AlgorithmEngine/AlgorithmEngineTest_sum.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,36 @@ public void SMALL_test()
317317

318318
#endregion
319319

320+
#region if
321+
[Test]
322+
public void COUNTIF_test()
323+
{
324+
AlgorithmEngine engine = new AlgorithmEngine();
325+
var t = engine.TryEvaluate("COUNTIF({1,2,3,4,2,2,1,4},'>1')", 0.0);
326+
Assert.AreEqual(t, 6.0);
327+
}
328+
329+
[Test]
330+
public void SUMIF_test()
331+
{
332+
AlgorithmEngine engine = new AlgorithmEngine();
333+
var t = engine.TryEvaluate("SUMIF({1,2,3,4,2,2,1,4},'>1')", 0.0);
334+
Assert.AreEqual(t, 17);
335+
t = engine.TryEvaluate("SUMIF({1,2,3,4,2,2,1,4},'>1',{1,1,1,1,1,1,1,1})", 0.0);
336+
Assert.AreEqual(t, 6);
337+
}
338+
[Test]
339+
public void AVERAGEIF_test()
340+
{
341+
AlgorithmEngine engine = new AlgorithmEngine();
342+
var t = engine.TryEvaluate("AVERAGEIF({1,2,3,4,2,2,1,4},'>1')", 0.0);
343+
Assert.AreEqual(Math.Round(t, 6), Math.Round(2.833333333, 6));
344+
t = engine.TryEvaluate("AVERAGEIF({1,2,3,4,2,2,1,4},'>1',{1,1,1,1,1,1,1,1})", 0.0);
345+
Assert.AreEqual(t, 1);
346+
}
320347

348+
349+
350+
#endregion
321351
}
322352
}

ToolGood.Algorithm/AlgorithmEngine.sum.cs

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,21 @@ private Operand SMALL(List<Operand> arg)
9494
{
9595
if (arg.Count < 2) return throwError("SMALL中参数不足", new List<Operand>());
9696
var list = arg[0].GetNumberList().OrderBy(q => q).ToList();
97-
return new Operand(OperandType.NUMBER, list[arg[1].IntValue-1]);
97+
return new Operand(OperandType.NUMBER, list[arg[1].IntValue - 1]);
9898
}
9999

100100
private Operand LARGE(List<Operand> arg)
101101
{
102102
if (arg.Count < 2) return throwError("LARGE中参数不足", new List<Operand>());
103-
var list = arg[0].GetNumberList().OrderByDescending(q=>q).ToList();
104-
return new Operand(OperandType.NUMBER, list[arg[1].IntValue-1]);
103+
var list = arg[0].GetNumberList().OrderByDescending(q => q).ToList();
104+
return new Operand(OperandType.NUMBER, list[arg[1].IntValue - 1]);
105105
}
106106

107107
private Operand FISHERINV(List<Operand> arg)
108108
{
109109
if (arg.Count < 1) return throwError("FISHER中参数不足", new List<Operand>());
110110
var x = arg[0].NumberValue;
111-
var n = (Math.Exp(2*x) - 1) / (Math.Exp(2*x) + 1);
111+
var n = (Math.Exp(2 * x) - 1) / (Math.Exp(2 * x) + 1);
112112
return new Operand(OperandType.NUMBER, n);
113113
}
114114

@@ -183,54 +183,55 @@ private Operand EXPONDIST(List<Operand> arg)
183183
#region SUMIF COUNTIF
184184
private Operand AVERAGEIF(List<Operand> arg)
185185
{
186-
throw new NotImplementedException();
186+
if (arg.Count < 2) return throwError("SUMIF中参数不足", new List<Operand>());
187+
var dbs = arg[0].GetNumberList();
188+
List<double> sumdbs = dbs;
189+
if (arg.Count == 3) sumdbs = arg[2].GetNumberList();
190+
191+
double sum = 0;
192+
int count = 0;
193+
if (arg[1].Type == OperandType.NUMBER) {
194+
count = countif(dbs, arg[1].NumberValue);
195+
sum = count * arg[1].NumberValue;
196+
} else {
197+
double d;
198+
if (double.TryParse(arg[1].StringValue.Trim(), out d)) {
199+
count = countif(dbs, arg[1].NumberValue);
200+
sum = sumif(dbs, "=" + arg[1].StringValue.Trim(), sumdbs);
201+
} else {
202+
count = countif(dbs, arg[1].StringValue.Trim());
203+
sum = sumif(dbs, arg[1].StringValue.Trim(), sumdbs);
204+
}
205+
}
206+
return new Operand(OperandType.NUMBER, sum / count);
207+
187208
}
188209

189210

190211
private Operand SUMIF(List<Operand> arg)
191212
{
192-
throw new NotImplementedException();
193-
194213
if (arg.Count < 2) return throwError("SUMIF中参数不足", new List<Operand>());
195214
var dbs = arg[0].GetNumberList();
215+
var sumdbs = dbs;
216+
if (arg.Count == 3) sumdbs = arg[2].GetNumberList();
196217
double sum = 0;
197218
if (arg[1].Type == OperandType.NUMBER) {
198219
sum = countif(dbs, arg[1].NumberValue) * arg[1].NumberValue;
199220
} else {
200221
double d;
201222
if (double.TryParse(arg[1].StringValue.Trim(), out d)) {
202-
sum = countif(dbs, arg[1].NumberValue) * arg[1].NumberValue;
223+
sum = sumif(dbs, "=" + arg[1].StringValue.Trim(), sumdbs);
203224
} else {
204-
//sum = countif(dbs, arg[1].StringValue.Trim());
225+
sum = sumif(dbs, arg[1].StringValue.Trim(), sumdbs);
205226
}
206227
}
207228
return new Operand(OperandType.NUMBER, sum);
208229
}
209-
private double sumif(List<double> dbs, string s)
210-
{
211-
Regex re = new Regex(@"(<|<=|>|>=|=|==|!=|<>) *([-+]?\d+(\.(\d+)?)?)");
212-
if (re.IsMatch(s) == false) {
213-
return 0;
214-
}
215-
var m = re.Match(s);
216-
var d = double.Parse(m.Groups[2].Value);
217-
var ss = m.Groups[1].Value;
218-
double sum = 0;
219230

220-
foreach (var item in dbs) {
221-
if (compare(item, d, s)) {
222-
sum += item;
223-
}
224-
}
225-
return sum;
226-
}
227231

228232

229233
private Operand COUNTIF(List<Operand> arg)
230234
{
231-
//throw new NotImplementedException();
232-
233-
234235
if (arg.Count < 2) return throwError("COUNTIF中参数不足", new List<Operand>());
235236
var dbs = arg[0].GetNumberList();
236237
int count = 0;
@@ -247,53 +248,10 @@ private Operand COUNTIF(List<Operand> arg)
247248
return new Operand(OperandType.NUMBER, count);
248249
}
249250

250-
private bool compare(double a, double b, string ss)
251-
{
252-
if (ss == "<") {
253-
return a < b;
254-
} else if (ss == "<=") {
255-
return a <= b;
256-
} else if (ss == ">") {
257-
return a > b;
258-
} else if (ss == ">=") {
259-
return a >= b;
260-
} else if (ss == "=" || ss == "==") {
261-
return a == b;
262-
}
263-
return a != b;
264-
}
265251

266-
private int countif(List<double> dbs, string s)
267-
{
268-
Regex re = new Regex(@"(<|<=|>|>=|=|==|!=|<>) *([-+]?\d+(\.(\d+)?)?)");
269-
if (re.IsMatch(s) == false) {
270-
return 0;
271-
}
272-
var m = re.Match(s);
273-
var d = double.Parse(m.Groups[2].Value);
274-
var ss = m.Groups[1].Value;
275-
int count = 0;
276252

277-
foreach (var item in dbs) {
278-
if (compare(item, d, s)) {
279-
count++;
280-
}
281-
}
282-
return count;
283-
}
284253

285254

286-
private int countif(List<double> dbs, double d)
287-
{
288-
int count = 0;
289-
d = Math.Round(d, 12);
290-
foreach (var item in dbs) {
291-
if (Math.Round(item, 12) == d) {
292-
count++;
293-
}
294-
}
295-
return count;
296-
}
297255

298256
#endregion
299257

@@ -616,26 +574,11 @@ private Operand COUNT(List<Operand> arg)
616574

617575
private Operand AVERAGE(List<Operand> arg)
618576
{
619-
//if (arg.Count < 1) return throwError("AVERAGE中参数不足", new List<Operand>());
577+
if (arg.Count < 1) return throwError("AVERAGE中参数不足", new List<Operand>());
620578
List<double> list = GetList(arg);
621579
return new Operand(OperandType.NUMBER, list.Average());
622580
}
623581

624-
private List<double> GetList(List<Operand> arg)
625-
{
626-
List<double> list = new List<double>();
627-
foreach (var item in arg) {
628-
if (item.Type == OperandType.NUMBER) {
629-
list.Add(item.NumberValue);
630-
} else if (item.Type == OperandType.ARRARY) {
631-
var ls = item.GetNumberList();
632-
if (ls == null) continue;
633-
foreach (var d in ls) {
634-
list.Add(d);
635-
}
636-
}
637-
}
638-
return list;
639-
}
582+
640583
}
641584
}

ToolGood.Algorithm/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
3333
// 方法是按如下所示使用“*”: :
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.1.9.0")]
36-
[assembly: AssemblyFileVersion("1.1.9.0")]
35+
[assembly: AssemblyVersion("1.1.10.0")]
36+
[assembly: AssemblyFileVersion("1.1.10.0")]

ToolGood.Algorithm/_base/AlgorithmEngine.base.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,88 @@ private int lgm(List<int> list)
8686
}
8787

8888

89+
90+
private double sumif(List<double> dbs, string s, List<double> sumdbs)
91+
{
92+
Regex re = new Regex(@"(<|<=|>|>=|=|==|!=|<>) *([-+]?\d+(\.(\d+)?)?)");
93+
if (re.IsMatch(s) == false) {
94+
return 0;
95+
}
96+
var m = re.Match(s);
97+
var d = double.Parse(m.Groups[2].Value);
98+
var ss = m.Groups[1].Value;
99+
double sum = 0;
100+
101+
for (int i = 0; i < dbs.Count; i++) {
102+
if (compare(dbs[i], d, s)) {
103+
sum += sumdbs[i];
104+
}
105+
}
106+
return sum;
107+
}
108+
109+
private int countif(List<double> dbs, double d)
110+
{
111+
int count = 0;
112+
d = Math.Round(d, 12);
113+
foreach (var item in dbs) {
114+
if (Math.Round(item, 12) == d) {
115+
count++;
116+
}
117+
}
118+
return count;
119+
}
120+
121+
private bool compare(double a, double b, string ss)
122+
{
123+
if (ss == "<") {
124+
return a < b;
125+
} else if (ss == "<=") {
126+
return a <= b;
127+
} else if (ss == ">") {
128+
return a > b;
129+
} else if (ss == ">=") {
130+
return a >= b;
131+
} else if (ss == "=" || ss == "==") {
132+
return a == b;
133+
}
134+
return a != b;
135+
}
136+
137+
private int countif(List<double> dbs, string s)
138+
{
139+
Regex re = new Regex(@"(<|<=|>|>=|=|==|!=|<>) *([-+]?\d+(\.(\d+)?)?)");
140+
if (re.IsMatch(s) == false) {
141+
return 0;
142+
}
143+
var m = re.Match(s);
144+
var d = double.Parse(m.Groups[2].Value);
145+
var ss = m.Groups[1].Value;
146+
int count = 0;
147+
148+
foreach (var item in dbs) {
149+
if (compare(item, d, s)) {
150+
count++;
151+
}
152+
}
153+
return count;
154+
}
155+
private List<double> GetList(List<Operand> arg)
156+
{
157+
List<double> list = new List<double>();
158+
foreach (var item in arg) {
159+
if (item.Type == OperandType.NUMBER) {
160+
list.Add(item.NumberValue);
161+
} else if (item.Type == OperandType.ARRARY) {
162+
var ls = item.GetNumberList();
163+
if (ls == null) continue;
164+
foreach (var d in ls) {
165+
list.Add(d);
166+
}
167+
}
168+
}
169+
return list;
170+
}
171+
89172
}
90173
}

0 commit comments

Comments
 (0)