PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/DotNet.Framework.Common/FormulaExpress.cs

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