public override Double Compute() { if (String.IsNullOrWhiteSpace(Expression)) { return(Double.NaN); } var _unitList = new List <IComputeUnit>(); try { var parameters = GetParameterStrings(this.Expression); foreach (var parameter in parameters) { var unit = NumericCalculator.Parse(parameter); if (unit == null) { return(Double.NaN); } _unitList.Add(unit); } var values = _unitList.Select(p => p.Compute()).ToList(); return(this.ApplyFunction(values)); } catch { return(Double.NaN); } finally { _unitList.Clear(); } }
public Double Calculate(List <String> variableValues) { try { if (variableValues == null || this.VariableNames.Count != variableValues.Count || variableValues.Any(p => !Helper.IsNumericValue(p))) { return(Double.NaN); } var strNumericExpression = Regex.Replace(this.Expression, " ", ""); var newValues = variableValues.Select(p => p.TrimStart('+')).Select(p => p[0] == '-' ? String.Format("({0})", p) : p).ToList(); for (int i = 0; i < this.VariableNames.Count; i++) { strNumericExpression = Regex.Replace(strNumericExpression, String.Format(@"\b{0}\b", this.VariableNames[i]), newValues[i]); } var expressionUnit = NumericCalculator.Parse(strNumericExpression); if (expressionUnit != null) { return(expressionUnit.Compute()); } return(Double.NaN); } catch { return(Double.NaN); } }
public ComputeUnit(String strExpression) { this.Expression = NumericCalculator.Normalize(strExpression); }