帮助类之自动解析表达式判定结果方法类

 最近一直在做一个项目,到现在为止已经算是快结束了,其中碰见一个问题:怎样判定工厂中检测数据的合格,其中已提供了检测标准,但是这个标准是文本。问题来了,怎样解析给定的标准,然后判定检测的结果值是否合格呢?当时一直困扰我的,想了好久觉得没有更好的办法,因为我们一般只能针对表达式的标准进行判定。比如:标准:1<x<2 ,检测的结果假如是1.2 那么就需要将  1.2 分别与 1 (下限)和2(上限)进行比较;还有其他比如文本型的:标准:"通过" 检测结果值是:‘不通过’或‘通过’等等,还有其他。

       好了,说到这里,我就开始对这类标准解析的过程,其中我共改过3个版本,现将最新的一份粘贴上来。附件连接一份

先截图一张测试的截图代码:(如下图)

 

 当选中一种格式的表达式格式,然后解析成想要的结果进行判定,下面是全部代码:

 1.静态字符串,用于匹配特殊表达式类型

 1 private static string[] ExpressionChar = {">","<","","","="};
 2         
 3         /// <summary>
 4         /// 判定表达式类型
 5         /// </summary>
 6         /// <param name="exp">标准</param>
 7         /// <returns>返回标准类型 0=文本,1=单向,2=双向</returns>
 8         private static int ExpressionCategory(string exp)
 9         {
10             int count = 0;
11             for (int i = 0; i < exp.Length; i++)
12             {
13                 if (ExpressionChar.Contains(exp.Substring(i,1)))
14                 {
15                     count++;
16                 }
17             }
18             return count;
19         }
View Code

 2.解析表达式,并获取标准的表达式字符串数组

 1 /// <summary>
 2         /// 解析表达式
 3         /// </summary>
 4         /// <param name="exp">标准</param>
 5         /// <returns>返回标准与值分类的字符串数组,键=标准,值=标准值</returns>
 6         private static string[] EnExpression(string exp)
 7         {
 8             int category = ExpressionCategory(exp);
 9             string[] tt=null;
10             //文本
11             if ( category== 0)
12             {
13                 tt= new string[]{exp};
14             }
15             //单向
16             else if(category==1)
17             {
18                 string opr = exp.Substring(1, 1);
19                 string value = exp.Substring(2);
20                 tt= new string[] { opr, value };
21             }
22             //双向
23             else if (category == 2)
24             {
25                 int x = exp.IndexOf("X");
26                 string down =exp.Substring(0, x - 1);
27                 string up = exp.Substring(x + 2);
28                 string downOpr = exp.Substring(x - 1, 1);
29                 string upOpr = exp.Substring(x + 1, 1);
30                 if (downOpr == "<") downOpr = ">";
31                 else if (downOpr == "") downOpr = "";
32                 tt= new string[]{downOpr,down,upOpr,up};
33             }
34             return tt;
35         }
View Code

3.判定表达式结果,比如标准是<1,需要判定的结果是0.93,结论肯定是合格的。

 1 /// <summary>
 2         /// 判定表达式结果
 3         /// </summary>
 4         /// <param name="oprChar">表达式符号</param>
 5         /// <param name="oprValue">表达式标准值</param>
 6         /// <param name="value">待判定的值</param>
 7         /// <returns>0=正确,1=错误</returns>
 8         private static int CheckNum(string oprChar, double oprValue, double value)
 9         {
10             int result = 0;
11             switch (oprChar)
12             {
13                 case "=":
14                     if (oprValue.Equals(value)) result=0;
15                     else result = 1;
16                     break;
17                 case ">":
18                     if (value > oprValue) result = 0;
19                     else result = 1;
20                     break;
21                 case "<":
22                     if (value < oprValue) result = 0;
23                     else result = 1;
24                     break;
25                 case "":
26                     if (value >= oprValue) result = 0;
27                     else result = 1;
28                     break;
29                 case "":
30                     if (value <= oprValue) return 0;
31                     else return 1;
32                     break;
33             }
34             return result;
35         }
View Code

4.因为有两种表达式:文本标准、表达式标准

  现在先说文本标准,也就是说,我采用的是直接判定文本标准是否和给定值是否相等

 1 /// <summary>
 2         /// 判定文本结果
 3         /// </summary>
 4         /// <param name="exp">文本标准值</param>
 5         /// <param name="value">待判定的值</param>
 6         /// <returns>0=正确,1=错误</returns>
 7         private static int CheckTxt(string exp, string value)
 8         {
 9             int result = 0;
10             if (exp.Equals(value))
11             {
12                 result = 0;
13             }
14             else
15             {
16                 result = 1;
17             }
18             return result;
19         }
View Code

5.表达式标准判定

 1         /// <summary>
 2         /// 解析表达式
 3         /// </summary>
 4         /// <param name="exp">标准</param>
 5         /// <returns>返回标准与值分类的字符串数组,键=标准,值=标准值</returns>
 6         private static string[] EnExpression(string exp)
 7         {
 8             int category = ExpressionCategory(exp);
 9             string[] tt=null;
10             //文本
11             if ( category== 0)
12             {
13                 tt= new string[]{exp};
14             }
15             //单向
16             else if(category==1)
17             {
18                 string opr = exp.Substring(1, 1);
19                 string value = exp.Substring(2);
20                 tt= new string[] { opr, value };
21             }
22             //双向
23             else if (category == 2)
24             {
25                 int x = exp.IndexOf("X");
26                 string down =exp.Substring(0, x - 1);
27                 string up = exp.Substring(x + 2);
28                 string downOpr = exp.Substring(x - 1, 1);
29                 string upOpr = exp.Substring(x + 1, 1);
30                 if (downOpr == "<") downOpr = ">";
31                 else if (downOpr == "") downOpr = "";
32                 tt= new string[]{downOpr,down,upOpr,up};
33             }
34             return tt;
35         }
View Code

上面的这种接卸标准的方式,虽然说在默写方面不是很完美,但是在比较常规的情况下目前都是能应付的。如果今后还有其他的判定标准,可以在这个基础上进行改进。

以上完全是个人在工作上做的一点小结,可能还有很多不足之处,希望读者可以借鉴给出建议。

原文地址:https://www.cnblogs.com/zhaosw/p/5321336.html