Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- public static class SimpleMathParser
- {
- static List<String> OperationOrder = new List<string>();
- static Dictionary<string, double> Parameters { get; set; }
- static SimpleMathParser()
- {
- Parameters = new Dictionary<string, double>();
- Parameters.Add("Pi", Math.PI);
- Parameters.Add("E", Math.E);
- OperationOrder.AddRange(new string[] { "%", "^", "/", "*", "-", "+" });
- }
- public static string Calculate(string Formula)
- {
- try
- {
- string[] arr = Formula.Split("/+-*()".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- foreach (KeyValuePair<string, double> de in Parameters)
- {
- foreach (string s in arr)
- if (s != de.Key && s.EndsWith(de.Key))
- Formula = Formula.Replace(s, (Convert.ToDouble(s.Replace(de.Key, "")) * de.Value).ToString());
- Formula = Formula.Replace(de.Key, de.Value.ToString());
- }
- while (Formula.LastIndexOf("(") > -1)
- {
- int lastOpenPhrantesisIndex = Formula.LastIndexOf("("),
- firstClosePhrantesisIndexAfterLastOpened = Formula.IndexOf(")", lastOpenPhrantesisIndex);
- double result = ProcessOperation(Formula.Substring(lastOpenPhrantesisIndex + 1, firstClosePhrantesisIndexAfterLastOpened - lastOpenPhrantesisIndex - 1));
- bool AppendAsterix = false;
- if (lastOpenPhrantesisIndex > 0)
- if (Formula.Substring(lastOpenPhrantesisIndex - 1, 1) != "(" && !OperationOrder.Contains(Formula.Substring(lastOpenPhrantesisIndex - 1, 1)))
- AppendAsterix = true;
- Formula = Formula.Substring(0, lastOpenPhrantesisIndex) +
- (AppendAsterix ? "*" : "") +
- result.ToString() +
- Formula.Substring(firstClosePhrantesisIndexAfterLastOpened + 1);
- }
- return ProcessOperation(Formula).ToString();
- }
- catch { return Formula; }
- }
- static double ProcessOperation(string operation)
- {
- var arr = new ArrayList(); string s = "";
- for (int i = 0; i < operation.Length; i++)
- {
- string currentCharacter = operation.Substring(i, 1);
- if (OperationOrder.IndexOf(currentCharacter) > -1)
- {
- if (s != "") arr.Add(s);
- arr.Add(currentCharacter); s = "";
- }
- else s += currentCharacter;
- }
- arr.Add(s); s = "";
- foreach (string op in OperationOrder)
- {
- while (arr.IndexOf(op) > -1)
- {
- int operatorIndex = arr.IndexOf(op);
- double digitBeforeOperator = Convert.ToDouble(arr[operatorIndex - 1]);
- double digitAfterOperator = 0;
- if (arr[operatorIndex + 1].ToString() == "-")
- {
- arr.RemoveAt(operatorIndex + 1);
- digitAfterOperator = Convert.ToDouble(arr[operatorIndex + 1]) * -1;
- }
- else digitAfterOperator = Convert.ToDouble(arr[operatorIndex + 1]);
- arr[operatorIndex] = CalculateByOperator(digitBeforeOperator, digitAfterOperator, op);
- arr.RemoveAt(operatorIndex - 1);
- arr.RemoveAt(operatorIndex);
- }
- }
- return Convert.ToDouble(arr[0]);
- }
- static double CalculateByOperator(double a, double b, string Operator)
- {
- if (Operator == "^") return Math.Pow(a, b);
- else if (Operator == "%") return a % b;
- else if (Operator == "/") return a / b;
- else if (Operator == "*") return a * b;
- else if (Operator == "-") return a - b;
- else if (Operator == "+") return a + b;
- else return Double.NaN;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement