static void Main(string[] args) { while (true) { Console.WriteLine("Please enter any mathematical expression:"); var strExpression = Regex.Replace(Console.ReadLine(), " ", ""); if (strExpression.ToLower() == "exit") { break; } //strExpression = "12 + sqrt((1+3)*abs(-5+1-(2+3-2-3)))*max(-abs(-1),min(-5,-3))-min(12,128)"; //strExpression = "abs(min(a,-100))*sqrt(a*b)+c-d%e"; //strExpression = "(7*9-(-abs(9* 5)/ (9 / 6)))"; //strExpression = "pow(5,2)"; strExpression = "abs(23-4*(8+Pow(6-3,(1+1))*4-10))"; var handler = new ExpressionHandler(strExpression); var variableValues = new List <String>(); foreach (var variableName in handler.VariableNames) { Console.WriteLine(String.Format("Please enter value for [{0}]:", variableName)); var variableValue = Regex.Replace(Console.ReadLine(), " ", "").TrimStart('+'); while (!Helper.IsNumericValue(variableValue)) { Console.WriteLine("Please enter a VALID value!!!"); variableValue = Regex.Replace(Console.ReadLine(), " ", "").TrimStart('+'); } variableValues.Add(variableValue); } var value = handler.Calculate(variableValues); Console.WriteLine(String.Format("Result:\r\n{0}\r\n", value)); } }
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 = ExpressionHandler.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 ComputeUnit(String strExpression) { this.Expression = ExpressionHandler.Normalize(strExpression); }
public override Double Compute() { if (String.IsNullOrWhiteSpace(this.Expression)) { return(Double.NaN); } if (Helper.IsNumericValue(this.Expression)) { return(this.ApplyFunction(Double.Parse(this.Expression))); } var _operatorList = new List <String>(); var _unitList = new List <IComputeUnit>(); try { // prepare int i = 0; while (i < Expression.Length) { var strExpression = Expression.Substring(i); var nextMatch = Regex.Match(strExpression, @"[\+\-*/%([{]"); if (!nextMatch.Success) // must be numeric number { _unitList.Add(new NumericUnit(strExpression)); break; } if (nextMatch.Index == 0) { if (Regex.IsMatch(nextMatch.Value, @"[([{]")) { var subExpression = Helper.GetSubExpression(strExpression); var unit = ExpressionHandler.Parse(subExpression); if (unit == null) { return(Double.NaN); } _unitList.Add(unit); i += subExpression.Length; } else if (Regex.IsMatch(nextMatch.Value, @"[\+\-*/%]")) { Debug.Assert(nextMatch.Index == 0 && nextMatch.Length == 1); _operatorList.Add(strExpression[0].ToString()); i++; } } else { if (Regex.IsMatch(strExpression, @"^[0-9]")) // start with number, it must be a numeric unit { _unitList.Add(new NumericUnit(strExpression.Substring(0, nextMatch.Index))); i += nextMatch.Index; } else // must be a complex unit { var subExpression = Helper.GetSubExpression(strExpression); var unit = ExpressionHandler.Parse(subExpression); if (unit == null) { return(Double.NaN); } _unitList.Add(unit); i += subExpression.Length; } } } // calculate Debug.Assert(_operatorList.Count == _unitList.Count - 1); var operators = new List <String>(); var values = new List <Double>(); Double value = Double.NaN; for (i = 0; i < _operatorList.Count; i++) { if (Double.IsNaN(value)) { value = _unitList[i].Compute(); } if (Double.IsNaN(value)) { return(Double.NaN); } if (_operatorList[i] == "*" || _operatorList[i] == "/" || _operatorList[i] == "%") { if (_operatorList[i] == "*") { value *= _unitList[i + 1].Compute(); } else { var temp = _unitList[i + 1].Compute(); if (temp == 0) { return(Double.NaN); } if (_operatorList[i] == "/") { value /= temp; } else { value %= temp; } } continue; } if (Double.IsNaN(value)) { return(Double.NaN); } if (operators.Any() && operators.Last() == "-") { value *= -1; } values.Add(value); operators.Add(_operatorList[i]); value = Double.NaN; } if (Double.IsNaN(value) && (!_operatorList.Any() || _operatorList.Last() == "+" || _operatorList.Last() == "-")) { value = _unitList.Last().Compute(); } if (!Double.IsNaN(value)) { if (operators.Any() && operators.Last() == "-") { value *= -1; } values.Add(value); } // apply function return(this.ApplyFunction(values.Sum())); } catch { return(Double.NaN); } finally { _operatorList.Clear(); _unitList.Clear(); } }