FormulaExpress.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. namespace Ant.Service.Utilities
  3. {
  4. /// <summary>
  5. /// EnumFormula
  6. /// </summary>
  7. public enum EnumFormula
  8. {
  9. Add,//加号
  10. Dec,//减号
  11. Mul,//乘号
  12. Div,//除号
  13. Sin,//正玄
  14. Cos,//余玄
  15. Tan,//正切
  16. ATan,//余切
  17. Sqrt,//平方根
  18. Pow,//求幂
  19. None,//无
  20. }
  21. /// <summary>
  22. /// FormulaDeal
  23. /// </summary>
  24. public class FormulaDeal
  25. {
  26. static FormulaDeal()
  27. {
  28. }
  29. private double CalculateExpress(string strExpression)
  30. {
  31. string strTemp = "";
  32. string strTempB = "";
  33. string strOne = "";
  34. string strTwo = "";
  35. double ReplaceValue = 0;
  36. while (strExpression.IndexOf("+") != -1 || strExpression.IndexOf("-") != -1
  37. || strExpression.IndexOf("*") != -1 || strExpression.IndexOf("/") != -1)
  38. {
  39. if (strExpression.IndexOf("*") != -1)
  40. {
  41. strTemp = strExpression.Substring(strExpression.IndexOf("*") + 1, strExpression.Length - strExpression.IndexOf("*") - 1);
  42. strTempB = strExpression.Substring(0, strExpression.IndexOf("*"));
  43. strOne = strTempB.Substring(GetPrivorPos(strTempB) + 1, strTempB.Length - GetPrivorPos(strTempB) - 1);
  44. strTwo = strTemp.Substring(0, GetNextPos(strTemp));
  45. ReplaceValue = Convert.ToDouble(GetExpType(strOne)) * Convert.ToDouble(GetExpType(strTwo));
  46. strExpression = strExpression.Replace(strOne + "*" + strTwo, ReplaceValue.ToString());
  47. }
  48. else if (strExpression.IndexOf("/") != -1)
  49. {
  50. strTemp = strExpression.Substring(strExpression.IndexOf("/") + 1, strExpression.Length - strExpression.IndexOf("/") - 1);
  51. strTempB = strExpression.Substring(0, strExpression.IndexOf("/"));
  52. strOne = strTempB.Substring(GetPrivorPos(strTempB) + 1, strTempB.Length - GetPrivorPos(strTempB) - 1);
  53. strTwo = strTemp.Substring(0, GetNextPos(strTemp));
  54. ReplaceValue = Convert.ToDouble(GetExpType(strOne)) / Convert.ToDouble(GetExpType(strTwo));
  55. strExpression = strExpression.Replace(strOne + "/" + strTwo, ReplaceValue.ToString());
  56. }
  57. else if (strExpression.IndexOf("+") != -1)
  58. {
  59. strTemp = strExpression.Substring(strExpression.IndexOf("+") + 1, strExpression.Length - strExpression.IndexOf("+") - 1);
  60. strTempB = strExpression.Substring(0, strExpression.IndexOf("+"));
  61. strOne = strTempB.Substring(GetPrivorPos(strTempB) + 1, strTempB.Length - GetPrivorPos(strTempB) - 1);
  62. strTwo = strTemp.Substring(0, GetNextPos(strTemp));
  63. ReplaceValue = Convert.ToDouble(GetExpType(strOne)) + Convert.ToDouble(GetExpType(strTwo));
  64. strExpression = strExpression.Replace(strOne + "+" + strTwo, ReplaceValue.ToString());
  65. }
  66. else if (strExpression.IndexOf("-") != -1)
  67. {
  68. strTemp = strExpression.Substring(strExpression.IndexOf("-") + 1, strExpression.Length - strExpression.IndexOf("-") - 1);
  69. strTempB = strExpression.Substring(0, strExpression.IndexOf("-"));
  70. strOne = strTempB.Substring(GetPrivorPos(strTempB) + 1, strTempB.Length - GetPrivorPos(strTempB) - 1);
  71. strTwo = strTemp.Substring(0, GetNextPos(strTemp));
  72. ReplaceValue = Convert.ToDouble(GetExpType(strOne)) - Convert.ToDouble(GetExpType(strTwo));
  73. strExpression = strExpression.Replace(strOne + "-" + strTwo, ReplaceValue.ToString());
  74. }
  75. }
  76. return Convert.ToDouble(strExpression);
  77. }
  78. private double CalculateExExpress(string strExpression, EnumFormula ExpressType)
  79. {
  80. double retValue = 0;
  81. switch (ExpressType)
  82. {
  83. case EnumFormula.Sin:
  84. retValue = Math.Sin(Convert.ToDouble(strExpression));
  85. break;
  86. case EnumFormula.Cos:
  87. retValue = Math.Cos(Convert.ToDouble(strExpression));
  88. break;
  89. case EnumFormula.Tan:
  90. retValue = Math.Tan(Convert.ToDouble(strExpression));
  91. break;
  92. case EnumFormula.ATan:
  93. retValue = Math.Atan(Convert.ToDouble(strExpression));
  94. break;
  95. case EnumFormula.Sqrt:
  96. retValue = Math.Sqrt(Convert.ToDouble(strExpression));
  97. break;
  98. case EnumFormula.Pow:
  99. retValue = Math.Pow(Convert.ToDouble(strExpression), 2);
  100. break;
  101. }
  102. if (retValue == 0) return Convert.ToDouble(strExpression);
  103. return retValue;
  104. }
  105. private int GetNextPos(string strExpression)
  106. {
  107. int[] ExpPos = new int[4];
  108. ExpPos[0] = strExpression.IndexOf("+");
  109. ExpPos[1] = strExpression.IndexOf("-");
  110. ExpPos[2] = strExpression.IndexOf("*");
  111. ExpPos[3] = strExpression.IndexOf("/");
  112. int tmpMin = strExpression.Length;
  113. for (int count = 1; count <= ExpPos.Length; count++)
  114. {
  115. if (tmpMin > ExpPos[count - 1] && ExpPos[count - 1] != -1)
  116. {
  117. tmpMin = ExpPos[count - 1];
  118. }
  119. }
  120. return tmpMin;
  121. }
  122. private int GetPrivorPos(string strExpression)
  123. {
  124. int[] ExpPos = new int[4];
  125. ExpPos[0] = strExpression.LastIndexOf("+");
  126. ExpPos[1] = strExpression.LastIndexOf("-");
  127. ExpPos[2] = strExpression.LastIndexOf("*");
  128. ExpPos[3] = strExpression.LastIndexOf("/");
  129. int tmpMax = -1;
  130. for (int count = 1; count <= ExpPos.Length; count++)
  131. {
  132. if (tmpMax < ExpPos[count - 1] && ExpPos[count - 1] != -1)
  133. {
  134. tmpMax = ExpPos[count - 1];
  135. }
  136. }
  137. return tmpMax;
  138. }
  139. public string SpiltExpression(string strExpression)
  140. {
  141. string strTemp = "";
  142. string strExp = "";
  143. while (strExpression.IndexOf("(") != -1)
  144. {
  145. strTemp = strExpression.Substring(strExpression.LastIndexOf("(") + 1, strExpression.Length - strExpression.LastIndexOf("(") - 1);
  146. strExp = strTemp.Substring(0, strTemp.IndexOf(")"));
  147. strExpression = strExpression.Replace("(" + strExp + ")", CalculateExpress(strExp).ToString());
  148. }
  149. if (strExpression.IndexOf("+") != -1 || strExpression.IndexOf("-") != -1
  150. || strExpression.IndexOf("*") != -1 || strExpression.IndexOf("/") != -1)
  151. {
  152. strExpression = CalculateExpress(strExpression).ToString();
  153. }
  154. return strExpression;
  155. }
  156. private string GetExpType(string strExpression)
  157. {
  158. strExpression = strExpression.ToUpper();
  159. if (strExpression.IndexOf("SIN") != -1)
  160. {
  161. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("N") + 1, strExpression.Length - 1 - strExpression.IndexOf("N")), EnumFormula.Sin).ToString();
  162. }
  163. if (strExpression.IndexOf("COS") != -1)
  164. {
  165. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("S") + 1, strExpression.Length - 1 - strExpression.IndexOf("S")), EnumFormula.Cos).ToString();
  166. }
  167. if (strExpression.IndexOf("TAN") != -1)
  168. {
  169. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("N") + 1, strExpression.Length - 1 - strExpression.IndexOf("N")), EnumFormula.Tan).ToString();
  170. }
  171. if (strExpression.IndexOf("ATAN") != -1)
  172. {
  173. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("N") + 1, strExpression.Length - 1 - strExpression.IndexOf("N")), EnumFormula.ATan).ToString();
  174. }
  175. if (strExpression.IndexOf("SQRT") != -1)
  176. {
  177. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("T") + 1, strExpression.Length - 1 - strExpression.IndexOf("T")), EnumFormula.Sqrt).ToString();
  178. }
  179. if (strExpression.IndexOf("POW") != -1)
  180. {
  181. return CalculateExExpress(strExpression.Substring(strExpression.IndexOf("W") + 1, strExpression.Length - 1 - strExpression.IndexOf("W")), EnumFormula.Pow).ToString();
  182. }
  183. return strExpression;
  184. }
  185. }
  186. }