『算法』.Net 字符串计算算法
转载请标明:舒小龙 http://www.cnblogs.com/shuxiaolong/articles/2901784.html
什么都不说,直接贴测试类代码:
1 [Test]
2 public static void TestCompute()
3 {
4 const bool result = 12 + 13 == 14 + 11;
5 Console.WriteLine(result);
6
7 //这个作为 预运算,作为加载 类,函数 的.Net 底层开销——不计算在执行时间内
8 string none = ComputeHelper.Compute("1+(1+1)").Value + "\r\n";
9 Console.WriteLine("预运算: " + none);
10
11
12 DateTime dt1 = DateTime.Now;
13 string sTemp = string.Empty;
14
15 sTemp += ComputeHelper.Compute("(2+8)*(1+2+77+(12+8))").Value + "\r\n";
16 sTemp += ComputeHelper.Compute("345*657").Value + "\r\n";
17 sTemp += ComputeHelper.Compute("2+(235*(2+3))").Value + "\r\n";
18 sTemp += ComputeHelper.Compute("123+65+234+132+432").Value + "\r\n";
19 sTemp += ComputeHelper.Compute("1+1+1+1+1").Value + "\r\n";
20 sTemp += ComputeHelper.Compute("123*654*907").Value + "\r\n";
21 sTemp += ComputeHelper.Compute("(1234+987)*765").Value + "\r\n";
22 sTemp += ComputeHelper.Compute("7868*989+5678").Value + "\r\n";
23 sTemp += ComputeHelper.Compute("1234*546").Value + "\r\n";
24 sTemp += ComputeHelper.Compute("(122+5654)*(2+976)").Value + "\r\n";
25 sTemp += ComputeHelper.Compute("100-50+50-50").Value + "\r\n";
26 sTemp += ComputeHelper.Compute("False?111:222").Value + "\r\n";
27 sTemp += ComputeHelper.Compute("11111===11111").Value + "\r\n";
28 sTemp += ComputeHelper.Compute("(11111===2222)?111:222").Value + "\r\n";
29 sTemp += ComputeHelper.Compute("10-1+2-3+4-5+6").Value + "\r\n";
30 sTemp += ComputeHelper.Compute("10-1*2-3*4-5/6").Value + "\r\n";
31 sTemp += ComputeHelper.Compute("10-1*2-3*4-5/6").Value + "\r\n";
32 sTemp += ComputeHelper.Compute("\"QWERTYUIOP{}:\" LIKE \"%ERT%U%\"").Value + "\r\n";
33 sTemp += ComputeHelper.Compute("(\"QWERTYUIOP{}:\" LIKE \"%ERT%U\")?1111+1111:2222+2222").Value + "\r\n";
34 sTemp += ComputeHelper.Compute("-2+5*(-2+7)").Value + "\r\n";
35 sTemp += ComputeHelper.Compute("(REPLACE(REPLACE(\"AAAAAAAAKKK\",\"K\",\"M\"),\"A\",\"B\") == \"BBBBBBBBMMM\")?\"HHHHHHHHH\":\"IIIIIIIIII\"").Value + "\r\n";
36 sTemp += ComputeHelper.Compute("3^4").Value + "\r\n";
37 sTemp += ComputeHelper.Compute("81^(1/4)").Value + "\r\n";
38
39 DateTime dt2 = DateTime.Now;
40
41 Console.WriteLine(sTemp + "计算时间:" + (dt2 - dt1).TotalMilliseconds);
42 }
运行结果:
True
预运算: 3
1000
226665
1177
986
5
72960894
1699065
7787130
673764
5648928
50
222
True
222
13
-4.83333333333333
-4.83333333333333
True
4444
23
HHHHHHHHH
81
3
计算时间:9.0006 (毫秒)
------------------------------------------------------------------------------------
1 [Test]
2 public void TestComputeBooleanExpres()
3 {
4 string none = ComputeHelper.ComputeBoolean("True AND False OR True").ToString();
5 string none2 = ComputeHelper.ComputeBoolean("((REPLACE(\"CCC\",\"A\",\"B\")==\"BBB\"?200:100)>150)").ToString();
6 Console.WriteLine("预运算: " + none + " " + none2);
7 DateTime dt1 = DateTime.Now;
8 string sTemp = string.Empty;
9
10 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\"") + "\r\n";
11 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" AND 300>100") + "\r\n";
12 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" AND 50>100") + "\r\n";
13 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" AND 100>=100") + "\r\n";
14 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" AND 100>100") + "\r\n";
15 sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" OR 100>=100 OR \"错误\"") + "\r\n";
16 //sTemp += ComputeHelper.ComputeBoolean("\"AABBCC\" LIKE \"%BB%\" AND 100>=100 AND \"错误\"") + "\r\n";
17 sTemp += ComputeHelper.ComputeBoolean("(True OR \"错误\") AND (100>200 AND 300>100)") + "\r\n";
18 sTemp += ComputeHelper.ComputeBoolean("(300>200 OR \"错误\") AND ((REPLACE(\"AAA\",\"A\",\"B\")==\"BBB\"?200:100)>150)") + "\r\n";
19
20
21 DateTime dt2 = DateTime.Now;
22
23 Console.WriteLine(sTemp + "计算时间:" + (dt2 - dt1).TotalMilliseconds);
24 Console.ReadLine();
25
26 }
运行结果:
预运算: True False
True
True
False
True
False
True
False
True
计算时间:3.0002
------------------------------------------------------------------------------------
1 [Test]
2 public void TestComputeBooleanList()
3 {
4 List<UserInfo> list = new List<UserInfo>();
5 List<UserInfo> listUser = GetUserInfo();
6 DateTime dt1 = DateTime.Now;
7 ComputeExecuter executer = new ComputeExecuter("UserAge>=20 AND LEN(UserName)>=3 AND REPLACE(UserName,\"舒\",\"龙\") LIKE \"龙%龙\"", true, typeof(UserInfo), string.Empty, true, false, true);
8 foreach (UserInfo user in listUser)
9 {
10 bool result = executer.ComputeBoolean(user);
11 if (result)
12 list.Add(user);
13 }
14 DateTime dt2 = DateTime.Now;
15 string resultStr = GetUserInfos(list);
16 Console.WriteLine(resultStr);
17 Console.WriteLine("预运算:" + (dt2 - dt1).TotalMilliseconds);
18
19 list.Clear();
20 dt1 = DateTime.Now;
21 executer = new ComputeExecuter("UserClassName == \"Y2T01\"", true, typeof(UserInfo), string.Empty, true, false, true);
22 foreach (UserInfo user in listUser)
23 {
24 bool result = executer.ComputeBoolean(user);
25 if (result)
26 list.Add(user);
27 }
28 dt2 = DateTime.Now;
29 resultStr = GetUserInfos(list);
30 Console.WriteLine(resultStr);
31 Console.WriteLine("正式运算:" + (dt2 - dt1).TotalMilliseconds);
32
33 list.Clear();
34 executer = new ComputeExecuter("UserAge>=@UserAge AND UserName LIKE @UserName", true, typeof(UserInfo), string.Empty, true, false, true);
35 dt1 = DateTime.Now;
36 IgnoreHashtable ht = new IgnoreHashtable {{"@UserAge", 20}, {"@UserName", "%舒%"}};
37 foreach (UserInfo user in listUser)
38 {
39 bool result = executer.ComputeBoolean(user, ht);
40 if (result)
41 list.Add(user);
42 }
43 dt2 = DateTime.Now;
44 resultStr = GetUserInfos(list);
45 Console.WriteLine(resultStr);
46 Console.WriteLine("正式运算:" + (dt2 - dt1).TotalMilliseconds);
47
48 list.Clear();
49 dt1 = DateTime.Now;
50 ht = new IgnoreHashtable { { "@UserAge", 18 }, { "@UserName", 3 } };
51 foreach (UserInfo user in listUser)
52 {
53 bool result = executer.ComputeBoolean(user, ht);
54 if (result)
55 list.Add(user);
56 }
57 dt2 = DateTime.Now;
58 resultStr = GetUserInfos(list);
59 Console.WriteLine(resultStr);
60 Console.WriteLine("正式运算:" + (dt2 - dt1).TotalMilliseconds);
61
62
63 }
64
65
66 public static List<UserInfo> GetUserInfo()
67 {
68 List<UserInfo> list = new List<UserInfo>()
69 {
70 new UserInfo("舒小龙","Y2T01001",20,Convert.ToDateTime("1989-11-27"),"Y2T01","应城市",5500),72 new UserInfo("陈雪琴","Y2T03002",19,Convert.ToDateTime("1990-09-01"),"Y2T03","孝感市",6000),
73 new UserInfo("尹华伟","Y2T04003",19,Convert.ToDateTime("1990-11-01"),"Y2T03","孝感市",12000),
74 new UserInfo("舒珊","G2T09001",18,Convert.ToDateTime("1992-09-18"),"G2T09","应城市",200),
75 new UserInfo("无窗帘","G2T09002",18,Convert.ToDateTime("1992-10-02"),"G2T09","孝昌",-300),
76 new UserInfo("吴文","G2T05001",18,Convert.ToDateTime("1991-02-03"),"G2T05","安陆市",4000),
77 new UserInfo("李欢","XG009001",20,Convert.ToDateTime("1990-01-01"),"XG009","孝感市",7000),
78 new UserInfo("HYS","NC008001",19,Convert.ToDateTime("1990-07-19"),"NC008","应城市",500),
79 new UserInfo("毛三","Y2T01001",20,Convert.ToDateTime("1989-01-01"),"Y2T01","应城市",0),
80
81 };
82 return list;
83 }
84
85 public static string GetUserInfos(IList<UserInfo> list)
86 {
87 if (list == null || list.Count <= 0) return string.Empty;
88 StringBuilder sb = new StringBuilder();
89 foreach (UserInfo user in list)
90 sb.AppendLine(user.ToString());
91 return sb.ToString();
92 }
运行结果:
舒小龙 Y2T01001 20 1989-11-27 00:00:00 Y2T01 应城市 5500
预运算:836.0478
舒小龙 Y2T01001 20 1989-11-27 00:00:00 Y2T01 应城市 5500
毛三 Y2T01001 20 1989-01-01 00:00:00 Y2T01 应城市 0
正式运算:2.0001
正式运算:3.0002
正式运算:1