| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using Core.Mes.Client.Comm.Tool;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Core.StlMes.Client.YdmStuffManage
- {
- public static class JudgeHelper
- {
- private static Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
- /// <summary>
- /// 计算字符串表达式 如:(2*a-8)/d 或者三元运算 (2*a-8)/d > 0 ? 2*a : (2*a + 1)
- /// </summary>
- /// <param name="formula">输入的表现公式</param>
- /// <returns></returns>
- public static string Eval(string express)
- {
- //1942.57*(Axc^0.2)/(517^0.9) 8^2^4
- express = powSign(express);
-
- //1942.57*(Math.Pow((double)1, (double)0.2))/(Math.Pow((double)517, (double)0.9))
- string result = "";
- try
- {
- result = Microsoft.JScript.Eval.JScriptEvaluate(express, ve).ToString();
- }
- catch (Exception ex)
- {
- return express;
- }
- return result;
- }
- private static string powSign(string express)
- {
- if (!express.Contains("^")) return express;
- //2+(6+2)^2+2^2
- String sings = "+-*/()^";
- int index = express.IndexOf("^");
- char[] strChars = express.Substring(0, index).ToCharArray();
- char[] str2Chars = express.Substring(index + 1).ToCharArray();
- StringBuilder strBld = new StringBuilder();
- strBld.Append(strChars);
- int khCnt = 0;
- bool isKh = false;
- for (int i = strChars.Length - 1; i >= 0; i--)
- {
- char strChar = strChars[i];
- if (strChar == ')')
- {
- if (i == strChars.Length - 1)
- {
- isKh = true;
- }
- khCnt++;
- }
- else if (strChar == '(')
- {
- khCnt--;
- }
- if (isKh)
- {
- if (khCnt == 0)
- {
- strBld.Insert(i, "Math.pow(");
- break;
- }
- }
- else
- {
- if (i == 0)
- {
- strBld.Insert(i, "Math.pow(");
- break;
- }
- else if (sings.IndexOf(strChar) > -1)
- {
- strBld.Insert(i + 1, "Math.pow(");
- break;
- }
- }
- }
- //2+(6+2)^2+2^2
- StringBuilder str2Bld = new StringBuilder();
- str2Bld.Append(str2Chars);
- khCnt = 0;
- isKh = false;
- for (int i = 0; i < str2Chars.Length; i++)
- {
- char strChar = str2Chars[i];
- if (i == 0)
- {
- str2Bld.Insert(0, ", ");
- if (strChar == '(')
- {
- isKh = true;
- }
- }
- if (strChar == '(')
- {
- khCnt++;
- }
- else if (strChar == ')')
- {
- khCnt--;
- }
- if (isKh)
- {
- if (khCnt == 0)
- {
- str2Bld.Insert(i + 3, ")");
- break;
- }
- }
- else
- {
- if (i == str2Chars.Length - 1)
- {
- str2Bld.Insert(i + 3, ")");
- break;
- }
- else if (sings.IndexOf(strChar) > -1)
- {
- str2Bld.Insert(i + 2, ")");
- break;
- }
- }
- }
- express = strBld.ToString() + str2Bld.ToString();
- return powSign(express);
- }
- public static string GetExpress(string stdMinSign, string stdMin, string stdMaxSign, string stdMax)
- {
- List<string> expressList = new List<string>();
- if (stdMin != "")
- {
- expressList.Add(stdMinSign + stdMin);
- }
- if (stdMax != "")
- {
- expressList.Add(stdMaxSign + stdMax);
- }
- return string.Join(";", expressList);
- }
- public static bool JudgeExpress(string value, string express)
- {
- if (express == "") return true;
- if (value == "" && express != "") return false;
- if (value == "" && express == "") return true;
- string[] expressAry = express.Split(';');
- double outValue = 0.0;
- if (expressAry.Length == 1)
- {
- if (expressAry[0].StartsWith("="))
- {
- if (!double.TryParse(expressAry[0].TrimStart('='), out outValue))
- {
- return false;
- }
- expressAry[0] = expressAry[0].Replace("=", "==");
- }
- return bool.Parse((value + expressAry[0]).Eval());
- }
- else
- {
- if (expressAry[0].StartsWith("="))
- {
- if (!double.TryParse(expressAry[0].TrimStart('='), out outValue))
- {
- return false;
- }
- expressAry[0] = expressAry[0].Replace("=", "==");
- }
- if (expressAry[1].StartsWith("="))
- {
- if (!double.TryParse(expressAry[1].TrimStart('='), out outValue))
- {
- return false;
- }
- expressAry[1] = expressAry[0].Replace("=", "==");
- }
- return bool.Parse((value + expressAry[0]).Eval()) & bool.Parse((value + expressAry[1]).Eval());
- }
- }
- //public static bool CompareExpress(string value, string express)
- //{
- // //string[] expressArray = express.TrimStart('<', '>', '=');// Split(new string[] { "=", "<", ">", "<=", ">=" },
- // // StringSplitOptions.RemoveEmptyEntries);
- // //if (expressArray.Length != 2) return false;
- // if (value == "") return false;
-
- // string express1 = value;
- // string symbol = "";
- // if (express.StartsWith("<="))
- // {
- // symbol = "<=";
- // }
- // else if (express.StartsWith(">="))
- // {
- // symbol = ">=";
- // }
- // else if (express.StartsWith("="))
- // {
- // symbol = "=";
- // }
- // else if (express.StartsWith("<"))
- // {
- // symbol = "<";
- // }
- // else if (express.StartsWith(">"))
- // {
- // symbol = ">";
- // }
- // else
- // {
- // return false;
- // }
- // string express2 = express.TrimStart('<', '>', '=');
- // decimal? dcmExp1 = 0;
- // decimal? dcmExp2 = 0;
- // if (!express1.TryParseDecimal())
- // {
- // express1 = express1.Replace("C", "1");
- // dcmExp1 = express1.CompileFormula();
- // }
- // else
- // {
- // dcmExp1 = decimal.Parse(express1);
- // }
- // if (!express2.TryParseDecimal())
- // {
- // express2 = express2.Replace("C", "1");
- // dcmExp2 = express2.CompileFormula();
- // }
- // else
- // {
- // dcmExp2 = decimal.Parse(express2);
- // }
-
- // if (symbol == "<=")
- // {
- // return dcmExp1 <= dcmExp2;
- // }
- // else if (symbol == ">=")
- // {
- // return dcmExp1 >= dcmExp2;
- // }
- // else if (symbol == "=")
- // {
- // return dcmExp1 == dcmExp2;
- // }
- // else if (symbol == "<")
- // {
- // return dcmExp1 < dcmExp2;
- // }
- // else if (symbol == ">")
- // {
- // return dcmExp1 > dcmExp2;
- // }
- // else
- // {
- // return false;
- // }
- //}
- }
- }
|