JudgeHelper.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using Core.Mes.Client.Comm.Tool;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Core.StlMes.Client.YdmStuffManage
  7. {
  8. public static class JudgeHelper
  9. {
  10. private static Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
  11. /// <summary>
  12. /// 计算字符串表达式 如:(2*a-8)/d 或者三元运算 (2*a-8)/d > 0 ? 2*a : (2*a + 1)
  13. /// </summary>
  14. /// <param name="formula">输入的表现公式</param>
  15. /// <returns></returns>
  16. public static string Eval(string express)
  17. {
  18. //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
  19. express = powSign(express);
  20. //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
  21. string result = "";
  22. try
  23. {
  24. result = Microsoft.JScript.Eval.JScriptEvaluate(express, ve).ToString();
  25. }
  26. catch (Exception ex)
  27. {
  28. return express;
  29. }
  30. return result;
  31. }
  32. private static string powSign(string express)
  33. {
  34. if (!express.Contains("^")) return express;
  35. //2+(6+2)^2+2^2
  36. String sings = "+-*/()^";
  37. int index = express.IndexOf("^");
  38. char[] strChars = express.Substring(0, index).ToCharArray();
  39. char[] str2Chars = express.Substring(index + 1).ToCharArray();
  40. StringBuilder strBld = new StringBuilder();
  41. strBld.Append(strChars);
  42. int khCnt = 0;
  43. bool isKh = false;
  44. for (int i = strChars.Length - 1; i >= 0; i--)
  45. {
  46. char strChar = strChars[i];
  47. if (strChar == ')')
  48. {
  49. if (i == strChars.Length - 1)
  50. {
  51. isKh = true;
  52. }
  53. khCnt++;
  54. }
  55. else if (strChar == '(')
  56. {
  57. khCnt--;
  58. }
  59. if (isKh)
  60. {
  61. if (khCnt == 0)
  62. {
  63. strBld.Insert(i, "Math.pow(");
  64. break;
  65. }
  66. }
  67. else
  68. {
  69. if (i == 0)
  70. {
  71. strBld.Insert(i, "Math.pow(");
  72. break;
  73. }
  74. else if (sings.IndexOf(strChar) > -1)
  75. {
  76. strBld.Insert(i + 1, "Math.pow(");
  77. break;
  78. }
  79. }
  80. }
  81. //2+(6+2)^2+2^2
  82. StringBuilder str2Bld = new StringBuilder();
  83. str2Bld.Append(str2Chars);
  84. khCnt = 0;
  85. isKh = false;
  86. for (int i = 0; i < str2Chars.Length; i++)
  87. {
  88. char strChar = str2Chars[i];
  89. if (i == 0)
  90. {
  91. str2Bld.Insert(0, ", ");
  92. if (strChar == '(')
  93. {
  94. isKh = true;
  95. }
  96. }
  97. if (strChar == '(')
  98. {
  99. khCnt++;
  100. }
  101. else if (strChar == ')')
  102. {
  103. khCnt--;
  104. }
  105. if (isKh)
  106. {
  107. if (khCnt == 0)
  108. {
  109. str2Bld.Insert(i + 3, ")");
  110. break;
  111. }
  112. }
  113. else
  114. {
  115. if (i == str2Chars.Length - 1)
  116. {
  117. str2Bld.Insert(i + 3, ")");
  118. break;
  119. }
  120. else if (sings.IndexOf(strChar) > -1)
  121. {
  122. str2Bld.Insert(i + 2, ")");
  123. break;
  124. }
  125. }
  126. }
  127. express = strBld.ToString() + str2Bld.ToString();
  128. return powSign(express);
  129. }
  130. public static string GetExpress(string stdMinSign, string stdMin, string stdMaxSign, string stdMax)
  131. {
  132. List<string> expressList = new List<string>();
  133. if (stdMin != "")
  134. {
  135. expressList.Add(stdMinSign + stdMin);
  136. }
  137. if (stdMax != "")
  138. {
  139. expressList.Add(stdMaxSign + stdMax);
  140. }
  141. return string.Join(";", expressList);
  142. }
  143. public static bool JudgeExpress(string value, string express)
  144. {
  145. if (express == "") return true;
  146. if (value == "" && express != "") return false;
  147. if (value == "" && express == "") return true;
  148. string[] expressAry = express.Split(';');
  149. double outValue = 0.0;
  150. if (expressAry.Length == 1)
  151. {
  152. if (expressAry[0].StartsWith("="))
  153. {
  154. if (!double.TryParse(expressAry[0].TrimStart('='), out outValue))
  155. {
  156. return false;
  157. }
  158. expressAry[0] = expressAry[0].Replace("=", "==");
  159. }
  160. return bool.Parse((value + expressAry[0]).Eval());
  161. }
  162. else
  163. {
  164. if (expressAry[0].StartsWith("="))
  165. {
  166. if (!double.TryParse(expressAry[0].TrimStart('='), out outValue))
  167. {
  168. return false;
  169. }
  170. expressAry[0] = expressAry[0].Replace("=", "==");
  171. }
  172. if (expressAry[1].StartsWith("="))
  173. {
  174. if (!double.TryParse(expressAry[1].TrimStart('='), out outValue))
  175. {
  176. return false;
  177. }
  178. expressAry[1] = expressAry[0].Replace("=", "==");
  179. }
  180. return bool.Parse((value + expressAry[0]).Eval()) & bool.Parse((value + expressAry[1]).Eval());
  181. }
  182. }
  183. //public static bool CompareExpress(string value, string express)
  184. //{
  185. // //string[] expressArray = express.TrimStart('<', '>', '=');// Split(new string[] { "=", "<", ">", "<=", ">=" },
  186. // // StringSplitOptions.RemoveEmptyEntries);
  187. // //if (expressArray.Length != 2) return false;
  188. // if (value == "") return false;
  189. // string express1 = value;
  190. // string symbol = "";
  191. // if (express.StartsWith("<="))
  192. // {
  193. // symbol = "<=";
  194. // }
  195. // else if (express.StartsWith(">="))
  196. // {
  197. // symbol = ">=";
  198. // }
  199. // else if (express.StartsWith("="))
  200. // {
  201. // symbol = "=";
  202. // }
  203. // else if (express.StartsWith("<"))
  204. // {
  205. // symbol = "<";
  206. // }
  207. // else if (express.StartsWith(">"))
  208. // {
  209. // symbol = ">";
  210. // }
  211. // else
  212. // {
  213. // return false;
  214. // }
  215. // string express2 = express.TrimStart('<', '>', '=');
  216. // decimal? dcmExp1 = 0;
  217. // decimal? dcmExp2 = 0;
  218. // if (!express1.TryParseDecimal())
  219. // {
  220. // express1 = express1.Replace("C", "1");
  221. // dcmExp1 = express1.CompileFormula();
  222. // }
  223. // else
  224. // {
  225. // dcmExp1 = decimal.Parse(express1);
  226. // }
  227. // if (!express2.TryParseDecimal())
  228. // {
  229. // express2 = express2.Replace("C", "1");
  230. // dcmExp2 = express2.CompileFormula();
  231. // }
  232. // else
  233. // {
  234. // dcmExp2 = decimal.Parse(express2);
  235. // }
  236. // if (symbol == "<=")
  237. // {
  238. // return dcmExp1 <= dcmExp2;
  239. // }
  240. // else if (symbol == ">=")
  241. // {
  242. // return dcmExp1 >= dcmExp2;
  243. // }
  244. // else if (symbol == "=")
  245. // {
  246. // return dcmExp1 == dcmExp2;
  247. // }
  248. // else if (symbol == "<")
  249. // {
  250. // return dcmExp1 < dcmExp2;
  251. // }
  252. // else if (symbol == ">")
  253. // {
  254. // return dcmExp1 > dcmExp2;
  255. // }
  256. // else
  257. // {
  258. // return false;
  259. // }
  260. //}
  261. }
  262. }