Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public static class SimpleMathParser
  6. {
  7. static List<String> OperationOrder = new List<string>();
  8. static Dictionary<string, double> Parameters { get; set; }
  9.  
  10. static SimpleMathParser()
  11. {
  12. Parameters = new Dictionary<string, double>();
  13. Parameters.Add("Pi", Math.PI);
  14. Parameters.Add("E", Math.E);
  15. OperationOrder.AddRange(new string[] { "%", "^", "/", "*", "-", "+" });
  16. }
  17.  
  18. public static string Calculate(string Formula)
  19. {
  20. try
  21. {
  22. string[] arr = Formula.Split("/+-*()".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  23. foreach (KeyValuePair<string, double> de in Parameters)
  24. {
  25. foreach (string s in arr)
  26. if (s != de.Key && s.EndsWith(de.Key))
  27. Formula = Formula.Replace(s, (Convert.ToDouble(s.Replace(de.Key, "")) * de.Value).ToString());
  28. Formula = Formula.Replace(de.Key, de.Value.ToString());
  29. }
  30. while (Formula.LastIndexOf("(") > -1)
  31. {
  32. int lastOpenPhrantesisIndex = Formula.LastIndexOf("("),
  33. firstClosePhrantesisIndexAfterLastOpened = Formula.IndexOf(")", lastOpenPhrantesisIndex);
  34.  
  35. double result = ProcessOperation(Formula.Substring(lastOpenPhrantesisIndex + 1, firstClosePhrantesisIndexAfterLastOpened - lastOpenPhrantesisIndex - 1));
  36.  
  37. bool AppendAsterix = false;
  38.  
  39. if (lastOpenPhrantesisIndex > 0)
  40. if (Formula.Substring(lastOpenPhrantesisIndex - 1, 1) != "(" && !OperationOrder.Contains(Formula.Substring(lastOpenPhrantesisIndex - 1, 1)))
  41. AppendAsterix = true;
  42.  
  43. Formula = Formula.Substring(0, lastOpenPhrantesisIndex) +
  44. (AppendAsterix ? "*" : "") +
  45. result.ToString() +
  46. Formula.Substring(firstClosePhrantesisIndexAfterLastOpened + 1);
  47. }
  48. return ProcessOperation(Formula).ToString();
  49. }
  50. catch { return Formula; }
  51. }
  52.  
  53. static double ProcessOperation(string operation)
  54. {
  55. var arr = new ArrayList(); string s = "";
  56.  
  57. for (int i = 0; i < operation.Length; i++)
  58. {
  59. string currentCharacter = operation.Substring(i, 1);
  60.  
  61. if (OperationOrder.IndexOf(currentCharacter) > -1)
  62. {
  63. if (s != "") arr.Add(s);
  64. arr.Add(currentCharacter); s = "";
  65. }
  66. else s += currentCharacter;
  67. }
  68.  
  69. arr.Add(s); s = "";
  70.  
  71. foreach (string op in OperationOrder)
  72. {
  73. while (arr.IndexOf(op) > -1)
  74. {
  75. int operatorIndex = arr.IndexOf(op);
  76. double digitBeforeOperator = Convert.ToDouble(arr[operatorIndex - 1]);
  77. double digitAfterOperator = 0;
  78.  
  79. if (arr[operatorIndex + 1].ToString() == "-")
  80. {
  81. arr.RemoveAt(operatorIndex + 1);
  82. digitAfterOperator = Convert.ToDouble(arr[operatorIndex + 1]) * -1;
  83. }
  84. else digitAfterOperator = Convert.ToDouble(arr[operatorIndex + 1]);
  85.  
  86. arr[operatorIndex] = CalculateByOperator(digitBeforeOperator, digitAfterOperator, op);
  87. arr.RemoveAt(operatorIndex - 1);
  88. arr.RemoveAt(operatorIndex);
  89. }
  90. }
  91. return Convert.ToDouble(arr[0]);
  92. }
  93.  
  94. static double CalculateByOperator(double a, double b, string Operator)
  95. {
  96. if (Operator == "^") return Math.Pow(a, b);
  97. else if (Operator == "%") return a % b;
  98. else if (Operator == "/") return a / b;
  99. else if (Operator == "*") return a * b;
  100. else if (Operator == "-") return a - b;
  101. else if (Operator == "+") return a + b;
  102. else return Double.NaN;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement