/// <summary> /// Main method to Calcuate Mathematical Results. /// </summary> /// <param name="equation"></param> /// <returns></returns> internal T Calculate <T>(string equation) { equation = BracketMathematicEquations(equation); return(EvalResults.Evaluate <T>(GetMathematicalExpression <T>(equation))); }
/// <summary> /// As we pass on the Mathematical equation with all kinds of precendence from brackets, /// this method will calcualte all values inside all nested bracket and form an single liner non-nested bracket equation and send it to caller. /// </summary> /// <param name="equation">strign with brackets or no brackets</param> /// <returns>Expression with simplified mathematical equation.</returns> private Expression GetMathematicalExpression <T>(string equation) { //return GenerateEvalutionExpression(SearchAndCalcualteBracketGroupEquations(equation)); while (equation.Contains("(")) { equation = SearchAndCalcualteBracketGroupEquations(equation); } return(GenerateEvalutionExpression(equation)); //Local function to Recurssively get all equations from Brackets and process them to return single line equation. string SearchAndCalcualteBracketGroupEquations(string bracketOperand) { var matches = Regex.Matches(bracketOperand, EvalConstants.OperandParameterNestedBrackests).Cast <Match>(); matches.All(m => { var _val = m.Groups[0].Value.Substring(1); bracketOperand = bracketOperand.Replace($"({_val}", SearchAndCalcualteBracketGroupEquations(_val)); //Recursive Call return(true); }); return(CalculateEquation(bracketOperand)); //Returning Calculated values for the outer bracket equation. } //Local function to get Expression and calculation based on the parsed equation string passed on by caller. string CalculateEquation(string operandEquation) { if (operandEquation.Contains("(") && operandEquation.Contains(")")) { return(operandEquation); } if (operandEquation.EndsWith(")")) { operandEquation = operandEquation.Remove(operandEquation.Length - 1); } var expression = GenerateEvalutionExpression(operandEquation); var result = EvalResults.Evaluate <T>(expression).ToString(); return(result); } //Local function once again to calcualte the values of each group. Expression GenerateEvalutionExpression(string groupOperand) { var _equation = groupOperand; var _operator = ""; var _prevOperator = ""; Expression _exprEquation = null; //Matches all Operators(*,+,/,-,^) and Reverses them to take Right to Left advantage in mathematical calculation. Regex.Matches(groupOperand, EvalConstants.OperandPrameterPattern).Cast <Match>().Reverse().All(m => { //if (Regex.IsMatch(equation, @"[-]\d+$")) _operator = m.Groups[0].Value; var _right = $"{(_operator == "-" ? "-" : "")}{_equation.Substring(_equation.LastIndexOf(_operator) + 1)}"; _equation = _equation.Substring(0, _equation.LastIndexOf(_operator)); _exprEquation = AddOperationExpression(_prevOperator, _exprEquation, _right.ParseNumber <T>()); _prevOperator = _operator; return(true); }); if (!_equation.Empty()) { _exprEquation = AddOperationExpression(_operator, _exprEquation, _equation.ParseNumber <T>()); //for the last one out of loop. } return(_exprEquation); } }